Nginx Basic Auth not working before re-directing to a subpath of the website

Francis Daly francis at
Fri Feb 19 16:04:20 UTC 2021

On Fri, Feb 19, 2021 at 05:17:28PM +0530, Majid M A wrote:

Hi there,

> I have a scenario that whenever a user hits the website (
>, it should re-direct to the sub-path (/xxx/yyy/) of
> the website (ex:

Your config shows both "proxy_pass" and "return". That is probably not
going to do what you want.

I suspect that if you just remove the "return" line, things may work
better for you.

But I am not certain what response you want to get, for each request,
so there may be some other things to check first.

Maybe you should add

  location = / { return 301 /xxx/yyy/; }

as well?

>      location / {
>        proxy_pass $backend;
>        proxy_pass_header  Authorization;
>        auth_basic "Access Denied or Restricted";
>        auth_basic_user_file /etc/nginx/.htpasswd;
>        return 301;
>        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
>        proxy_set_header Host;
>        client_max_body_size       10m;
>        proxy_http_version 1.1;
>        proxy_set_header Upgrade $http_upgrade;
>        proxy_set_header Connection "upgrade";
>        proxy_read_timeout 900;
>     }
> }

> If you see above in the location block i am using basic auth just
> before the re-direction, but the basic auth is not in effect.

In general, the order of directives as written in nginx conf is not

"return" happens before "auth_basic".

But you possibly don't need the "return" here anyway.

Good luck with it,

Francis Daly        francis at

More information about the nginx mailing list