How to: route-based redirection to services

Hi, I am back.

Given a swarm with traefik service and another 2 apps, one at port 3000 and another at 3001, how can we we redirect requests with route /app1 to route 3000 /app2 to route 3001, and therefore the paths as well after /app1 or /app2? Is it even possible?

Best regards,
Bruno Peixoto

You can use Traefik middlewares to handle that, RedirectRegexp to redirect the client/browser to use a different URL (doc) and ReplacePathRegex to change the path internally (doc).

Thanks once again, @bluepuma77. On previous episodes, we redirected incoming traffic from https://customer.example.com:port to respective service defined with such internal port and entrypoints as websecure AND custom-port-entrypoint. GREAT!

For the sake of usability, it would be nice to have https://customer.example.com/customer_name instead of https://customer.example.com:port, do you agree? Will the below definitions on static configuration do the trick (I just redirected to working incoming port-defined routes, without path replacement)

labels:
  - "traefik.http.middlewares.app1-redirect.redirectregex.regex=^https://customer.example.com/app1/(.*)"
  - "traefik.http.middlewares.app1-redirect.redirectregex.replacement=https://customer.example.com:port_1/$${1}"

  - "traefik.http.middlewares.app2-redirect.redirectregex.regex=^https://customer.example.com/app2/(.*)"
  - "traefik.http.middlewares.app2-redirect.redirectregex.replacement=https://customer.example.com:port_2/$${1}"

http is always Traefik dynamic config :wink:

Not sure if using ports is a good idea, many corporate firewalls block strange ports, so business customers might not be able to access your service.

I would probably rather use a subdomain like customer.app2.example.com, then you can use a single wildcard TLS cert again.

Or use a dedicated path, but your app needs to support that, like setting a "base path". Simply using stripPrefix middleware will not work with GUI web apps.

Well, ok. I am trying this stretching approach because, you know, demands are oftentimes weird. The goal here is not to create multiple subdomains, because requires manual actions (Or even web crawling, very ugly).

I thought, Traefik could modify the the incoming URL before requesting from service.

You can modify path of request with ReplacePath and ReplacePathRegex middleware (doc).

1 Like

To make sure:

If I define the middleware with tags below on traefik compose file, we add on service compose file as - "traefik.http.routers.test_service.middlewares=test_redirect". However, there is a caveat of having to restart traefik service everytime a new redirect middleware is defined. Therefore, it seems more reasonable to define and assign on the service compose file.

- "traefik.http.middlewares.test_redirect.redirectregex.regex=^https://customer.example.com/test/(.*)"
- "traefik.http.middlewares.test_redirect.redirectregex.replacement=https://customer.example.com:3000/${1}"

Dynamic config files and labels can be reloaded, no need for restart.

For providers.file you need to enable watch and touch (or edit) the config file itself. providers.swarm can set an update interval.