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
?
Best Answer
your problem is explained in the nginx documentation. only one of those locations is used, they are not merged together:
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:
Another option might be to use
if ($uri ~ ^/api)
within the regex-location