Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

  • Routing paths with Traefik
  • Is there an equivalent to ReverseProxyPass for Apache in Traefik?

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:

  • Wordpress & Nginx with Docker: Static files not loaded

Any ideas?

like image 987
eventhorizon Avatar asked Apr 03 '18 20:04

eventhorizon


2 Answers

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

like image 165
Victor Barajas Avatar answered Nov 09 '22 09:11

Victor Barajas


Since v2.0, Traefik doesn't support PathPrefixStrip anymore, you need to use a middleware as specified in this article : https://doc.traefik.io/traefik/migration/v1-to-v2/#strip-and-rewrite-path-prefixes 😊

like image 44
Vincent Kelleher Avatar answered Nov 09 '22 09:11

Vincent Kelleher