如何使用traefik将http重定向到https?

sec*_*sec 2 docker docker-compose traefik

我在使用 Traefik 将 http 重定向到 https 流量时遇到了一个小问题。到目前为止,我的带有 acme 的 https 路由器工作正常,但我有两个问题需要解决。

  • 我必须手动指定https://domain通过 https 路由。否则它会尝试通过 http 路由并获得 404。
  • 即使我手动输入https://domain它在大多数情况下都有效,但在某些路径中,它会无缘无故地尝试通过 http 路由,并获得 404。

无论 https 是否输入到地址栏中,如何确保路由始终对路由中的每个路径使用 https?

入口点是 namehttphttps. 我的部分工作设置:

- "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"
Run Code Online (Sandbox Code Playgroud)

Ral*_*rdz 7

此外,如果您使用命令而不是标签来配置它,您可以添加此

 - "--entrypoints.web.http.redirections.entrypoint.to=websecure"
 - "--entrypoints.web.http.redirections.entrypoint.scheme=https"
Run Code Online (Sandbox Code Playgroud)


Al-*_*deh 5

在 traefik 服务本身上添加以下标签,所有 HTTP 流量将被重定向到 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
Run Code Online (Sandbox Code Playgroud)

可以在此处找到有关使用 Traefik 的更多信息和完整示例