I have a blog at https://oberbrunner.com. It's served by my traefik instance running in a docker-swarm. Traefik does the SSL termination and I have a docker server for the actual web server (using nginx).
I'm trying to replace this with https://blog.oberbrunner.com which is now hosted by cloudflare pages (including SSL). So I want to strip out all of the oberbrunner.com config from traefik, and just have it permanent-redirect everything from http[s]://oberbrunner.com to https://blog.oberbrunner.com.
I tried to do this, but I get "bad gateway" errors. Here's my traefik config:
# Traefik static (initial) config file
# We use docker and a config file to provide dynamic configuration
providers:
docker: {}
file:
directory: /etc/traefik/config
watch: true
entryPoints:
web:
address: ":80"
http:
redirections:
entryPoint:
to: websecure
websecure:
address: ":443"
http:
tls: {}
certificatesResolvers:
myresolver:
acme:
...
# Redirect with domain replacement
# Note: all dollar signs need to be doubled for escaping.
labels:
- "traefik.http.middlewares.test-redirectregex.redirectregex.regex=^http://localhost/(.*)"
- "traefik.http.middlewares.test-redirectregex.redirectregex.replacement=http://mydomain/$${1}"
Enable and check Traefik debug log and access log.
Thanks -- I've just tried 2.11 and 3.1; same issue (3.1 had some other issues, I'd probably have to update other parts of my config). I couldn't change provider to swarm, since I'm only using docker-compose. I'm using your first example, with the middleware defined in my traefik-dynamic.yaml along with the call to the middleware in the router. I do need a router for that host still, right? I don't want traefik to do SSL termination, just return a redirect.
You still need to terminate TLS, otherwise Traefik can not process any https requests. It needs to decrypt TLS, match the domain, and send a http redirect back encrypted to the client/browser.
Hmm. Perhaps traefik can't do what I want then. I was hoping to just repoint my DNS record to the new web host, but as it is at the root of the domain, I can't easily do that.
Ah, good point. So I need to terminate the oberbrunner.com ssl (with a traefik-managed cert) and then redirect to the actual blog (which has its own SSL cert). That makes sense. I guess that means the redirect regex should be http:// since the SSL will already have been terminated by the time the middleware sees it.
One followup question though: I'm using acme/letsencrypt for the certs, using http challenge via :80. That authenticates via a special http route, right? Will that work given the total redirect?
Hi, sorry to keep on this -- I'm almost there I think. I'm terminating SSL and have set up to use middleware, but now I have a redirect loop because it's not using my replacement URL properly.
My dynamic config:
middlewares:
# redirect oberbrunner.com to blog
# Note: TLS has already been terminated, so use http urls
oberbrunner-redirect-to-blog:
redirectRegex:
regex: "^http://oberbrunner.com/(.*)"
replacement: "https://blog.oberbrunner.com/$${1}"
permanent: true
Clearly it's using my middleware because I'm getting a 308 as expected. But it's not applying the replacement -- just using the original URL as location. What could I be doing wrong?
$-escaped $${1} is needed in Docker labels, not in yaml file.
Note that with permanent: true you are shooting yourself in the foot, as the browser will cache the value and not request again. So updates now will have no effect on what's happening in the browser.
Thanks -- tried without $$ and removed permanent: true (good point). Same error.
I think the actual 308 is coming from my http-to-https redirect here, now commented out:
because with that commented out, I just get a 404 when I curl (curl should avoid any caches). So my middleware isn't actually getting triggered, looks like.
The log only says this:
the 127.0.0.1 is presumably from the dummy service I had to add to the router, even though the redirect should catch everything. Looks like the middleware isn't triggering, and the dummy service serves a 404.
(Note: I'm running Traefik 2.11 now.)
EDIT: aha -- using ^https?://oberbrunner.com as the regex (i.e. adding optional "s") makes it work. So I guess the regex is applied to the URL before TLS termination.
Whew -- thanks for sticking with me til I got it working!