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!
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.
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.
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"