I built a web app that will listen on a port and handle HTTP requests. For example, if I run it locally at 127.0.0.1:3000
. I can access it with http://127.0.0.1:3000/path/within/app
on my browser. I would like to deploy it on one of my servers, which is configured with nginx
to handle all incoming requests (and TLS) and forward them to different applications (listening at http://127.0.0.1:xxx
). Normally, I would give each app a different subdomain (e.g., access app1
with app1.example.com
and app2
with app2.example.com
), but it would be more convenient if I can use subpath (e.g., access app1
with example.com/app1
and app2
with example.com/app2
). But I am not sure how to configure it.
My current configuration file is like the following. Suppose my app is listening at 127.0.0.1:3000
.
location ^~ /app1 {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:3000/;
}
I would like to achieve the following.
- When I access
https://example.com/app1
, it will be equivalent to accessing http://127.0.0.1:3000
.
- When I access
https://example.com/app1/path/within/app
, it will be equivalent to access http://127.0.0.1:3000/path/within/app
.
However, with the above mentioned configuration file, only the first part work. If I access https://example.com/app1/path
, my app complaints that it was http://127.0.0.1:3000//path
that actually get accessed, and it doesn't know how to handle //path
.
I would prefer not to modify any part of my application so that it can run independently if I decided to give it a subdomain in the future, and expects a fix with only modifying the nginx configuration file if possible. In addition, I am aware of the problem that any clickable links generated by the app will also need to handle subpath, but this app is simple enough that that is not a problem.
Thanks.