Shouldn't the `StripPrefix` middleware also adjust the `Location` response header?

Hi,
so I have an issue with the following configuration:

- "traefik.enable=true"
- "traefik.http.middlewares.app-stripprefix.stripprefix.prefixes=/my-app"
- "traefik.http.middlewares.app-stripprefix.stripprefix.forceslash=true"
- "traefik.http.routers.app.rule=PathPrefix(`/my-app{end:\\/|$$}`)"
- "traefik.http.routers.app.middlewares=app-stripprefix"

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.

Hi,

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.

Thanks!

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/dev my-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 ...

server {
    root /app/public;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass php-fpm:9001;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param HTTPS off;
        internal;
    }

    error_log /var/log/nginx/symfony_error.log;
    access_log /var/log/nginx/symfony_access.log;
}

Any help would be welcome :slight_smile:

ps : I can't have sub-domain :crazy_face: for now ...