nginx basic_auth 在移动到外部位置时失败,到“root”

nginx basic_auth 在移动到外部位置时失败,到“root”

这有效:

location /someplace/ {
  auth_basic "Restricted Access";
  auth_basic_user_file /etc/nginx/.htpasswd;
}

我转到/someplace/并提示输入用户和密码,并可以成功验证。但是,将这些确切的行移动到根目录,甚至location /在那时也会产生奇怪的行为:

Chrome 不断要求再次进行身份验证,并且 nginx 的调试输出显示no user/password was provided for basic authentication

服务器配置:

   server {
        listen       443 ssl;
        server_name  some_domain;

        root /some_path;

        passenger_enabled on;
        passenger_ruby /usr/lib/fullstaq-ruby/versions/2.6.6-jemalloc/bin/ruby;
        passenger_env_var RAILS_ENV staging;

        # This causes the issue:
        #auth_basic "Restricted";
        #auth_basic_user_file /etc/nginx/.htpasswd;

        location / {
            index  index.html index.htm;
            # This also causes the same issue:
            #auth_basic "Restricted";
            #auth_basic_user_file /etc/nginx/.htpasswd;
        }

        error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /some_path { return 301 /some_path/; }
        location /some_path/ {
            # This works as expected on some_path:
            auth_basic "Restricted";
            auth_basic_user_file /etc/nginx/.htpasswd;
            if (!-e $request_filename){
                rewrite ^/some_path(/|$) /some_path/index.html break;
            }
        }
    }

答案1

您的应用程序可能也使用 HTTP 身份验证。这意味着,nginx 预期的凭据被接受,但应用程序需要通过 HTTP 身份验证传递另一组凭据。

为了解决这个问题,您需要确保只有一个地方实现 HTTP 身份验证。

相关内容