Proxy pass location inheritance

Brian Hill hillb at
Mon Feb 17 08:55:02 UTC 2014

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?

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.

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?

From: nginx-bounces at [nginx-bounces at] on behalf of Maxim Dounin [mdounin at]
Sent: Friday, February 14, 2014 4:19 AM
To: nginx at
Subject: Re: Proxy pass location inheritance


On Thu, Feb 13, 2014 at 06:43:08PM +0000, Brian Hill wrote:

> Hello, we are using NGINX to serve a combination of local and
> proxied content coming from both an Apache server (mostly PHP
> content) and IIS 7.5 (a handful of third party .Net
> applications). The proxy is working properly for the pages
> themselves, but we wanted set up a separate location block for
> the "static" files (js, images, etc) to use different caching
> rules. In theory, each of the static file location blocks should
> be serving from the location specified in its parent location
> block, but instead ALL image requests are being routed to the
> root block.


> Three of the four conditions are working properly.
> A request for gets sent to
> A request for gets sent to
> A request for gets sent to
> But the last condition is not working properly.
> A request for should be sent to
> Instead, it's being routed to, which is an invalid location.
> So it appears that the first server location block is catching
> ALL of the requests for the static files.  Anyone have any idea
> what I'm doing wrong?


    location / {
        location ~ regex1 {
            # regex inside /

    location ~ regex2 {
        # regex

The question is: where a request matching regex1 and regex2 will
be handled?

The answer is - in "location ~ regex1".  Locations given by
regular expressions within a matching prefix location are tested
before other locations given by regular expressions.

Maxim Dounin

nginx mailing list
nginx at

More information about the nginx mailing list