问题
Later versions of Traefik support custom error pages – the ability to send a request to a different back end if there is an error. Commonly, this is a 502 when the reverse proxy can't communicate with a an application server. This docs page explains how to do this with the file
backend type, and this works well enough.
However, I'm trying to use the Docker backend. This docs page shows a prospective Docker configuration – though it's not clear what the <name>
substitution is supposed to mean.
So, using Docker Compose, I've got a basic setup where I want to direct all HTTP 500-599 traffic to an alternate backend, called error
. Here's what I've got in 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"
As you can see, I've got a deliberate mismatch on the hello
container between its published port and the port that Traefik is looking for. This is to force a 502 Bad Gateway
error.
My traefik.toml
file looks like this:
# 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
But, a request to http://localhost
still returns the generic Bad gateway
message, rather than redirecting to the /500s.html
served by the error
container (which works fine by going directly to http://error.localhost/500s.html)
Thank you anyone for your help!
回答1:
Error pages with labels are not available in 1.5: https://docs.traefik.io/v1.5/configuration/backends/docker/#on-containers
The support come in 1.6: https://docs.traefik.io/v1.6/configuration/backends/docker/#on-containers
来源:https://stackoverflow.com/questions/49673352/traefik-custom-error-pages-when-using-docker-labels