nginx basic HTML authentication on a subfolder

June 25, 2015 2.5k views
Nginx

Hello - I am attempting to apply basic HTML authentication to a subfolder on my site. I followed the tutorial - https://www.digitalocean.com/community/tutorials/how-to-set-up-http-authentication-with-nginx-on-ubuntu-12-10 and am able to:
1) Generate a .htpasswd file in the subfolder, and
2) Add the following lines to a location block in the nginx.conf as follows:

  location / {
    try_files $uri/index.html $uri.html $uri @app;
    auth_basic "Restricted";
    auth_basic_user_file /var/www/pepperslice/current/public/ps/jeffaltman/.htpasswd;
}

However, my results are as follows:

  1. There is no prompt for credentials
  2. The entire site and specified subfolder can longer find various css and js files

Any ideas for how I can 1) password protect the subfolder pepperslice.com/ps/jeffaltman and 2) why the paths are lost?

I would also like to use different username and password combinations for other subfolders.

Thanks,

Jeff

1 comment
  • I just tried commenting out:

    try_files $uri/index.html $uri.html $uri @app;
    

    The result is that:

    1. The entire site prompts for credentials - not just the subfolder, and
    2. The entire site and specified subfolder can longer find various css and js files
1 Answer

Hi - After some more research I found a solution and solved the issue. This guide is where I found it - see section 3 - https://www.howtoforge.com/basic-http-authentication-with-nginx

Instead of the location block starting with "location / {...}" (root for site), for a subfolder it should start with the subfolder path. For example:

  location /ps/jeffaltman {
    auth_basic "Restricted";
    auth_basic_user_file /var/www/pepperslice/current/public/ps/jeffaltman/.htpasswd;
}

Also the css and js path problem disappeared.

Cheers.

Have another answer? Share your knowledge.