Traefik: Custom Error Pages and Returning 503 for Unknown Services
I've been wanting to customize the rather boring default "404 page not found" message that Traefik returns when a service is unknown/unavailable. Occasionally this error is returned when restarting the container running the service or if the container fails to start.
If you have customers visiting some of these services (like I do), then providing some more information on what to do is better than a short and cryptic error message.
5xx
class, except for the 500
, 501
, 503
, and 505
codes.For services that aren't running, the 503 status code is the most optimal, as search engines will see this as a temporary situation and won't impact your site ranking.
In your docker-compose.yaml
file, define a new container that will handle catchall requests and error pages. In this example, I've chosen to re-write the path to /
for catchall requests (the actual request URI path isn't important).
In the nginx.conf
file, we'll setup a basic server that simply returns static html files (you could also serve dynamically generated pages using e.g.: SSI, PHP, Python).
In the location = /
block that will be used for the catchall requests, we define a custom error page for the 503
status code and return a 503 Service Unavailable error that Traefik proxies to the user.
With this configuration, all that is left is to create custom error pages that match the status code, e.g. 404.html
and place them in the ./catchall/pages
directory.
Now, when you visit the URL of a service that hasn't fully started yet, your custom "catchall" error page will be displayed with a 503
status code.