Nginx basic HTML authentication on a subfolder

时间:2015-06-25 19:16:03

标签: authentication nginx .htpasswd

I need help applying basic HTML authentication to password protect a subfolder on my site - hosted by Digitalocean served using Nginx. I followed the tutorial - https://www.digitalocean.com/community/tutorials/how-to-set-up-http-authentication-with-nginx-on-ubuntu-12-10. But my results are: 1. The entire site prompts for credentials rather than the specified subfolder, and 2. All the pages on the site can longer find css and js files. Here's what I tried: 1) Generated a .htpasswd file in the subfolder, 2) Added a location block in the nginx.conf file (see below), and 3) Reloaded nginx. location / { auth_basic "Restricted"; auth_basic_user_file /var/www/pepperslice/current/public/ps/jeffaltman/.htpasswd; } The complete nginx.config file is as follows: upstream unicorn { server unix:/tmp/unicorn.pepperslice.sock fail_timeout=0; } server { listen 80 default; root /var/www/pepperslice/current/public; try_files $uri/index.html $uri @unicorn; location = /images { root /var/www/pepperslice/current/public/images; } location @unicorn { proxy_pass http://unicorn; } location / { auth_basic "Restricted"; auth_basic_user_file /var/www/pepperslice/current/public/ps/jeffaltman/.htpasswd; } error_page 500 502 503 504 /500.html; } At the moment, the location block I added is commented out. I am seeking help on how I can: 1. password protect the subfolder pepperslice.com/ps/jeffaltman, and 2. password protect other subfolders using different username and password combinations. Also, any ideas why the css and js paths failed? I am guessing once the authentication problem is fixed, the css/js path problem will go away. Thanks.

1 个答案:

答案 0 :(得分:0)

经过一些研究后,我找到了解决方案并解决了问题。本指南是我找到的地方 - 请参阅第3节 - https://www.howtoforge.com/basic-http-authentication-with-nginx

而不是以" location / {...}"开头的位置块(站点的根目录),对于子文件夹,它应该以子文件夹路径开头。例如:

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

css和js路径问题也消失了。

干杯。