Question
Nginx basic auth - exception by location doesn't work
Hello,
we’ve got an Ubuntu 16.04 and a Nginx server with several virtlual hosts, like
- /var/www/one.test-host.org/html (https://one.test-host.org),
- /var/www/one.test-host.org/html (https://two.test-host.org),
- etc.
We added a basic auth on each of the hosts using this tutorial.
The auth itself works fine, but then we tried to add exceptions to it for pages like that:
https://one.test-host.org*/inner-api/service/…*
So such URLs woudn’t require basic auth.
It’s worth of note that this is a logical path - no such folders exist on the server.
Many of the answers online went like this:
location /inner-api/service/ {
auth_basic off;
}
Or, via map Nginx directive:
map $uri $use_basic_auth {
~/inner-api/service/ off;
default "Restricted";
}
# some other configs...
auth_basic $use_basic_auth;
But this is where things got strange.
At first, adding this code to the host Nginx config resulted in 404 error on any of the URLs to match.
Then, couple of days later, this behavior changed. Now Nginx just doesn’t match the /inner-api/service pattern at all - it gives basic auth on all URLs.
We didn’t do anything with the configs in this couple of days.
What may be the reason for this, and how can we make basic auth exceptions work?
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
×