【发布时间】: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