我有一个问题,我可以路由HTTPS流量,但是无法将HTTP流量全局重定向到HTTPS。就我而言,我只需要HTTPS流量,因此我想重定向所有传入的流量。
目前,当我尝试通过HTTP提供URL时,出现404错误。
我已经在Treafik中启用了DEBUG日志,但是在日志中看不到任何问题或不正常的内容。
另外,我在Stackoverflow上看到了一个非常相似的主题,但是我们发现,他的错误与我的错误有所不同:How to redirect http to https with Traefik 2.0 and Docker Compose labels?
以下设置基于此处的博客条目:https://blog.containo.us/traefik-2-0-docker-101-fc2893944b9d
我的设置
我在集群中这样配置了Traefik:
global:
checkNewVersion: false
sendAnonymousUsage: false
api:
dashboard: true
entryPoints:
web:
address: :80
websecure:
address: :443
providers:
providersThrottleDuration: 2s
docker:
watch: true
endpoint: unix:///var/run/docker.sock
swarmMode: true
swarmModeRefreshSeconds: 15s
exposedByDefault: false
network: webgateway
log:
level: DEBUG
accessLog: {}
certificatesResolvers:
default:
acme:
email: {email}
storage: /etc/traefik/acme/acme.json
httpChallenge:
entryPoint: web
并使用以下docker-compose文件启动Traefik
version: '3'
services:
proxy:
image: traefik:latest
ports:
- "80:80"
- "443:443"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /data/docker_data/traefik/traefik-2.yml:/etc/traefik/traefik.yml
- /data/docker_data/traefik/acme-2.json:/etc/traefik/acme/acme.json
labels:
# redirect
- "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
- "traefik.http.routers.redirs.rule=hostregexp(`{host:.+}`)"
- "traefik.http.routers.redirs.entrypoints=web"
- "traefik.http.routers.redirs.middlewares=redirect-to-https"
我的服务配置有以下标签:
traefik.http.routers.myapp.rule=Host(`myapp.ch`)
traefik.http.routers.myapp.service=myapp
traefik.http.routers.myapp.entrypoints=websecure
# I don't think that the following one is required here...
# traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https
traefik.http.routers.myapp.tls.certresolver=default
traefik.http.services.myapp.loadbalancer.server.port=3000
traefik.http.routers.myapp.tls=true
traefik.enable=true
任何想法为什么这不起作用?
最佳答案
您不需要配置Traefik服务本身。在Traefik上,您只需要具有:443(websecure)和:80(web)的入口点
因为Traefik仅充当入口点,并且不执行重定向,所以目标服务上的中间件将执行此操作。
现在,将目标服务配置如下:
version: '2'
services:
mywebserver:
image: 'httpd:alpine'
container_name: mywebserver
labels:
- traefik.enable=true
- traefik.http.middlewares.mywebserver-redirect-websecure.redirectscheme.scheme=https
- traefik.http.routers.mywebserver-web.middlewares=mywebserver-redirect-websecure
- traefik.http.routers.mywebserver-web.rule=Host(`sub.domain.com`)
- traefik.http.routers.mywebserver-web.entrypoints=web
- traefik.http.routers.mywebserver-websecure.rule=Host(`sub.domain.com`)
- traefik.http.routers.mywebserver-websecure.tls.certresolver=mytlschallenge
- traefik.http.routers.mywebserver-websecure.tls=true
- traefik.http.routers.mywebserver-websecure.entrypoints=websecure
# if you have multiple ports exposed on the service, specify port in the websecure service
- traefik.http.services.mywebserver-websecure.loadbalancer.server.port=9000
所以基本上流程是这样的:
请求:http://sub.domain.com:80-> traefik(服务)-> mywebserver-web(路由器,http规则)-> mywebserver-redirect-websecure(中间件,重定向到https)-> mywebserver-websecure(路由器,https规则)- -> mywebserver(服务)
关于docker - Traefik v2 : 404 while routing HTTP traffic globally to HTTPS,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58666711/