ReplacePathRegex docker provider not working

I have the following labels on one of my docker-compose services. I'm trying to rewrite a url like dev.app.com/api/v5/test to dev.app.com/test. With this setup, when I curl a path like dev.app.com/api/v5/hello, it seems like the middleware is bypassed completely.

I can see the middleware is attached to the router, so I figured i must be using the regex values incorrectly? Thanks!

labels:
      - "traefik.http.routers.internal-api.rule=Host(`dev.app.com`) && Path(`/api/v5`)"
      - "traefik.http.middlewares.internal-api-replace-path.replacepathregex.regex=^/api/v5/(.*)"
      - "traefik.http.middlewares.internal-api-replace-path.replacepathregex.replacement=/$$1"
      - "traefik.http.routers.internal-api.middlewares=internal-api-replace-path@docker"

Hello,

This is working here :

labels:
  - "traefik.http.routers.internal-api.rule=Host(`dev.app.com`) && Path(`/api/v5`)"
  - "traefik.http.routers.internal-api.middlewares=internal-api-replace-path@docker"
  - "traefik.http.middlewares.internal-api.middlewares.replacepathregex.regex=^/app/v5/(.*)"
  - "traefik.http.middlewares.internal-api.middlewares.replacepathregex.replacement=/$1"

The problem i think, is the double '$$'

https://docs.traefik.io/v2.0/middlewares/redirectregex/#configuration-examples

That's strange, I was sure that docker-compose uses $ for variable substitution, and you need to escape it (e.g. $$) to make it interpret it literally.

1 Like

You said it : docker compose uses $ and need escape for variable substitution.

Docker write this line as a labels of the container, this line is not interpreted by docker but by traefik

Was not talking about docker but about docker-compose. If you specify the label in the docker-compose you will have to double the dollar sign to avoid it being interpreted as a variable. In oder for this line to be interpreted by traefik it first needs to get to traefik. This is happening by docker-compose executable reading the yaml, and then executing docker commands, that decorate the container with the label. This all happens before traefik had a chance to read them. Now when the label is set and ready for traefik to be read, the yaml parsing is long finished by then and variables with $ are already substituted.

I'm having a similar issue. I detailed my user case here.

    labels:
      #### core configs
      - "traefik.enable=true"
      # - "traefik.http.routers.caddy.service=nginx" # swarm
      - "traefik.http.routers.nginx.rule=Host(`devkiwi.club`) && PathPrefix(`/nginx`)"
      - "traefik.http.services.nginx.loadbalancer.server.port=80"
      #### set TLS (https)
      - "traefik.http.routers.nginx.entrypoints=websecure"
      - "traefik.http.routers.nginx.tls=true"
      - "traefik.http.routers.nginx.tls.certresolver=leresolver"
      #### use these middlewares (rules like our-auth,our-retry,our-ratelimit are defined within traefik's labels)
      - "traefik.http.routers.nginx.middlewares=our-retry,our-ratelimit,our-header,nginx-strip,nginx-pathregex"
        #___ specific middleware rule for this service
      - "traefik.http.middlewares.nginx-strip.stripprefix.prefixes=/nginx"
        #___ add missing trailing slash / not working :-/
      - "traefik.http.middlewares.nginx-pathregex.replacepathregex.regex=^/nginx/(.*)"
      - "traefik.http.middlewares.nginx-pathregex.replacepathregex.replacement=/nginx/$$1"

When using a single $ docker is giving me this error:

ERROR: Invalid interpolation format for "labels" option in service "nginx": "traefik.http.middlewares.nginx-pathregex.replacepathregex.replacement=/nginx/$1"

@kwngo, have you resolved your issue?

Here is the solution to add a missing trailing slash.