将 nginx 转发到 Apache Tomcat 无限循环

将 nginx 转发到 Apache Tomcat 无限循环

我对 nginx 还很陌生。

我想将两个子域转发到我的 apache tomcat 服务器中的两个应用程序。

我在网上搜索后发现,rewrite 确实会转发,但我看不到转发。从日志中我看到,它陷入了无限循环。

这是我的服务器配置

http {
     upstream tomcat {
       server tomcat.ip.address:8080;
      }
      server {
        listen 80;
        server_name  sub1.domain.com;
        location / {
            proxy_pass    http://tomcat/app1
            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_redirect http://tomcat/app1 http://sub1.domain.com;
            proxy_buffering off;
            proxy_store     off;

        }
      }
      server {
        listen 80;
        server_name  sub2.domain.com;
        location / {
            proxy_pass    http://tomcat/demo;
            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_redirect http://tomcat/demo http://sub2.domain.com;
            proxy_buffering off;
            proxy_store     off;
        }
      }
}

每当我调用 sub1.domain.com 或 sub2.domain.com 时,我都会收到一条消息,提示存在重定向循环问题并且无效?

那么,我该如何解决这个问题?

答案1

这是正确的做法。希望对您有帮助:

https://wiki.duraspace.org/display/DSPACE/NginX+Tomcat+Proxying

答案2

我会尝试这个:

http{
  upstream tomcat {
    tomcat.ip:8080;
  }
  server {
    listen 80;
    server_name  subdomain1.domain.com;
    location / {
      proxy_pass    http://tomcat/app1;
    }
  }
  server {
    listen 80;
    server_name  subdomain2.domain.com;
    location / {
      proxy_pass    http://tomcat/app2;
    }
  }
  ....

看着nginx 文档

相关内容