Kalsan
Kalsan

Reputation: 1039

Use traefik to run oblivious backend under a prefix

Given the following situation:

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...?:

Upvotes: 0

Views: 646

Answers (3)

I spent a lot of time trying to find a solution, both with Traefik and Nginx.

When finally managed to achieve the desired result with Nginx in the login page, I soon realized that several other obstacles steped ahead: for example, the hrefs of the page's links were all pointed to the / path, making everything break apart when clicked.

In the end, I decided to abandon the cause and follow @larsks recommendation: instead of trying to use my application in the path example.com/path1/ I started serving it in path1.example.com/

Upvotes: 0

Ignacio Heredia
Ignacio Heredia

Reputation: 31

I'm just dealing right now with a similar usecase and I made it partially work using the using HeadersRegexp and the Referer header (like here):

"traefik.http.routers.customprefix.rule=PathPrefix(`/customprefix`) || HeadersRegexp(`Referer`, `.*\\/customprefix\\/.*`)",
"traefik.http.routers.customprefix.middlewares=customprefix",
"traefik.http.middlewares.customprefix.stripprefix.prefixes=/customprefix",

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\/.*

Upvotes: 1

larsks
larsks

Reputation: 312213

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.

Upvotes: 1

Related Questions