Catchall router that returns 503 Service Unavailable response code

Hello everybody,

This topic exists to provide an example for a catchall non-TLS router that will make unmatched requests answered by a 503 Service Unavailable response code. It could be adapted as a TLS router for HTTPS.

The example below is only a file provider version (yaml) of the configuration, thus if anyone wants to adapt it for other providers, it will be gladly welcomed.

Static configuration (traefik.yaml):

entrypoints:
  web:
    address: :80

providers:
  file:
    filename: dynamic.yaml

Dynamic configuration (dynamic.yaml):

http:
  routers:
    catchall:
      # attached only to web entryPoint
      entryPoints:
        - "web"
      # catchall rule
      rule: "PathPrefix(`/`)"
      service: unavailable
      # lowest possible priority
      # evaluated when no other router is matched
      priority: 1

  services:
    # Service that will always answer a 503 Service Unavailable response
    unavailable:
      loadBalancer:
        servers: {}

4 Likes

Hey @rtribotte ,

Does it work with docker swarm provider?

Ok, this is the solution I found for docker swarm provider:

I've just added these lines:

traefik:
    image: traefik:v2.4
    ...
    deploy:
      labels:
        - "traefik.enable=true"
        - "traefik.http.routers.traefik-502.entrypoints=http"
        - "traefik.http.routers.traefik-502.rule=PathPrefix(`/`)"
        - "traefik.http.routers.traefik-502.priority=1"
        - "traefik.http.services.traefik-502.loadbalancer.server.port=0"

With the above code, any not registered rule or with empty backends (without up replicas) will throw 502 instead of 404

PS: I did not define any service for that, just mapping the same traefik service to an invalid port

Full script here: Traefik fix 502 for empty backends · GitHub

1 Like