启用基本身份验证时,奇怪的 http 重定向将 nginx 循环到上游的 glassfish

strange http redirection loop nginx to glassfish upstream when basic authentication is enabled

我在 Ubuntu 12.04 服务器中有一个集群 glassfish 实例 运行,nginx 作为 front-end。 我已经在 nginx conf 文件中配置了 glassfish upstream 并且代理参数都已设置。

nginx.conf
    glassfish_custer ( upstream name )

现在的问题是,

我在 glassfish 中添加了一个包含用户名和密码条目的文件领域,以便为我的一个应用程序启用基本身份验证。 我在 web.xml 文件中添加了必要的登录配置参数,捆绑 war 并部署在 glassfish 服务器中,当我启动 url、

http://domain.com/application

它落入了重定向循环

https://domain.com/application

它仅在我启用基本身份验证时发生。如果我关闭,一切都会按预期工作。

我想我需要在 glassfish 管理控制台中为 http 侦听器设置一些代理 header 参数并更改身份验证设置?

如果以前有人遇到过这个问题,请告诉我....

简而言之,如何在以 glassfish 为上游的 nginx 负载均衡器中进行基本身份验证

更新 1: nginx.conf

## http redirects to https ##
server {
    #listen      [::]:80;
    listen      80;
    server_name domain.com www.domain.com;

    location / {
    try_files $uri $uri/ @backend;
    }

    location @backend {
    proxy_buffering off;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header x-forwarded-for $remote_addr;
    proxy_pass http://glassfish_servers;
    proxy_intercept_errors on;
    proxy_set_header        X-Real-IP         $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    }

    # Strict Transport Security
    # add_header Strict-Transport-Security max-age=2592000;
    # rewrite ^/.*$ https://$host$request_uri? permanent;
}

server {
    listen 443 ssl;
    #listen [::]:443 ssl;
    server_name domain.com www.domain.com;

    location / {
    try_files $uri $uri/ @backend;
    }

     ## default location ##
    location @backend {
    proxy_buffering off;
    proxy_pass    http://glassfish_servers;
    proxy_intercept_errors on;

    #proxy_http_version 1.1;
    #proxy_set_header Connection "";

    # force timeouts if the backend dies
    proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

    # set headers
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    #proxy_redirect off;
    }
    ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
    ssl_certificate_key /etc/nginx/ssl/domain_com.key;

    ssl_session_cache shared:SSL:20m;
    ssl_session_timeout 10m;

    ssl_prefer_server_ciphers       on;
    ssl_protocols                   TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers                     ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!$

}

回答我自己的问题。

web.xml 中的 xml 配置是重定向循环的根本原因。 由于我添加了 "CONFIDENTIAL" 作为权限值,当请求命中后端 glassfish 实例时,http 请求被重定向到 https。 我将此值更改为 "NONE" 并且一切正常。

<security-constraint>
    <web-resource-collection>
        <web-resource-name>wholesale</web-resource-name>
        <url-pattern>/acme/wholesale/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>PARTNER</role-name>
    </auth-constraint>
    <user-data-constraint>
        <transport-guarantee>CONFIDENTIAL</transport-guarantee>
    </user-data-constraint>
</security-constraint>

进行以下更改

Change <transport-guarantee>CONFIDENTIAL</transport-guarantee>

<transport-guarantee>NONE</transport-guarantee>

此外,确保在 nginx conf 文件中设置正确的代理 header 值(或者)如果您在 sites-available 文件夹中单独配置站点 conf 文件,请添加以下代理 headers

proxy_set_header x-forwarded-for $remote_addr;
proxy_intercept_errors on;
proxy_set_header        X-Real-IP         $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;