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;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…