我正在尝试使用密码保护我的 Nginx 配置中的默认服务器。但是,当我访问该站点时,没有显示用户名/密码对话框。 Nginx 像往常一样返回内容。这是完整的配置:
worker_processes 1;
events
{
multi_accept on;
}
http
{
include mime.types;
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
tcp_nodelay on;
gzip on;
# Set path for Maxmind GeoLite database
geoip_country /usr/share/GeoIP/GeoIP.dat;
# Get the header set by the load balancer
real_ip_header X-Forwarded-For;
set_real_ip_from 0.0.0.0/0;
real_ip_recursive on;
server {
listen 80;
server_name sub.domain.com;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd/sub.domain.com.htpasswd;
expires -1;
access_log /var/log/nginx/sub.domain.com.access default;
error_log /var/log/nginx/sub.domain.com.error debug;
location / {
return 200 '{hello}';
}
}
}
有趣的是,当我尝试使用无效的文件路径作为 auth_basic_user_file 的值时,configtest 仍然通过。这不应该是这种情况。
这是 Nginx 和系统信息:
[root@ip nginx]# nginx -v
nginx version: nginx/1.8.0
[root@ip nginx]# uname -a
Linux 4.1.7-15.23.amzn1.x86_64 #1 SMP Mon Sep 14 23:20:33 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
我们正在使用 yum 提供的 Nginx RPM。
最佳答案
您需要在 location block 而不是 server block 中添加 auth_basic 和 auth_basic_user_file 。
location / {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd/sub.domain.com.htpasswd;
return 200 '{hello}';
}
关于authentication - Nginx 基本身份验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33107535/