Proxy pass location inheritance

Maxim Dounin mdounin at mdounin.ru
Mon Feb 17 13:13:28 UTC 2014


Hello!

On Mon, Feb 17, 2014 at 08:55:02AM +0000, Brian Hill wrote:

> Close, it's more akin to:
> 
>     location / {
>         location ~ regex1 {
>             # regex inside /
>         }
>     }
> 
>     location ~ regex2 {
>         location ~ regex3 {
>             # regex inside regex2
>         }
>     }
> 
> And the question is: where will a request matching both regex1 
> and regex3 be handled?

Much like in the previous case, regex1 is checked first because 
it's inside a prefix location matched.  And matching stops once it 
matches a request.

> Regex 1 & 3 look for the same file types and are identical, but 
> contain different configurations based on the parent location.  
> Currently, regex1 is catching all matches, irrespective of the 
> parent location.

That's expected behaviour.

> If I understand correctly, I could solve my problem by moving 
> the regex2 location block before the / location block, and then 
> rewriting regex3 so that it included the elements of both the 
> current regex2 and regex3. That way, regex3 would ONLY hit for 
> items that matched both the current regex2 and regex3, and it 
> would appear before regex1 in the order of execution.
> 
> Is this correct, or will NGINX always give priority to the / 
> location?

No.  There is no difference between

    location / {  location ~ regex1 { ... } }
    location ~ regex2 { ... }

and

    location ~ regex2 { ... }
    location / {  location ~ regex1 { ... } }

Locations given by regular expressions within a matching prefix 
location (not necessary "/") are always checked first.

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx mailing list