Global HTTP Redirect in Traefik 2

I'm setting up traefik 2 using a docker-compose file. In order to redirect all HTTP traffic to the equivalent HTTPS address, we can setup the following configuration as labels in the docker-compose file.

    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"  
docker-compose.yml labels

In the above configuration, there is a reference to the web entrypoint. This is configured via the entryPoints entry in traefik.yaml

The advantage of this configuration is that you don't have to do redirects on a per service / route level and there is a single configuration that applies to all providers e.g. docker, kubernetes, file.

Of course, if you have a few services that you must use HTTP for, you will have to figure out a workaround.

References

Global http to https redirect in v2
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.redirecttohttp…