Alerts and monitoring
Error pages
Available since
- HAProxy 2.2
- HAProxy Enterprise 2.2r1
- HAProxy ALOHA 12.5
You can customize the HTTP error pages clients receive to add your own styling, text, and images. Two scenarios exist:
- Customize HTTP errors generated by the load balancer
- Customize HTTP errors received from the backend web servers
Customize load balancer errors Jump to heading
The http-errors
section locates custom HTML pages to return to clients when errors occur. By default, the load balancer will serve these pages only if it initiated the error itself. For example, it will return the page for a 503 Service Unavailable
error if it can’t reach any backend servers.
To define custom error pages:
-
Create a file with a
.http
file extension and add HTTP metadata at the top of the file. The metadata should include the HTTP start line and headers. In the following example, we define a custom403
error page, and save it as403.http
:403.httphtmlHTTP/1.1 403 ForbiddenCache-Control: no-cacheConnection: closeContent-Type: text/html<!DOCTYPE html><html><body><h1>403 Forbidden</h1><p>Sorry, but you are not authorized to view this page.</p></body></html>403.httphtmlHTTP/1.1 403 ForbiddenCache-Control: no-cacheConnection: closeContent-Type: text/html<!DOCTYPE html><html><body><h1>403 Forbidden</h1><p>Sorry, but you are not authorized to view this page.</p></body></html> -
In your load balancer configuration, add an
http-errors
section with one or moreerrorfile
directives, where each specifies an HTTP response status code and the path to an error file. In the following example, we define one custom error page to use for403 Forbidden
errors:haproxyhttp-errors myerrorserrorfile 403 /403.httphaproxyhttp-errors myerrorserrorfile 403 /403.httpYou can define different error page files for different response status code:
haproxyhttp-errors myerrorserrorfile 403 /403.httperrorfile 404 /404.httperrorfile 500 /500.httphaproxyhttp-errors myerrorserrorfile 403 /403.httperrorfile 404 /404.httperrorfile 500 /500.http -
To use custom error pages, reference the name of the
http-errors
section by using anerrorfiles
directive in yourfrontend
:haproxyfrontend wwwbind :80default_backend webserverserrorfiles myerrorshaproxyfrontend wwwbind :80default_backend webserverserrorfiles myerrors
Customize web server errors Jump to heading
You can intercept error responses from backend web servers and replace them with your custom error pages.
-
In your load balancer configuration, add an
http-errors
section with one or moreerrorfile
directives, where each specifies an HTTP response status code and the path to an error file. In the following example, we define one custom error page to use for404 Not Found
errors:haproxyhttp-errors myerrorserrorfile 404 /404.httphaproxyhttp-errors myerrorserrorfile 404 /404.http -
In your
frontend
, add anerrorfiles
directive that refers to yourhttp-errors
section. Also, add anhttp-response return
directive to intercept any response from the server that has a 404 status. It then, by specifyingdefault-errorfiles
, returns the 404errorfile
that was defined in themyerrors
section:haproxyfrontend wwwbind :80default_backend webserverserrorfiles myerrorshttp-response return status 404 default-errorfiles if { status 404 }haproxyfrontend wwwbind :80default_backend webserverserrorfiles myerrorshttp-response return status 404 default-errorfiles if { status 404 } -
Optional: The
default-errorfiles
argument instructs the load balancer to use the error files specified by theerrorfiles
directive within the frontend. Alternatively, you can indicate a different set of error files to use by setting theerrorfiles
argument on thehttp-response return
line. In the following example, we use the 404 page from thealternative_errors
if the status is 404 and theHost
header isexample.com
:haproxyhttp-errors myerrorserrorfile 403 /403.httperrorfile 404 /404.httperrorfile 500 /500.httphttp-errors alternative_errorserrorfile 403 /403-alt.httperrorfile 404 /404-alt.httperrorfile 500 /500-alt.httpfrontend wwwbind :80default_backend webservershttp-request set-var(txn.host) req.hdr(host)errorfiles myerrorshttp-response return status 404 errorfiles alternative_myerrors if { status 404 } { var(txn.host) example.com }http-response return status 404 default-errorfiles if { status 404 } !{ var(txn.host) example.com }haproxyhttp-errors myerrorserrorfile 403 /403.httperrorfile 404 /404.httperrorfile 500 /500.httphttp-errors alternative_errorserrorfile 403 /403-alt.httperrorfile 404 /404-alt.httperrorfile 500 /500-alt.httpfrontend wwwbind :80default_backend webservershttp-request set-var(txn.host) req.hdr(host)errorfiles myerrorshttp-response return status 404 errorfiles alternative_myerrors if { status 404 } { var(txn.host) example.com }http-response return status 404 default-errorfiles if { status 404 } !{ var(txn.host) example.com }
See also Jump to heading
Do you have any suggestions on how we can improve the content of this page?