【问题标题】:How can I redirect http to https using traefik?如何使用 traefik 将 http 重定向到 https?
【发布时间】:2020-03-03 15:11:22
【问题描述】:

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

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

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

入口点是名称httphttps。我的部分工作设置:

- "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"

【问题讨论】:

    标签: docker docker-compose traefik


    【解决方案1】:

    在 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
    

    有关使用 Traefik 的更多信息和完整示例,请访问 here

    【讨论】:

      【解决方案2】:

      另外,如果你使用命令而不是标签来配置它,你可以添加这个

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

      【讨论】:

        猜你喜欢
        • 2022-01-12
        • 1970-01-01
        • 1970-01-01
        • 2021-07-20
        • 2020-02-09
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 2020-04-01
        相关资源
        最近更新 更多