Search code examples
reverse-proxytraefik

Can traefik rewrite the location header of redirect responses (302)


I am using traefik 2.0.2 as reverse proxy in front of some services. One backend services is returning a redirect response (302), where the location header contains the absolute redirected url. The url of the backend is not reachable from the outside, how can I rewrite the location to go through the reverse proxy again?

E.g. a client requests http://my-domain/foo and receives a 302 response with location header containing http://backend:8080/foo/bar/, which of course will not work.

I am looking for something similar to ProxyPassReverse of apache mod_proxy. I have read through the available middlewares of traefik, but nothing seems to fit my requirement.

My simplified configuration:

# traefik.yml
entryPoints:
  web:
    address: ":80"

providers:
  file:
    filename: "dynamic-conf.yml"

# dynamic-conf.yml
http:
  routers:
    router1:
      entryPoints:
        - web
      service: service1
      rule: "PathPrefix(`/foo`)"
  services:
    service1
      loadBalancer:
        servers:
          - url: http://backend:8080

Solution

  • I did not find an option to rewrite the location header of a service response using traefik. A feature request to replaceResponseHeaders exists.

    My (temporary) solution is to perform the redirection in traefik using the RedirectRegex middleware, such that the backend service does not need to response with a redirect.

    The updated configuration would look like this:

    # dynamic-conf.yml
    http:
      routers:
        router1:
          entryPoints:
            - web
          service: service1
          rule: "PathPrefix(`/foo`)"
          middlewares:
            - my-redirect
      middlewares:
        my-redirect: # Workaround for service1 redirection
          redirectRegex:
            regex: "^https?://[^/]+/foo/?$"
            replacement: "/foo/webapp/"
      services:
        service1
          loadBalancer:
            servers:
              - url: http://backend:8080