首页 文章

使用Docker标签时Traefik自定义错误页面

提问于
浏览
0

Traefik的更高版本支持自定义错误页面 - 如果出现错误,可以将请求发送到不同的后端 . 通常,当反向代理无法与应用程序服务器通信时,这是502 . This docs页面解释了如何使用 file 后端类型执行此操作,这非常有效 .

但是,我正在尝试使用Docker后端 . This docs页面显示了一个预期的Docker配置 - 虽然不清楚 <name> 替换应该是什么意思 .

因此,使用Docker Compose,我有一个基本设置,我想将所有HTTP 500-599流量定向到备用后端,称为 error . 这里's what I' ve进入 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"

正如您所看到的,我发布的端口和Traefik正在寻找的端口之间的 hello 容器故意不匹配 . 这是为了强制 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可以正常工作)

谢谢你的帮助!

1 回答

相关问题