【问题标题】:Traefik custom error pages when using Docker labels [closed]使用 Docker 标签时的 Traefik 自定义错误页面 [关闭]
【发布时间】: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 可以正常工作)

感谢大家的帮助!

【问题讨论】:

    标签: docker traefik


    【解决方案1】:

    【讨论】:

    • 这绝对是@Idez 问题的一部分。所以,我已经过去了,还发现标签需要是errors.networks,而不是errors.network。
    • 但是......现在它告诉我Error Page is configured for Frontend frontend-Host-localhost-0, but either Backend error is not set or Backend URL is missing——尽管我有一个名为error的后端。还有什么想法吗?
    • 检查您的 traefik 监控 gui - 它将列出后端生成的完整名称以查找错误。比如我有dev-env_error,但是改成了dev-env-error
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 2015-06-22
    • 2020-07-19
    • 2012-12-04
    相关资源
    最近更新 更多