'How can I redirect http to https using traefik?

I am running into a slight issue with redirecting http to https traffic with Traefik. So far, my https router with acme is working fine, but I have two problems I am try to overcome.

  • I have to manually specify https://domain to go through the the https route. Otherwise it tries to go through the http route and gets a 404.
  • Even when I am manually typing in https://domain it works for the most part, but in some paths, for no reason, it will try to go via the http route, and gets a 404.

How can I make sure that the route is always using https for every path in the route whether https was typed into the address bar?

The entrypoints are name http and https. My partially working setup:

- "traefik.enable=true"
- traefik.port=8000
- traefik.backend=myapp
- traefik.http.routers.myapp.rule=Host(`sub.mydomain.com`)
- "traefik.http.routers.myapp.entrypoints=https"
- "traefik.http.routers.myapp.tls.certresolver=myresolver"
# redir http to https
- "traefik.http.routers.myapp-secure.rule=Host(`sub.mydomain.com`) && PathPrefix({p:.+})"
- "traefik.http.routers.myapp-secure.entrypoints=https"
- "traefik.http.middlewares.myapp-secure.redirectscheme.scheme=https"
- "traefik.http.routers.myapp.middlewares=myapp-secure"
- "traefik.http.routers.myapp-secure.tls=true"


Solution 1:[1]

Add the below labels on the traefik service itself and all the HTTP traffic will be redirected to HTTPS

        - traefik.http.middlewares.https_redirect.redirectscheme.scheme=https
        - traefik.http.middlewares.https_redirect.redirectscheme.permanent=true
        - traefik.http.routers.http_catchall.rule=HostRegexp(`{any:.+}`)
        - traefik.http.routers.http_catchall.entrypoints=http
        - traefik.http.routers.http_catchall.middlewares=https_redirect

More info and full examples for using Traefik can be found here

Solution 2:[2]

Also if you configure it using commands, instead of labels, you can add this

 - "--entrypoints.web.http.redirections.entrypoint.to=websecure"
 - "--entrypoints.web.http.redirections.entrypoint.scheme=https"

Solution 3:[3]

To add to this answer, adding the following should only redirect for requests not matching the ACME http validation for Letsencrypt certs

"traefik.http.routers.http_catchall.rule=HostRegexp(`{any:.+}`) && !PathPrefix(`/.well-known/acme-challenge/`)"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Al-waleed Shihadeh
Solution 2 Ralexrdz
Solution 3 OzzieFZI