1

I'm trying to setup Traefik to replace my Nginx reverse proxy setup. I have my domain setup like this: - I can access different services using http://example.com and detecting which service I should proxy to, by using subpath.

Nginx config of reverse proxy

I tried to recreate same setup with Traefik. First by using ReplacePathRegex and then StripPrefix. My api requests work fine, but when I try to use Portainer, requests made from web browser omit /portainer part which causes web ui to break.

For example: I make request to http://example.com/portainer. I get response and then consecutive requests should be made like this: http://example.com/portainer/vendor1.css and instead it makes request like this: http://example.com/vendor1.css

Is there any way to setup Traefik behavior to exactly match Nginx proxy_pass?

I found this post that gives me a partial solution: Is there an equivalent to ReverseProxyPass for Apache in Traefik?. Portainer seems to be working when I make request to http://example.com/portainer/ by adding "/" to the end. I tried to fix it with forceSlash, but it did not make any change

Current setup for Portainer with Traefik

Is it possible to make it work with or without trailing "/"?

This is how requests look like in /portainer and /portainer/: /portainer request

/portainer/ request

This is my current Traefik configuration (I'm only using Docker): Traefik configuration

Jan Jaworski
  • 139
  • 3
  • 12

2 Answers2

0

Stripprefix middleware is for removing prefixes from the path before forwarding the request. According to your configuration, everything after /portainer and /portainer itself will be stripped. In your case, it will also remove the trailing slash. If you need to forward / to the Portainer, then you need to use portainer (without slash) as to configure the prefix of stripprefix middleware.

humbaba
  • 318
  • 2
  • 10
  • That is not the case, as I need to forward it to "/" inside Portainer container. This is the behavior I have with Nginx that works fine. Without `stripPrefix` page does not load at all, as I get 404 not found. – Jan Jaworski Feb 21 '20 at 12:16
  • Sorry, I'm confused. Ok, then you need stripprefix the middleware. But if you configure it like as it is above, the part of the path `/portainer/whatever/whatever/...` will be just removed from the path. if you need to forward `/` into portainer and your prefix should be `portainer` instead of `/portainer`. – humbaba Feb 21 '20 at 12:26
  • I tried setting `PathPrefix` to `/portainer/` and behavior was the same as with `/portainer`. Then I set it to `portainer` and in all cases I receive 404. I also tried these combinations with `stripprefix` and results were the same. – Jan Jaworski Feb 21 '20 at 13:10
  • I also tried using `RedirectRegex` to redirect user from `http://example.com/portainer` to `http://example.com/portainer/`, but it did not work. – Jan Jaworski Feb 21 '20 at 13:27
  • Can you check what url is forwarded to portainer? I have a kubernetes setup and I tried stripprefix, it works as expected. – humbaba Feb 21 '20 at 14:08
  • Where can I check it? I'm using only Docker for my setup. – Jan Jaworski Feb 21 '20 at 14:37
  • I updated my post with additional information. I hope this helps clarify my setup. – Jan Jaworski Feb 21 '20 at 14:43
0

I found a solution: https://community.containo.us/t/middleware-to-add-the-if-needed/1895

This is what I had to add to labels in my portainer container to make it work:

- traefik.http.middlewares.strip-prefix.chain.middlewares=strip-prefix-1,strip-prefix-2
- traefik.http.middlewares.strip-prefix-1.redirectregex.regex=^(https?://[^/]+/[a-z0-9_]+)$$
- traefik.http.middlewares.strip-prefix-1.redirectregex.replacement=$${1}/
- traefik.http.middlewares.strip-prefix-1.redirectregex.permanent=true
- traefik.http.middlewares.strip-prefix-2.stripprefixregex.regex=/[a-z0-9_]+

It is not ideal solution as I think there should be an easier way to achieve it, but for the time being it satisfies my needs.

Gajus
  • 69,002
  • 70
  • 275
  • 438
Jan Jaworski
  • 139
  • 3
  • 12