【问题标题】:Redirect non-www to www with Traefik & Docker使用 Traefik 和 Docker 将非 www 重定向到 www
【发布时间】:2019-03-05 14:03:07
【问题描述】:

网上各个地方都贴了几个解决方案来解决我的问题,所以我在这里问一下是否有人能发现我的错误。

基于此评论 here,这应该将 www 指向非 www:

- traefik.frontend.rule=Host:example.com,www.example.com
- traefik.frontend.redirect.regex=^https?://www.example.com/(.*)
- traefik.frontend.redirect.replacement=https://example.com/$${1}

所以我只是翻转了行为来实现我的用例。请参阅下面的标签块。

但它不起作用。当前行为是:

http://example.com -> https://example.com(意外)

https://example.com -> https://example.com(意外)

http://www.example.com -> https://www.example.com (GTG)

https://www.example.com -> https://www.example.com (GTG

我希望一切都指向https://www.example.com

这是我的 docker-compose 文件:

version: "2"
services:

  app: 
    build:
      context: .
      dockerfile: Dockerfile-SSR-AngularApp
    environment: 
      - PORT=5200
    restart: always
    networks:
      - web
      - default
    expose:
      - "5200"
    labels:
      - traefik.enable=true
      - traefik.docker.network=web
      - traefik.basic.port=5200
      - traefik.basic.frontend.rule=Host:www.example.com,example.com
      - traefik.frontend.redirect.regex=^https?://example.com/?(.*) # I flipped these from the example to meet my use case
      - traefik.frontend.redirect.replacement=https://www.example.com$${1}
      - traefik.frontend.redirect.permanent=true
      - traefik.frontend.headers.SSLRedirect=true
      - traefik.frontend.headers.SSLForceHost=true
      - traefik.frontend.headers.SSLHost=www.example.com

networks:
  web:
    external: true

我的正则表达式看起来也不错,Regex101

关于为什么此配置无法按预期工作的任何想法?

更新:

这些是我从这些请求中得到的回复:

curl --head http://example.com
HTTP/1.1 302 Found -> This is the redirect from http to https
Location: https://example.com:443/
Date: Tue, 05 Mar 2019 14:30:29 GMT
Content-Length: 5
Content-Type: text/plain; charset=utf-8


curl --head https://example.com
HTTP/2 200 -> NO REDIRECT, should be 301
content-type: text/html; charset=utf-8
date: Tue, 05 Mar 2019 14:32:16 GMT
etag: W/"74c55-CIDAqU2YBPjLVCS1Hegttk4cLvI"
vary: Accept-Encoding
x-powered-by: Express
content-length: 478293

这是我的 traefik.toml 文件:

debug = false

logLevel = "ERROR"
defaultEntryPoints = ["https","http"]

[entryPoints]
[entryPoints.http]
address = ":80"
    [entryPoints.http.redirect]
    entryPoint = "https"
[entryPoints.https]
address = ":443"
compress = true
[entryPoints.https.tls]

[retry]

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

[acme]
email = "me@example.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
[acme.httpChallenge]
entryPoint = "http"

这是我最终得到的解决方案

debug = false

logLevel = "ERROR"
defaultEntryPoints = ["https","http"]

[entryPoints]
[entryPoints.http]
address = ":80"
    [entryPoints.http.redirect]
      #entryPoint = "https"
      regex = "^http://example.com/(.*)"
      replacement = "https://www.example.com/$1"
[entryPoints.https]
address = ":443"
compress = true
    [entryPoints.https.redirect]
      regex = "^https://example.com/(.*)"
      replacement = "https://www.example.com/$1"
    [entryPoints.https.tls]

[retry]

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

[acme]
email = "me@example.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
[acme.httpChallenge]
entryPoint = "http"

【问题讨论】:

  • @Idez,你能看看我的问题吗?任何方向将不胜感激。

标签: docker docker-compose traefik


【解决方案1】:

在应用你的正则表达式之前,设置traefik.frontend.headers.SSLRedirect=false 或Traefik 将为所有http 请求添加一个重定向规则到与https 相同的请求。

来自Traefik docker documentation

traefik.frontend.headers.SSLRedirect=true 强制前端 如果发送非 SSL 请求,则重定向到 SSL。

【讨论】:

  • 感谢您的帮助!我设置了 SSLRedirect=false,但是,它的行为仍然相同。
【解决方案2】:

对于我的解决方案,我最终放弃了 docker-compose 文件中 labels 块中的方法。如果有人为此提出解决方案,我很感兴趣。然而,与此同时,我想出了一个适用于我的简单案例的解决方案。我没有在容器上执行重定向,而是在我的 traefik.toml 配置文件中的 Traefik 入口点执行它:

debug = false

logLevel = "ERROR"
defaultEntryPoints = ["https","http"]

[entryPoints]
[entryPoints.http]
address = ":80"
    [entryPoints.http.redirect]
      #entryPoint = "https"
      regex = "^http://example.com/(.*)"
      replacement = "https://www.example.com/$1"
[entryPoints.https]
address = ":443"
compress = true
    [entryPoints.https.redirect]
      regex = "^https://example.com/(.*)"
      replacement = "https://www.example.com/$1"
    [entryPoints.https.tls]

[retry]

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

[acme]
email = "me@example.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
[acme.httpChallenge]
entryPoint = "http"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 2011-01-05
    • 2012-10-19
    相关资源
    最近更新 更多