【问题标题】:traefik - HTTP to HTTPS WWW Redirecttraefik - HTTP 到 HTTPS WWW 重定向
【发布时间】:2017-09-06 23:01:59
【问题描述】:

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

一直在寻找解决方案,但还没有解决。

我们将 Docker > Traefik 用于 WordPress,并将 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

【问题讨论】:

标签: https traefik


【解决方案1】:

尝试将您的 [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

【讨论】:

  • SSLRedirect 标签无法处理https://example.com => https://www.example.com 的情况。知道如何做到这一点吗?
  • 与我在标签部分之前的部分中描述的方式相同 - 使用 [entryPoints.https.redirect]
  • 这仅适用于入口点(即,适用于所有前端)。我想知道如何在每个前端的基础上完成。
  • 由于没有任何现成的标签可以满足您的需求,您要么必须进行自定义配置,要么替换使用现有标签的模板文件。在this answer 我有更多信息如何正确地做到这一点。
  • 我最终只是通过我的网络服务器使用了重定向。
【解决方案2】:

这是我必须做的。上面的答案很有帮助,但是 traefik 不会启动,因为您实际上需要双 \ 才能在 .toml 中转义。

此外,您还需要确保那里有正常的入口点和端口。 这是我完整的 entryPoints 部分:

[entryPoints]
  [entryPoints.http]
    address = ":80"
  [entryPoints.https]
    address = ":443"
  [entryPoints.http.redirect]
    regex = "^http:\\/\\/(www.)*(example\\.com)(.*)"
    replacement = "https://www.$2/$3"
    permanent = true
  [entryPoints.https.redirect]
    regex = "^https:\\/\\/(example.com)(.*)"
    replacement = "https://www.$1/$2"
    permanent = true
[entryPoints.https.tls]

【讨论】:

    【解决方案3】:

    这就是我让它与 AWS ELB 背后的 docker 提供商合作的方式。

    traefik 容器

    /usr/bin/docker run --rm \
      --name traefik \
      -p 5080:80 \
      -p 5443:443 \
      -v /etc/traefik/traefik.toml:/etc/traefik/traefik.toml \
      -v /var/run/docker.sock:/var/run/docker.sock \
      traefik
    

    traefik.toml

    defaultEntryPoints = ["http", "https"]
    
    [entryPoints]
      [entryPoints.http]
        address = ":80"
    
      [entryPoints.https]
        address = ":443"
    

    码头工人标签

      -l traefik.enable=true \
      -l traefik.http.middlewares.redirect.redirectregex.regex="^http://(.*)" \
      -l traefik.http.middlewares.redirect.redirectregex.replacement="https://\$1" \
      -l traefik.http.routers.web-redirect.rule="Host(\`domain.com\`)" \
      -l traefik.http.routers.web-redirect.entrypoints="http" \
      -l traefik.http.routers.web-redirect.middlewares="redirect" \
      -l traefik.http.routers.web-secure.rule="Host(\`domain.com\`)" \
      -l traefik.http.routers.web-secure.entrypoints="https" \
    

    ELB 监听器

    【讨论】:

    • 看到你的回答,我想你指的是 Traefik 2.0。我认为 OP 在使用 1.7 或更低版本时问了他的问题。编辑:他在 2 年前没有发布 2.0 时问过这个问题。也许,但我不知道,您的回答可以帮助功能请求。 :)
    • 他们也错过了问题的www 部分。
    猜你喜欢
    • 1970-01-01
    • 2015-10-03
    • 2018-09-26
    • 2014-05-08
    • 2016-01-20
    • 1970-01-01
    • 2021-07-20
    • 2016-02-28
    • 2015-10-23
    相关资源
    最近更新 更多