gpt4 book ai didi

docker - Traefik v2 : 404 while routing HTTP traffic globally to HTTPS

转载 作者:行者123 更新时间:2023-12-02 05:20:45 26 4
gpt4 key购买 nike

我遇到的问题是,我可以路由 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com