Generally running the app under an URI prefix when the app itself does not expect it is a tricky thing, and the only reliable solution would be to fix/setup the app making it generate all the assets/routes links either relative or including the prefix it is deployed under. Almost every existing workarounds are to rewrite the application responses "on-the-fly" replacing generated links with the new ones. Some kind of a generic answer is here, some additional considerations can be found here.
However if it is a really SPA, lets say a React app that uses something like HashRouter
rather than BrowserRouter
, a workaround based on the conditional rewrite according to the request Referer
HTTP header is possible:
server {
...
if ($http_referer ~ ^https?://yourdomain.com/app1/) {
rewrite ^ /app1$uri;
}
if ($http_referer ~ ^https?://yourdomain.com/app2/) {
rewrite ^ /app2$uri;
}
...
location /app1/ {
proxy_pass http://container1/;
}
location /app2/ {
proxy_pass http://container2/;
}
}
All the trailing slashes used here are used on purpose, removing any of them will break the solution!
This is not applicable for anything else than SPA (including applications that are using "virtual" routing based on the HTML5 browser history API) since the rewrite logic will be broken after the very first page-to-page transition.