如何使用 traefik 将 http 重定向到 https?
How can I redirect http to https using traefik?
我 运行 在使用 Traefik 将 http 重定向到 https 流量时遇到了一个小问题。到目前为止,我的带 acme 的 https 路由器工作正常,但我有两个问题需要克服。
- 我必须手动指定
https://domain
才能通过 https 路由。否则它会尝试通过 http 路由并获得 404。
- 即使我手动输入
https://domain
它在大多数情况下都有效,但在某些路径中,它会无缘无故地尝试通过 http 路由,并获得 404。
无论是否在地址栏中输入了 https,我如何才能确保路线中的每条路径始终使用 https?
入口点是名称 http
和 https
。我的部分工作设置:
- "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"
在 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
此外,如果您使用命令而不是标签来配置它,您可以添加这个
- "--entrypoints.web.http.redirections.entrypoint.to=websecure"
- "--entrypoints.web.http.redirections.entrypoint.scheme=https"
要添加到此 ,添加以下内容应该只重定向与 Letsencrypt 证书的 ACME http 验证不匹配的请求
"traefik.http.routers.http_catchall.rule=HostRegexp(`{any:.+}`) && !PathPrefix(`/.well-known/acme-challenge/`)"
我 运行 在使用 Traefik 将 http 重定向到 https 流量时遇到了一个小问题。到目前为止,我的带 acme 的 https 路由器工作正常,但我有两个问题需要克服。
- 我必须手动指定
https://domain
才能通过 https 路由。否则它会尝试通过 http 路由并获得 404。 - 即使我手动输入
https://domain
它在大多数情况下都有效,但在某些路径中,它会无缘无故地尝试通过 http 路由,并获得 404。
无论是否在地址栏中输入了 https,我如何才能确保路线中的每条路径始终使用 https?
入口点是名称 http
和 https
。我的部分工作设置:
- "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"
在 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
此外,如果您使用命令而不是标签来配置它,您可以添加这个
- "--entrypoints.web.http.redirections.entrypoint.to=websecure"
- "--entrypoints.web.http.redirections.entrypoint.scheme=https"
要添加到此
"traefik.http.routers.http_catchall.rule=HostRegexp(`{any:.+}`) && !PathPrefix(`/.well-known/acme-challenge/`)"