Welcome! Log In Create A New Profile

Advanced

Re: misunderstood regex location

B.R.
May 06, 2014 01:28PM
On Tue, May 6, 2014 at 5:03 PM, dcaillibaud <nginx-forum@nginx.us> wrote:

> I understand my mistake, thanks to both of you.
>
> May I suggest to insist on this in
> http://nginx.org/en/docs/http/ngx_http_core_module.html#location, with a
> remark on the fact that ^~ is not usable with a regex, for example with
>
> syntax:
> location [ = | ^~ ] uri { ... }
> location ~ | ~* regexUri { ... }
> location @name { ... }
>
> and adding there Igor advice (repeated many times in this forum like in
> http://forum.nginx.org/read.php?2,247529,247718 or
> http://forum.nginx.org/read.php?2,174517,174534#msg-174534), that it's a
> better practice to have a prefix location list at first level then put
> regex
> location within
>

​The $docs must stay as concise as possible, so I do not think Igorf advice
should be there, even irf it is very interesting information, should be
there.
A 'best practice/performance enhancement' page could host it though.

That wriktten, I think that yes, the docs could be clearer, by inserting:
"If the longest matching prefix location has the “^~” modifier then regular
expressions are not checked. "
inside the third para​graph, which takes the process step-by-step but is
incomplete (one could understand every request processes regexes).
This sentence, isolated at the end of the section, produces a leap backward
in the reasoning flow and loses the reader in his attempt to understand how
things work. Definitely not user-friendly.



>
> Reviewing all my server definitions, I'm starting to imagine what a
> nightmare could be, with on the first level a mix of standard prefix
> location, ^~ and ~ ^/ ;-)
>
> I was also wondering wich has precedence for /images/foo.jpeg in these
> case,
>
>
> 1) (I guess D, because ^/ before prefix shortcut regex)
>

> location ^~ /images/ {
> location ~* \.(gif|jpg|jpeg)$ {
> [ configuration D ]
> }
> }
>
> location ~* \.(gif|jpg|jpeg)$ {
> [ configuration E ]
> }
>

​​Right​. That is however exactly the example provided by the docs.
"If the longest matching prefix location has the “^~” modifier then regular
expressions are not checked."​
The regex is not even checked against the input request string.


> 2) (I guess E but I'm not so sure), this can also be a pitfall example (how
> complicating things can lead to unexpected behaviour)
>
> location /images/ {
> location ~* \.(gif|jpg|jpeg)$ {
> [ configuration D ]
> }
> }
>
> location ~* \.(gif|jpg|jpeg)$ {
> [ configuration E ]
> }
>

"Among [prefix locations], the location with the longest matching prefix is
selected and remembered. Then regular expressions are checked, in the order
of their appearance in the configuration file."
​The prefix string matches, it is remembered, but then the regex is checked
and also matches.
"The search of regular expressions terminates on the first match, and the
corresponding configuration is used."
The request processing stops on the first matching regex.


> 2bis) this makes 2) understandable (more predictable)
>
> location /images/ {
> location ~* \.(gif|jpg|jpeg)$ {
> [ configuration D ]
> }
> location ~* \.svg$ {
> [ configuration D2 ]
> }
> }
>
> location / {
> location ~* \.(gif|jpg|jpeg|svg|pdf)$ {
> // static stuff not in /images/
> [ configuration E ]
> }
> }
>

​Right IMHO.
---
*B. R.*
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

misunderstood regex location

dcaillibaud May 05, 2014 06:38PM

Re: misunderstood regex location

Francis Daly May 05, 2014 07:42PM

Re: misunderstood regex location

Igor Sysoev May 06, 2014 02:56AM

Re: misunderstood regex location

dcaillibaud May 06, 2014 11:03AM

Re: misunderstood regex location

B.R. May 06, 2014 01:28PM

Re: misunderstood regex location

dcaillibaud May 06, 2014 02:39PM

Re: misunderstood regex location

dcaillibaud May 06, 2014 02:53PM

Re: misunderstood regex location

B.R. May 07, 2014 09:14AM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 44
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready