【问题标题】:Nginx Basic Auth not WorkingNginx 基本身份验证不起作用
【发布时间】:2016-01-11 11:47:16
【问题描述】:

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

【问题讨论】:

    标签: authentication nginx configuration


    【解决方案1】:

    您需要在 location 块而不是 server 块中添加 auth_basic 和 auth_basic_user_file。

    location / {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/htpasswd/sub.domain.com.htpasswd;
    return 200 '{hello}';
    
        }
    

    【讨论】:

    • 这是不正确的。 Nginx docsserver 块中允许 auth_basicauth_basic_user_file
    • 另外,docs for ngx_http_auth_basic_module 指定上下文可以是httpserverlocationlimit_except
    【解决方案2】:

    在将基本身份验证添加到配置后,您是否尝试重新加载/停止并启动您的 nginx?有必要重新加载 nginx,例如:

    sudo -i service nginx reload
    

    ----为了使新设置生效。

    另外,我会仔细检查您测试中的 URL。 (有一次我尝试在 Nginx 代理配置中测试 Nginx Basic Auth,访问的是 Nginx 代理后面的资源的实际 URL,而不是 Nginx 的实际 URL。)


    附言

    使用无效的文件路径作为 auth_basic_user_file 的值仍然不会导致 configtest 在 2018 年失败。 这是我的 Nginx 版本:

    nginx version: nginx/1.10.2
    

    虽然无效的文件路径会导致基本身份验证检查失败并导致:

    403 Forbidden
    

    ---- 提供凭据后的 HTTP 响应。

    【讨论】:

      【解决方案3】:

      在我的例子中,将指令添加到 /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;
              }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-06
        • 2014-01-22
        • 2014-12-31
        • 2016-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多