mon*_*de_ 5 nginx https password-protected
我一直在尝试在 SSL 网站中设置受密码保护的目录,如下所示:
/etc/nginx/sites-available/default
server {
listen 443:
ssl on;
ssl_certificate /usr/certs/server.crt;
ssl_certificate_key /usr/certs/server.key;
server_name server1.example.com;
root /var/www/example.com/htdocs/;
index index.html;
location /secure/ {
auth_basic "Restricted";
auth_basic_user_file /var/www/example.com/.htpasswd;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当我尝试访问 URL 时https://server1.example.com/secure/
,出现“404:未找到”错误页面。
我的 error.log 显示以下错误:
011/11/26 03:09:06 [error] 10913#0: *1 no user/password was provided for basic authentication,
client: 192.168.0.24, server: server1.example.com, request: "GET /secure/ HTTP/1.1",
host: "server1.example.com"
Run Code Online (Sandbox Code Playgroud)
但是,我能够为普通的 HTTP 虚拟主机设置受密码保护的目录,没有任何问题。
是配置有问题还是别的什么?
可能是 auth_basic_user_file 的路径问题,您的绝对路径使 nginx 感到困惑:
http://wiki.nginx.org/HttpAuthBasicModule#auth_basic_user_file
从0.6.7版本开始,文件名路径是相对于nginx配置文件nginx.conf的目录,而不是相对于nginx前缀目录。
我不确定你在其他情况下是如何工作的。文档中的引用表明您应该拥有类似的内容:
auth_basic_user_file htpasswd/www.example.com
Run Code Online (Sandbox Code Playgroud)
如果您的服务器配置文件为“/etc/nginx/nginx.conf”,则密码文件为“/etc/nginx/htpasswd/www.example.com”。
您可以通过运行“sudo nginx -t”来检查 nginx 配置。如果存在配置错误,即身份验证文件不在预期的位置,它应该在那时告诉您。