Nginx + Tomcat 8 + SSL 反向代理

Nginx + Tomcat 8 + SSL 反向代理

我遵循这个例子:

https://devtidbits.com/2015/12/08/nginx-as-a-reverse-proxy-to-apache-tomcat/

我的目标是运行 Tomcat 并在其前面运行 Nginx。

我想要类似这样的结构:

test.com/ - 来自 nginx 的静态站点

test.com/tomcat-tomcat 管理器(带有有效链接)

test.com/app - tomcat 应用程序(部署在 tomcat 上的 /app 上)

app2.test.com/app

我的 Nginx 配置是:

upstream tomcat {
    server 127.0.0.1:8080 fail_timeout=0;
}
server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    include snippets/ssl-test.com.conf;
    include snippets/ssl-params.conf;
    root /var/www/test.com/html;
    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;
    server_name test.com www.test.com;
    location / {
        # First attempt to serve request as file, then as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    location ~ /.well-known {
                allow all;
        }

    location ~ ^/tomcat(/?)(.*)$ {  # OOPS!
      proxy_pass http://tomcat/$2$is_args$args;  # OOPS!
    }

    location /tomcat/ {
       include proxy_params;
       proxy_pass http://tomcat/;

        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  expires 1M;
 }
}

先感谢您!

答案1

请尝试以下一个位置,而不是两个tomcat位置:

location ~ /tomcat(.*)$ {
    include proxy_params;
    proxy_pass http://tomcat$1$is_args$args;

    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

相关内容