Custom 503 Error Page in HAProxy: Setup Guide
In this article
When a backend server goes down or is in maintenance, HAProxy returns a default 503 Service Unavailable page. This generic page is often ugly and unhelpful for users. By setting a custom 503 error page, you can display a branded, user-friendly message that explains the situation and provides next steps. This guide walks you through the process, from creating the HTML file to configuring HAProxy, and covers common pitfalls and troubleshooting.
What You'll Need
- A server or VM with HAProxy installed (version 1.5 or later recommended).
- Root or sudo access to edit HAProxy configuration files.
- A text editor (e.g., nano, vim) to create the error page file.
- Basic understanding of HAProxy configuration syntax (frontend, backend, defaults).
Step-by-Step
- Create the custom error page HTML file. Write a clean, self-contained HTML page. Keep it small (under 32KB) and avoid external resources (CSS, images) that might not load when the backend is down. Save it, for example, as
/etc/haproxy/errors/503.http. Here's a minimal example:<!DOCTYPE html> <html> <head><title>Service Unavailable</title></head> <body> <h1>We'll Be Right Back</h1> <p>Our servers are temporarily unavailable. Please try again in a few minutes.</p> </body> </html>
- Decide where to apply the error page. You can set it globally (affects all frontends), per-frontend, or per-backend. For most cases, setting it in the frontend is sufficient. If you have multiple frontends with different needs, set it individually.
- Edit the HAProxy configuration. Open the main config file, usually
/etc/haproxy/haproxy.cfg, with sudo. - Add the
errorfiledirective. In thefrontendsection (ordefaultsfor global), add a line like:errorfile 503 /etc/haproxy/errors/503.http
This tells HAProxy to serve the specified file whenever it returns a 503 status code. - Optional: Set a custom error for other codes. You can also define pages for 400, 403, 408, 500, 502, 504, etc., using the same directive with different codes.
- Check the syntax. Run
haproxy -c -f /etc/haproxy/haproxy.cfgto validate the configuration. Fix any errors reported. - Reload HAProxy. Apply the changes without dropping connections using
sudo systemctl reload haproxy(orsudo service haproxy reloadon older systems). - Test the error page. Temporarily stop a backend server or use a maintenance mode to trigger a 503. Visit your site and confirm the custom page appears.
Common Mistakes
- Incorrect file path: HAProxy runs in a chroot environment on some systems. If you use a chroot, the path inside the config must be relative to the chroot jail (e.g.,
/etc/haproxymight become/). Test withhaproxy -cto catch this. - File permissions: HAProxy must be able to read the error file. Ensure the file is owned by the HAProxy user (usually
haproxy) or has world-readable permissions (chmod 644). - Forgetting to reload: Editing the config doesn't apply changes until you reload or restart HAProxy.
- Using external resources: Linking to external CSS/JS/images in your error page can break when the backend is down, resulting in a broken-looking page.
- Placing the directive in the wrong section:
errorfilein abackendonly applies when that backend is used. For a global effect, put it indefaultsor eachfrontend.
Tips
- Keep it simple: Use inline CSS and base64-encoded images if you need visuals. Avoid external requests.
- Add a retry hint: Include a meta refresh or a JavaScript redirect to retry after a few seconds, improving user experience.
- Use a maintenance mode: Instead of stopping the backend, you can use HAProxy's
http-request denyor a specific ACL to return 503 without taking the server down. - Test with curl: Use
curl -I http://yourdomain.comto check the status code and headers. To see the body, usecurl -i. - Log the errors: Ensure HAProxy logs 503 responses so you can monitor downtime.
Troubleshooting
- Custom page not showing: Check that
errorfileis in the correct section. Verify the file path and permissions. Runhaproxy -cto check for config errors. Reload HAProxy after changes. - Default page still appears: This often means the directive isn't being read. Check for typos in the configuration. Also, if you have multiple
frontendsections, ensure you added the directive to the one handling your traffic. - HAProxy fails to start: The error file might be unreadable or the path is wrong. Use
haproxy -cto see the exact error. Adjust permissions or path accordingly. - Error page loads but looks broken: Remove external references. If you use inline CSS, ensure it's complete. Test the HTML file directly in a browser.
- Changes not applying: Some systems require a full restart instead of reload. Try
sudo systemctl restart haproxy(be aware this drops connections).
Frequently Asked Questions
Can I use a different error code page?
Yes, HAProxy supports errorfile for many HTTP error codes (400, 403, 404, 408, 500, 502, 503, 504, etc.). Simply specify the code and the file path.
Does the error page work for HTTPS?
Yes, it works for both HTTP and HTTPS frontends. The error page is served by HAProxy itself, before any backend connection.
Can I use a dynamic error page (e.g., PHP)?
No, errorfile serves static content. For dynamic responses, you'd need to use http-request return with a custom status and body, but that's more complex.
How do I set a maintenance mode without stopping the backend?
You can use an ACL to match a specific header or IP and then return 503 with http-request deny deny_status 503. This allows you to keep the backend running but show the maintenance page to select users.
Does the error page affect SEO?
Returning 503 with a custom page is actually good for SEO during brief outages, as it tells search engines the site is temporarily unavailable. Ensure the page includes a Retry-After header if you can set it.
Can I have different error pages for different frontends?
Yes, place the errorfile directive inside each frontend block. This is useful if you have multiple domains with different branding.