The approach is right:
# traefik.toml
## static configuration
[entryPoints]
[entryPoints.web]
address = 80
[entryPoints.websecure]
address = 443
[providers.file]
directory = "/dynamic/"
# /dynamic/redirect.toml
## dynamic configuration
[http.routers]
[http.routers.redirecttohttps]
entryPoints = ["web"]
middlewares = ["httpsredirect"]
rule = "HostRegexp(`{host:.+}`)"
service = "noop"
[http.services]
# noop service, the URL will be never called
[http.services.noop.loadBalancer]
[[http.services.noop.loadBalancer.servers]]
url = "http://192.168.0.1"
[http.middlewares]
[http.middlewares.httpsredirect.redirectScheme]
scheme = "https"
with Docker:
version: "3.7"
services:
traefik:
image: traefik:v2.1.2
command:
- --entrypoints.web.address=:80
- --entrypoints.websecure.address=:443
- --providers.docker=true
ports:
- 80:80
- 443:443
labels:
traefik.http.routers.http-catchall.rule: hostregexp(`{host:.+}`)
traefik.http.routers.http-catchall.entrypoints: web
traefik.http.routers.http-catchall.middlewares: redirect-to-https@docker
traefik.http.middlewares.redirect-to-https.redirectscheme.scheme: https
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
https://docs.traefik.io/migration/v1-to-v2/#http-to-https-redirection-is-now-configured-on-routers