【发布时间】:2018-04-05 13:15:49
【问题描述】:
更高版本的 Traefik 支持自定义错误页面——如果出现错误,可以将请求发送到不同的后端。通常,当反向代理无法与应用程序服务器通信时,这是一个 502。 This 文档页面解释了如何使用 file 后端类型执行此操作,并且效果很好。
但是,我正在尝试使用 Docker 后端。 This docs 页面显示了一个预期的 Docker 配置 - 尽管不清楚 <name> 替换应该是什么意思。
所以,使用 Docker Compose,我有一个基本设置,我想将所有 HTTP 500-599 流量定向到另一个后端,称为 error。这是我在docker-compose.yml 中得到的:
version: '2'
services:
traefik:
image: traefik
ports:
- "80:80"
- "8080:8080"
restart: always
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./acme:/etc/traefik/acme/
- ./traefik.toml:/etc/traefik/traefik.toml
hello:
image: kitematic/hello-world-nginx
ports:
- "80"
labels:
- traefik.backend=hello
- traefik.port=801
- traefik.frontend.rule=Host:localhost
- traefik.enable=true
- traefik.frontend.errors.network.status=["500-599"]
- traefik.frontend.errors.network.backend="error"
- traefik.frontend.errors.network.query="/500s.html"
error:
image: kitematic/hello-world-nginx
ports:
- "80"
volumes:
- ./html:/website_files
labels:
- "traefik.backend=error"
- "traefik.port=80"
- "traefik.frontend.rule=Host:error.localhost"
- "traefik.enable=true"
如您所见,hello 容器的发布端口与 Traefik 正在寻找的端口之间存在故意不匹配的情况。这是为了强制出现502 Bad Gateway 错误。
我的traefik.toml 文件如下所示:
# defaultEntryPoints must be at the top because it should not be in any table below
defaultEntryPoints = ["http"]
# [accessLog]
logLevel = "DEBUG"
[web]
# Port for the status page
address = ":8080"
# Entrypoints, http and https
[entryPoints]
[entryPoints.http]
address = ":80"
[docker]
endpoint = "unix:///var/run/docker.sock"
domain = "localhost"
watch = true
exposedbydefault = true
# swarmmode = true
但是,对http://localhost 的请求仍然返回通用的Bad gateway 消息,而不是重定向到error 容器提供的/500s.html(直接转到http://error.localhost/500s.html 可以正常工作)
感谢大家的帮助!
【问题讨论】: