我正在尝试密码保护我的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。
发布于 2015-10-14 05:26:36
您需要在location块中添加auth_basic和auth_basic_user_file,而不是在server块中。
location / {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd/sub.domain.com.htpasswd;
return 200 '{hello}';
}发布于 2018-08-23 17:32:23
在将基本身份验证添加到配置后,您是否尝试重新加载/停止并启动nginx?需要重新加载nginx,如下所示:
sudo -i service nginx reload-为了使新设置起作用。
我也会仔细检查你测试下的网址。(有一次,我尝试在一个Nginx代理配置中测试Nginx Basic Auth,它访问的是位于Nginx代理后面的资源的实际URL,而不是Nginx的实际URL。)
附注:
使用无效的文件路径作为auth_basic_user_file的值仍然不会导致2018年的配置测试失败。以下是我的Nginx版本:
nginx version: nginx/1.10.2尽管无效的文件路径会导致基本身份验证检查失败,并导致:
403 Forbidden-提供凭据后的HTTP响应。
发布于 2020-06-30 03:49:39
在我的例子中,向/etc/nginx/sites-available/default添加指令有效,而向/etc/nginx/nginx.conf添加指令无效。
当然,只有当您的nginx.conf文件中包含以下内容时,才会发生这种情况:
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;配置很简单(把它放在你网站特定部分的位置下,或者放在你整个网站的服务器下):
server {
location /foo/ {
auth_basic "This part of website is restricted";
auth_basic_user_file /etc/apache2/.htpasswd;
}
}https://stackoverflow.com/questions/33107535
复制相似问题