authentication - Nginx 基本身份验证不起作用

标签 authentication nginx configuration

我正在尝试使用密码保护我的 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/

相关文章:

python - Turbogears 通过函数调用改变用户

ruby-on-rails - current_user 如何在 devise rails 中工作

ruby-on-rails - Nginx + unicorn (rails) 在 nginx 错误日志中经常给出 "Connection refused"

java - 升级到 Struts 2.3.15 - 约定插件导致启动时崩溃

sql - Postgres 9.3 pg_stat_activity.query 长度不会改变

c# - 使用证书身份验证访问 Web 服务和 HTTP 接口(interface)

iphone - iOS Swift Storyboard 如何在登录后和登录前显示 View

nginx - 强制 nginx 写一个空的/空白的 http 授权请求 header

nginx rtmp 代理流热插拔?

android - 如何在 galaxy trend plus gt s 7580 上运行 eclipse 项目