How to rewrite paths with Traefik when using path prefix rules?

eventhorizon picture eventhorizon · Apr 3, 2018 · Viewed 18k times · Source

My Traefik config for WordPress contains the following docker-labels:

- "traefik.backend=wordpress"
- "traefik.docker.network=web"
- "traefik.frontend.rule=Host:MyHostName.net;PathPrefix:/blog"
- "traefik.enable=true"
- "traefik.port=80"

Now requesting the url "https://MyHostName/blog" seems to reach the service which seems to return a redirect to "https://MyHostName/wp-admin...".

I cannot use subdomains.

How can I solve this?

UPDATE 0

First thing to do should be adding the Filter "PathPrefixStrip:/blog" to remove the "/blog" prefix when forwarding the request to the service. Correct?

But how do I modify (for example) a redirect request to add the prefix "/blog" to the redirect URL?

UPDATE 1

At https://github.com/containous/traefik/issues/985 my question is "discussed" and a solution seems to be merged (https://github.com/containous/traefik/pull/1442).

In short: Stripped prefixes will be added as the respective header (X-Forwarded-Prefix).

I will check that and write down the results here.

Additional resources:

UPDATE 2

Now I created a request looking like this:

https://MYHOSTNAME/blog

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: de,en-US;q=0.7,en;q=0.3
Connection: keep-alive
Cookie: ocuvhr6ala6i=d2cd9020839889a752b4375a63dedad0; oc_sessionPassphrase=qJu13Q%2FlAoSsv5b0qC18Re%2BcrcML6o32c2XuDJEGViIMI4uERIf%2Bs77DvFbMSkEBkZs%2Bn%2FfnUjdB9APvk4zq2qlj6AiDXX2CGYf31MPVci8HkgcsXFcpL7cRLBbRGRWS; __Host-nc_sameSiteCookielax=true; __Host-nc_sameSiteCookiestrict=true
Host: MYHOSTNAME
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0

The "PathPrefixStrip" seems to work in the direction CLIENT>>SERVICE. This is what my traefik log contains:

traefik    | time="2018-04-04T18:12:54Z" level=debug msg="vulcand/oxy/roundrobin/rr: competed ServeHttp on request" Request="
{
   "Method":"GET",
   "URL":{
      "Scheme":"",
      "Opaque":"",
      "User":null,
      "Host":"",
      "Path":"/",
      "RawPath":"",
      "ForceQuery":false,
      "RawQuery":"",
      "Fragment":""
   },
   "Proto":"HTTP/2.0",
   "ProtoMajor":2,
   "ProtoMinor":0,
   "Header":{
      "Accept":[
         "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
      ],
      "Accept-Encoding":[
         "gzip, deflate, br"
      ],
      "Accept-Language":[
         "de,en-US;q=0.7,en;q=0.3"
      ],
      "Cookie":[
         "ocuvhr6ala6i=d2cd9020839889a752b4375a63dedad0; oc_sessionPassphrase=qJu13Q%2FlAoSsv5b0qC18Re%2BcrcML6o32c2XuDJEGViIMI4uERIf%2Bs77DvFbMSkEBkZs%2Bn%2FfnUjdB9APvk4zq2qlj6AiDXX2CGYf31MPVci8HkgcsXFcpL7cRLBbRGRWS; __Host-nc_sameSiteCookielax=true; __Host-nc_sameSiteCookiestrict=true"
      ],
      "Upgrade-Insecure-Requests":[
         "1"
      ],
      "User-Agent":[
         "Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0"
      ],
      "X-Forwarded-Prefix":[
         "/blog"
      ]
   },
   "ContentLength":0,
   "TransferEncoding":null,
   "Host":"MYHOSTNAME",
   "Form":null,
   "PostForm":null,
   "MultipartForm":null,
   "Trailer":null,
   "RemoteAddr":"81.128.35.176:33468",
   "RequestURI":"/",
   "TLS":null
}
"

But the redirection answer looks as follows in my browser:

HTTP/2.0 302 Found
cache-control: no-cache, must-revalidate, max-age=0
content-length: 0
content-type: text/html; charset=UTF-8
date: Wed, 04 Apr 2018 18:44:18 GMT
expires: Wed, 11 Jan 1984 05:00:00 GMT
location: https://MYHOSTNAME/wp-admin/install.php
server: Apache/2.4.25 (Debian)
X-Firefox-Spdy: h2
x-powered-by: PHP/7.2.2

So the redirect-response does not contain any information about the stripped path prefix "/blog".

UPDATE 3

At the end it looks like a problem of the served software inside the container that does not handle the header.

Additional resources:

Any ideas?

Answer

Victor Barajas picture Victor Barajas · Aug 3, 2018

Maybe you should add all possible values in your PathPrefixStrip: / blog rule eg.

PathPrefixStrip: /blog,/wp-admin,/abc,/xyz

In many cases, it works for standard routes. The biggest problem is when your backend service does not listen to requests in the root / but in some sub-dir /something/index.html and that sub-dir takes resources from the root /.