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

Majid M A majid210484 at
Fri Feb 19 11:47:28 UTC 2021

Dear Nginx Users,

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:

So re-direction to a website's sub-path ( is

I have implemented http basic auth, so whenever the user is re-directed to
sub-path /xxx/yyy/, the basic auth should come into effect and then the
website has to be re-directed.

The issue is basic auth is not coming into effect before re-directing to
the web-site's sub-path it just re-directs.

This is how my nginx config looks like

    listen 443 ssl;
    set $backend "";

     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.

Any ideas or suggestions are highly appreciated.

nginx version: nginx/1.16.1

Thanks & Regards,
Majid M A
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list