Search code examples
dockerurl-routinggitlab-ci-runnertraefikmkdocs

traefik V2 middlewares redirect-regex replace-regex not expanding variable


I've an issue wit a redirect-middleware in traefik V2. We want to add a trailing-slash to a sublocation and then remove the path with a PathPrefix-Rule to get correct paths from the docker service. (MkDOCS)

We defined the rule in dynamic_conf.toml for traefik as a general middleware.:

[...]
[http.middlewares.add-trailing-slash.redirectregex]
    regex= "(https?://[^/]+/[a-z0-9_]+)$$"
    replacement= "$${1}/"
    permanent = true
[...]

At the moment this is our label-file included with docker-run:

traefik.enable=true
traefik.http.routers.dockerservice.entryPoints=websecure
traefik.http.routers.dockerservice.rule=PathPrefix(`/dockerservice`)
traefik.http.routers.dockerservice.tls=true
traefik.http.middlewares.dockerservice-strip.stripprefix.prefixes=/dockerservice
traefik.http.routers.dockerservice.middlewares=add-trailing-slash@file,doc-strip

At https://regex101.com/ the rule seems to work fine for eg https://domain.tld/dockerservice

If the service is up and we navigate to https://domain.tld/dockerservice it redirects to https://domain.tld/${1}/ The Variable is not expanded. Instead we get the 404-not found error (as expected because a service route with this name does not exists in our traefik setup)

So the trailing-slash is added as desired, but the dockerservice-capture is not expanded.

We've also tried this as a @docker rule on the label_file for the docker-run command but the "error" remains.

We also tried this which we found on the web first (as @file in dyanmic_conf or @docker as label-file for docker run):

traefik.http.middlewares.add-trailing-slash.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_]+

We where trying with ${0} and multiple other attempts where made using double quotes, and single quotes or $-signs.

Our toolchain is as follows:

  • pushing into the git-repo on the master branach
  • gitlab-runner executes a .sh file with docker build and docker run command
  • label-file is provided in the git-repo

We would like to have a generic redirect for all services which have this middleware added to add a trailing slash if only one Path-Element is added and the trailing slash is missng

So https://domain.tld/dockerservice should redirect to https://domain.tld/dockerservice/ a Request like https://domain.tld/dockerservice/page should not be changed because of the strip in the mkdocs container only /page is needed.

At this point we tried a lot and we don't know why traefik is not expanding the variable.

Anyone knows what we are doing wrong?

Best wishes

Exa.Byte


Solution

  • I've finally found a solution which suits well for our purpose: I just used one $ sign in conjunction with two for the regex option.

    added in dynamic.toml for traefik itself:

    [http.middlewares.add-trailing-slash.redirectRegex]
      regex= "(https?://[^/]+/[a-z0-9_]+)$$"
      replacement= "${1}/"
      permanent = true 
    

    lg exa.byte