Report this

What is the reason for this report?

nginx basic HTML authentication on a subfolder

Posted on June 25, 2015

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



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.