jonny7
January 29, 2021, 5:31pm
1
Hi
We have all our containers running on a subdomain and exposed via ports. Right now for one service I am rewriting from the old subdomain to the new and redirecting the scheme and port to https and whatever port is needed like so:
- traefik.enable=true
- traefik.http.routers.web.rule=Host(`a.example.com`) || Host(`b.example.com`)
- traefik.http.routers.web.entrypoints=my-service
- traefik.http.middlewares.web-https-redirect.redirectscheme.scheme=https
- traefik.http.middlewares.web-https-redirect.redirectscheme.port=8888
- traefik.http.routers.web.middlewares=web-https-redirect
- traefik.http.routers.secure.tls=true
- traefik.http.routers.secure.entrypoints=my-service
- traefik.docker.network=traefik
- traefik.http.routers.secure.rule=Host(`a.example.com`) || Host(`b.example.com`)
- traefik.http.middlewares.sub-redirect.redirectregex.regex=^https://a.example.com(.*)
- traefik.http.middlewares.sub-redirect.redirectregex.replacement=https://b.example.com$${1}
How do I do this globally, rather than replicating all these for each exposed container. I did try in the config, but was unsuccessful
cakiwi
January 29, 2021, 5:35pm
2
Hi @jonny7
You can apply middlewares to an entrypoint. These middlewares are prepended to any a router provide, so would apply to every router using the entrypoint.
I define these middlewares in a file provider or in the traefik service itself so I know they will exist, then use the below option to attach them to the entrypoint.
https://doc.traefik.io/traefik/routing/entrypoints/#middlewares
jonny7
January 29, 2021, 7:58pm
3
Ok
So I wrote this middleware
[http.middlewares.sub-name.redirectRegex]
regex = "`^https://a.example.com(.*)`"
replacement = "`https://b.example.com$${1}`"
permanent = true
And I apply it to a container like:
- traefik.enable=true
- traefik.http.routers.testrail.tls=true
- traefik.http.routers.testrail.entrypoints=testrail
- traefik.docker.network=traefik
- traefik.http.routers.testrail.rule=Host(`a.example.com`) || Host(`b.example.com`)
- traefik.http.routers.testrail.middlewares=sub-name@file
Both URLs are now accessible, but the url doesn't change from a.example.com
to b.example.com
cakiwi
January 29, 2021, 8:27pm
4
No backticks required for regex and replacement. As far as I can tell (and do).
1 Like
jonny7
January 29, 2021, 8:47pm
5
Ok that fixed the redirect, I did update my regex to this:
[http.middlewares.sub-name.redirectRegex]
regex = "http(s)?://a.example.com(.*)"
replacement = "https://b.example.com${2}"
permanent = true
and it in regxr it seems to work fine, but I seem to lose the port, when I try to redirect
Regex101 allows you to create, debug, test and have your expressions explained for PHP, PCRE, Python, Golang and JavaScript. The website also features a community where you can share useful expressions.
As an example if I navigate to http://a.example.com:8005/api/v1/health
it becomes https://b.example.com/api/v1/health
cakiwi
January 30, 2021, 5:29pm
6
Hi Johnny,
Do you have any other redirects? Command line, environment or router labels?
jonny7
February 1, 2021, 2:03pm
7
Hi @cakiwi
Oddly, this seems to be ok now, but http
urls won't redirect to https
. (Maybe traefik hadn't updated properly when I last replied).
As far as I'm aware, only what you see above and the api router of in the dynamic config:
[http.routers]
[http.routers.api]
rule = "Host(`b.example.com`)"
entrypoints = ["websecure"]
middlewares = ["simpleAuth"]
service = "api@internal"
[http.routers.api.tls]
Everything else seems fine, the port redirects fine, so does the domain name. But I get 404 page not found
. Assuming I need a router on the non-tls. Which I added, but it just let me access it over non-https rather than redirect