Home  >  Q&A  >  body text

Problem with nginx reverse proxy tomcat

Hello everyone, I have a question

Problem Description:

location ^~ /tomcat/ {
            proxy_pass   http://127.0.0.1:8080/;
            proxy_redirect  off;
            proxy_set_header  X-Real-IP $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        }

location ^~ /ife2017/ {
            proxy_pass   http://127.0.0.1:8080/ife2017/;
            proxy_redirect  off;
            proxy_set_header  X-Real-IP $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
            }

The above is part of my nginx configuration, the purpose is

1. Jump all requests from www.abc.com/tomcat/xxx to tomcat, the function is normal

2. Jump all requests for www.abc.com/ife2017/xxx to tomcat, and then jump to ife2017 through tomcat. The function is not normal

Note: ife2017 is a folder in the root directory of tomcat, it can be accessed normally before using nginx proxy

The specific performance is that www.abc.com/ife2017/123 can jump normally without a port number
But www.abc.com/ife2017/123/456 cannot jump normally. Jump to www.127.0.0.1.com/ife2017/123/456

My current solution is to change 127.0.0.1 to the actual domain name, but in this case the port number 8080 will be added after the jump. Although the problem is not big, I still want a perfect solution

Thank you all in advance

巴扎黑巴扎黑2714 days ago443

reply all(1)I'll reply

  • 某草草

    某草草2017-05-16 17:11:04

    Reverse proxy configuration problem, there is no similar option in tomcat并不知道他在nginx后面,所以发送的重定向响应头仍然是使用后端的地址。在apache中可以通过配置ProxyPassReverse选项修改后端发给client的响应头来实现,在nginx, so add proxy information to the request sent to the backend (tell tomcat that there is a proxy in front of it):

    server {
        listen myhost:80;
        server_name  myhost;
        location / {
            root /path/to/myapp/public;
            proxy_set_header X-Forwarded-Host $host:$server_port;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://myapp:8080;
        }
    }

    Reference document: NGINX Solution for Apache ProxyPassReverse

    reply
    0
  • Cancelreply