使用 Nginx 作为反向代理的 Tomcat 应用程序 (JIRA) 的基本身份验证

sdu*_*ude 5 tomcat nginx reverse-proxy confluence jira

我有几个运行 Atlassian Tomcat 应用程序的子域(jira.example.com、confluence.example.com、stash.example.com),我想知道是否可以basic_auth使用.htpasswd.

Nginx 在没有 basic_auth 指令的情况下工作正常,但是如果我尝试在nginx.conf...

user              nginx;
worker_processes  1;

error_log         /var/log/nginx/error.log;
pid               /var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] $request '
                      '"$status" $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # Load config files from the /etc/nginx/conf.d directory
    include /etc/nginx/conf.d/*.conf;

    # Our self-signed cert
    ssl_certificate     /etc/ssl/certs/fissl.crt;
    ssl_certificate_key /etc/ssl/private/fissl.key;

    # Password
    auth_basic "Restricted";
    auth_basic_user_file /home/passwd/.htpasswd;

    # redirect non-ssl Confluence to ssl
    server {
        listen 80;
        server_name  confluence.example.com;
        rewrite ^(.*) https://confluence.example.com$1 permanent;
    }

    # redirect non-ssl Jira to ssl
    server {
        listen 80;
        server_name  jira.example.com;
        rewrite ^(.*) https://jira.example.com$1 permanent;
    }

    #
    # The Confluence server
    #
    server {
        listen       443;
        server_name  confluence.example.com;

        ssl on;

        access_log  /var/log/nginx/confluence.access.log  main;
        error_log   /var/log/nginx/confluence.error.log;

        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_set_header X-Forwarded-Proto  https;
            proxy_set_header Host $http_host;            
        }

        error_page  404              /404.html;
        location = /404.html {
           root   /usr/share/nginx/html;
        }

        redirect server error pages to the static page /50x.html

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
           root   /usr/share/nginx/html;
        }

    }

    #
    # The Jira server
    #
    server {
        listen       443;
        server_name  jira.example.com;

        ssl on;

        access_log  /var/log/nginx/jira.access.log  main;
        error_log   /var/log/nginx/jira.error.log;

        location / {
            proxy_pass http://127.0.0.1:9090/;
            proxy_set_header X-Forwarded-Proto  https;
            proxy_set_header Host $http_host;
        }

        error_page  404              /404.html;
        location = /404.html {
            root   /usr/share/nginx/html;
        }

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

    }
}
Run Code Online (Sandbox Code Playgroud)

..它要求提供凭据,但随后它返回一个

HTTP 状态 401 - 基本身份验证失败 - 原因:AUTHENTICATION_DENIED

看起来有人设法通过 apache 作为反向代理运行http://jira.10933.n7.nabble.com/mod-proxy-and-password-protecting-td17279.html解决了这个问题,但该线程中的那些链接已死.. .

编辑: 显然这个问题可以在 Apache 添加tomcatAuthentication="false"和使用protocol="AJP/1.3"Tomcat 的 server.xml 连接器中轻松解决。或者您可以阻止 apache 使用指令转发身份验证RequestHeader unset authorization。问题是,如何用 Nginx 做到这一点?我想我必须研究更多..有什么见解吗?

sdu*_*ude 11

好的,刚刚在nginx 邮件列表上找到了解决方案。我只需要告诉 nginx 不要将 auth 标头转发给 tomcat。在位置块中添加几行就nginx.conf成功了:

  location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://127.0.0.1:8090/;
        proxy_redirect off;

        # Password
        auth_basic "Restricted";
        auth_basic_user_file /home/passwd/.htpasswd;

        # Don't forward auth to Tomcat
        proxy_set_header   Authorization "";
    }
Run Code Online (Sandbox Code Playgroud)

现在我只需要弄清楚如何防止 nginx 要求对每个子域(jira、confluence、stash 等)进行身份验证。只需为所有人​​引入一次凭证就完美了,但这是另一个问题。

希望这可以帮助!

干杯。