Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

Nginx Reverse Proxy for Tomcat Multiple App

I have a tomcat server, and 3 applications run under it. I want to serve them in their own domains using Nginx Reverse proxy, but I couldn't configure it.

Example:

https://api.example.com > localhost:8081/api

https://data.example.com > localhost:8081/data

Nginx Conf.

location / {

      proxy_set_header        Host $host;
      proxy_set_header        X-Real-IP $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header        X-Forwarded-Proto $scheme;
      proxy_redirect off;
      proxy_pass          http://localhost:8081/api;
      proxy_read_timeout  90;
      sub_filter /api/ /;
    
    }
question from:https://stackoverflow.com/questions/66054355/nginx-reverse-proxy-for-tomcat-multiple-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Changing the URI path between NGINX and Tomcat can create many problems: e.g. many applications use absolute URI paths for associated resources (images, etc.). NGINX will not be able to rewrite them all.

Therefore I believe the best solution is to create several <Host>s on Tomcat:

<Engine name="Catalina" defaultHost="api.example.com">
    <Valve className="org.apache.catalina.valves.RemoteIpValve"
           protocolHeader="x-forwarded-proto" />
    <Host name="api.example.com"  appBase="webapps/api">
    ...
    </Host>
    <Host name="data.example.com"  appBase="webapps/data">
    ...
    </Host>
</Engine>

and deploy your applications under the name ROOT.war in either webapps/api or webapps/data. This way your NGINX configuration can look like:

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass       http://localhost:8081;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...