1

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/

1 Answer 1

0

You can take a look at the nginx auth basic docs! Try to use "auth_basic off;" and "allow all". I have a suspicion that there may be an additional issue at play.

try this:

location /appname/ {
    proxy_pass http://127.0.0.1:11399/;
    proxy_set_header Host $host;
    proxy_set_header X-Real_IP $remote_addr;
    allow all;
    auth_basic off;
}

You must log in to answer this question.

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