1

The following rules duplicate the proxy directives and I would prefer to only mention them once:

location /view/ {
    # Duplicated!
    proxy_pass http://127.0.0.1:9292;
    proxy_set_header Host $host;
}

location /api/ {
    # Duplicated!
    proxy_pass http://127.0.0.1:9292;
    proxy_set_header Host $host;

    auth_basic "esqulino Testbetrieb";
    auth_basic_user_file /srv/svn/users.conf;
}

There are more locations then the ones that I have mentioned here, so specifying the proxy directives globally is not an option.

My naive approach was to simply extend the first location with a regex, mention the proxy there and then only specifying the auth section for the API:

location ~ /(api|view)/ {
    proxy_pass http://127.0.0.1:9292;
    proxy_set_header Host $host;
}

location /api/ {
    auth_basic "esqulino Testbetrieb";
    auth_basic_user_file /srv/svn/users.conf;
}

But for some reason this does not trigger the authentication at all.

How could I mention the proxy (or actually any) directives only once for a set of locations and still "overload" some more specific locations with additional directives like auth_basic?

1
  • @downvoter Would you care to explain? If there is anything unclear I am more then happy to provide additional information. Jul 13, 2016 at 9:09

1 Answer 1

3

your problem is explained in the nginx documentation. only one of those locations is used, they are not merged together:

To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, 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 search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used. (http://nginx.org/en/docs/http/ngx_http_core_module.html#location)

as an alternative you could use an include and move the duplicated config lines to another file - for example below /etc/nginx/snippets/

EDIT The idea with nested locations from our comments:

Though I am not sure this is 100% correct as I did not test it, yet, try something like this:

location ~ /(view|api) {
    proxy_pass http://127.0.0.1:9292;
    proxy_set_header Host $host;

    location /api/ {
        auth_basic "esqulino Testbetrieb";
        auth_basic_user_file /srv/svn/users.conf;
    }
}

Another option might be to use if ($uri ~ ^/api) within the regex-location

3
  • This perfectly expains why this doesn't work, but I am not quite comfortable with using another file. Is there a possibility to define some kind of constant and refer to that instead of putting these two lines in a new file? Jul 13, 2016 at 10:06
  • 1
    The very same documentation mentions that one could nest locations. Though I haven't used this feature, yet. Jul 13, 2016 at 10:08
  • This seems to be exactly what I am looking for, thanks a lot for pointing me in the right direction. Jul 13, 2016 at 10:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .