Given the following situation:
- Actual entrypoint from the web is https://my.tld/customprefix
- The application is unaware of the prefix and listens to
/
This means that the application will redirect to /login
and serve assets as /style.css
, but from the Internet those resources are to be accessed as /customprefix/login
and /customprefix/style.css
.
SSL is terminated at the reverse proxy, which is also expected to strip the prefix. With Nginx or Apache that wouldn’t be a problem (e.g. ProxyPass
with Nginx). However, I want to use Traefik as reverse proxy and I can’t find the right combination of middlewares to make this happen:
Traefik’s StripPrefix
correctly maps /customprefix/...
to /...
but that is for requests only. The response of the backend is left untouched, all paths pointing to /
instead of being rewritten as /customprefix
.
How can I get Traefik to re-write the response as well for…?:
- Redirects
- Assets
- Cookies
2
Answers
Converting my comments into an answer:
How would you solve this with nginx or apache? Making this work requires filtering HTML from the backend to rewrite links (e.g. something like
mod_proxy_html
, and even in that case generally fails when links are generated dynamically using Javascript (because this is running in the client browser so your proxy pipeline has no opportunity to make changes to the result).I would say that either the backend needs to be aware of the prefix, or it needs to use a relatively small, well known set of paths so that you can create appropriate redirections for all of them on the frontend.
If you have the option, you can use host-based rather than path-based rules to direct traffic to the backend, in which case you can redirect all paths as appropriate.
I’m just dealing right now with a similar usecase and I made it partially work using the using
HeadersRegexp
and theReferer
header (like here):It correclty mapped some routes but other failed. Give it a try. Anyway, I agree with @larsks comment.
Note: I’m deploying with Nomad, which parses
hcl
, in other cases you might need to use the regexp without double\
like.*/customprefix/.*