Traefik Http to Https Non-Global Redirect - Is There a Better Way?

I have recently finished setting up Traefik with multiple configs but haven't been able to find a solution. I am using docker-compose with dynamic configurator and want to correctly redirect http to https.

tl;dr: Do I have to declare both http and https entrypoints to redirect http -> https in Traefik? Really? No better way? Not including an entrypoint-based global redirect (not considering that)

For the service whoami, to achieve a simple combo of local service + http auth + https + http redirect I need the following:

whoami:
    ...
    labels:
      - traefik.enable=true

      # External
      - traefik.http.routers.whoami.rule=Host(`whoami.example.com`)
      - traefik.http.routers.whoami.entrypoints=https
      - traefik.http.routers.whoami.tls.certresolver=lets-encrypt-tls
      - traefik.http.routers.whoami.middlewares=simpleAuth@file

      # External http redirect
      - traefik.http.routers.whoami-http.rule=Host(`whoami.example.com`)
      - traefik.http.routers.whoami-http.entrypoints=http
      - traefik.http.routers.whoami-http.middlewares=redirect-https@file
      # - traefik.http.routers.whoami-http.middlewares=simpleAuth@file # Do I need it at all?

      # Local
      - traefik.http.routers.whoami-local.rule=Host(`whoami.local`)
      - traefik.http.routers.whoami-local.entrypoints=http
      - traefik.http.routers.whoami-local.middlewares=simpleAuth@file

Relevant configs:

### Static config:
entryPoints:
  http:
    address: :80
  https:
    address: :443

### Dynamic config:
 middlewares:
    redirect-https:
      redirectScheme:
        scheme: https
        permanent: false

My question is: Is there a better, more concise way to achieve the redirect? Naturally, outside of redirecting on the entrypoints level as I want to retain per-container control of redirects.

I initially thought that I could do:

# External .dev
      - traefik.http.routers.whoami.rule=Host(`whoami.example.com`)
      - traefik.http.routers.whoami.entrypoints=http, https
      - traefik.http.routers.whoami.tls.certresolver=lets-encrypt-tls
      - traefik.http.routers.whoami.middlewares=redirect-https@file

However that doesn't work, unfortunately.

I would probably be more ok with it if I could store all of that in the dynamic configuration file, but because --traefik.enable=ture creates un-deletable and unmanageable from dynamic configuration routes and services (seemingly indented), I have to define all routes in the docker-compose file.

2
Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article