nginx basic HTML authentication on a subfolder

Hello - I am attempting to apply basic HTML authentication to a subfolder on my site. I followed the tutorial - 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 and 2) why the paths are lost?

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



Show comments

Submit an answer

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!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi - After some more research I found a solution and solved the issue. This guide is where I found it - see section 3 -

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.