https - traefik - HTTP 到 HTTPS WWW 重定向

标签 https traefik

我找不到与此类似的问题,还有其他人提到了 https 重定向,但没有提到最小化重定向。

一直在寻找解决方案,但尚未解决。

我们为 WordPress 使用 Docker > Traefik,并将 www 作为 WordPress 的首选版本。有多个 WP 实例。域是动态添加的。

但是,使用此配置,我收到了两次重定向,从 http 到 https 到 https www

http://example.com/
https://example.com/
https://www.example.com/

有什么办法可以减少重定向?

理想情况下 301 重定向
http://example.com directly to https://www.example.com 

Traefik 配置文件如下

defaultEntryPoints = ["http", "https"]

[web]
address = ":8080"

[entryPoints]

[entryPoints.http]
address = ":80"
[entryPoints.http.redirect]
entryPoint = "https"

[entryPoints.https]
address = ":443"
compress = true
[entryPoints.https.tls]

[acme]
email = "email@domain.com"
storage = "acme.json"
entryPoint = "https"
onDemand = false
OnHostRule = true


[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "traefik.example.com"
watch = true
exposedbydefault = false

最佳答案

尝试更换您的 [entryPoints.http.redirect]输入:

[entryPoints.http.redirect]
#entryPoint = "https"
regex = "^http:\/\/(www\.)*(example\.com)(.*)"
replacement = "https://www.$2$3"
permanent = true

Regex101

它不会处理 https://example.com/条目,因此您需要添加:
[entryPoints.https.redirect]
regex = "^https:\/\/(example\.com)(.*)"
replacement = "https://www.$1/$2"
permanent = true

如果你有多个frontedns,正则表达式会很难处理,所以你可以考虑使用a label on the container , 像这样:
traefik.frontend.headers.SSLRedirect=true
traefik.frontend.headers.SSLHost=www.example.com

从 1.7 开始,有新选项 SSLForceHost这甚至会强制重定向现有的 SSL 连接。
traefik.frontend.headers.SSLForceHost=true

关于https - traefik - HTTP 到 HTTPS WWW 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46085504/

相关文章:

tomcat - SEVERE : Failed to load keystore type JKS with path webapps/FT. keystore 由于签名溢出,TomCat 6 启动时字节数 = 128

c# - 使 wsHttpBinding 替代 netTcpBinding 的场景是什么

docker - 无法通过dockerized Traefik访问dockerized Neo4j Web界面

kubernetes - 使用 Traefik 和 Kubernetes 将流量路由到不同命名空间中的服务

javascript - JSF 2.0 - 在 URL 中包含带符号的脚本标记

django - Django 站点运行的默认 Web 服务器是什么?如何使我的 Django 站点启用 https?

web-services - 通过 HTTPS 发送的密码加密

kubernetes - Traefik Ku​​bernetes : Expose non Kubernetes service

docker - acme.json 的 traefik 权限 777 太开放,请使用 600

git - Gitlab运行程序克隆在traefik之后的超时(相对路径)