Disclaimer: Im not close with nginx
I had this location block inside my server:
location /appname/ {
proxy_pass http://127.0.0.1:11399/;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
}
I want to set basic auth on every page except for /appname/
.
So if path is /appname/
then just pass this to the docker container on 11399 port. But if path is anything else (^/appname/.+
) then ask to auth before.
Important! I can not edit outer server block since it contains tons of other apps on other locations and I need this to apply only to this location block.
I tried this:
location = /appname/ {
proxy_pass http://127.0.0.1:11399/;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
}
location ~ ^/appname/(.+)$ {
auth_basic "Hello hiiii";
auth_basic_user_file /path/to/.htpasswd;
proxy_pass http://127.0.0.1:11399/$1;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
}
It made sense to me, if the path is /appname/
then go to location = /appname/
block. If not, go to regex block. But when I access myhost.com/appname/
I get prompted with auth form for some reason. It doesnt make sense, not in any world /appname/
matches ^/appname/.+
regex expression. Looks like something goes behind the nginx's curtains that I am not aware of.
I will also appreciate a lot if someone can suggest me a way to make this work using one location block with nested ones. I dont like that prev example has duplicate lines. I tried something like this
location /appname/ {
auth_basic "Hello hiiii";
auth_basic_user_file /path/to/.htpasswd;
proxy_pass http://127.0.0.1:11399/;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
location = /appname/ {
auth_basic off;
}
}
And a lot of it's variants but none worked. Mostly it gave 404 when accessing /appname/