So I make a request to localhost/my-app/some/path and it gets correctly forwarded to app-container/some/path.
An issue rises when the server decides to make a redirect. It sets the Location-header for example to / and Traefik passes the header unmodified to the client. I'd expect Traefik (more precisely the StripPrefix-Middleware) to adjust the response header by prepending the previously stripped prefix to the Location header, so the original / would become /my-app/.
Finally my questions: Am I wrong in the regard? What is the correct way to handle my issue?
So the application in question is a NodeJS application, so I made some adjustments to fix this issue in this end, by defining a middleware that prepends the stripped prefix on redirect:
app.use((req, res, next) => {
req.baseUrl = req.get("x-forwarded-prefix") || req.baseUrl;
req.originalUrl = req.baseUrl + req.originalUrl;
const { redirect } = res;
// we rewrite the redirect method to prepend the baseUrl if applicable
res.redirect = (...args) => {
const newArgs = Array.from(args);
const redirectPath = args.slice(-1)[0];
// only handle domain relative paths like `/my/path`
if (typeof redirectPath === "string" && redirectPath.startsWith("/")) {
const newPath = req.baseUrl + redirectPath;
// replace the old path with the new path
newArgs.splice(-1, 1, newPath);
}
// call the original `redirect` method with the new arguments
redirect.apply(res, newArgs);
};
next();
});
It works but I'd prefer an option in Traefik to make this work.
Did you find any solution for this using Traefik without changing the application?
I manage to this with Apache server with the ProxyPassReverse and also the ProxyPassReverseCookiePath options.
I want to fully migrate to Traefik but I don't want to change my application to work with the proxy, it should be the other way around.
Hi
Same traefik conf than @Hafas here.
Same issue with a dev env deployment with PathPrefix where i try to have multiple instance my-hostname.com/devmy-hostname.com/pre-prod using sub-directories with nginx as web server and Symfony as app.
The PathPrefix work in traefik > nginx but when app need to redirect (for each request) I loose the prefix and got 404.
I suppose i need to adjust my nginx conf to achieve it ...