I'm trying to use nginx to reverse proxy multiple web applications on the same host / port, using a different path to distinguish between applications.
My nginx config looks like the following:
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $proxy_connection;
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 $proxy_x_forwarded_proto;
proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl;
proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;
upstream app1 {
server 192.168.0.1:8080;
}
upstream app2 {
server 192.168.0.2:8080;
}
server {
server_name my-application-server;
listen 80;
location /app1/ {
proxy_pass http://app1/;
}
location /app2/ {
proxy_pass http://app2/;
}
}
This correctly proxies any requests to individual pages on my app - e.g. http://my-application-server/app1/context/login
, but any hyperlinks in my app are broken because they're missing the app1
part of the path - e.g. they direct me to http://my-application-server/context/login-success
rather than http://my-application-server/app1/context/login-success
.
I've tried adding in various values for proxy_redirect
and rewrite
, but nothing I do can convince these links to be rendered correctly.
My app is a java webapp running in tomcat, if that makes any difference. I've seen other solutions where I can change the context path of my webapp, but I need nginx to transparently proxy the requests without the tomcat having to be configured to know about the nginx path.