Alerts and monitoring
Error pages
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 itself
- Customize HTTP errors received from the backend web servers
Customize load balancer errors Jump to heading
This section applies to:
- HAProxy 2.1 and newer
- HAProxy Enterprise 2.1r1 and newer
- HAProxy ALOHA 12.0 and newer
When customizing load balancer errors, you can create multiple groups of error files defined in an http-errors section that can be loaded per proxy / per site. You can also render dynamic content on these pages using template files.
The load balancer supports the following HTTP status code for customization: 200, 400, 401, 403, 404, 405, 407, 408, 410, 413, 425, 429, 500, 501, 502, 503, and 504.
About making changes
The load balancer reads and stores in memory custom error files at the same time it reads the load balancer configuration. If you make changes to the files while the load balancer is running, it won’t automatically apply the changes. You must reload the load balancer configuration to pick up new changes to your custom error files.
Also, you must give the absolute path when defining the location of your custom error files. The load balancer reads these files before it performs a change root or chroot.
Declare groups of HTTP 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
.httpfile 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 custom403error 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 aren't 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 aren't authorized to view this page.</p></body></html>Note that while this file contains HTML file, it is a
.httpfile instead of an.htmlfile, as it contains both HTML content and HTTP metadata. -
In your load balancer configuration, add an
http-errorssection with one or moreerrorfiledirectives, 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 Forbiddenerrors:haproxyhttp-errors myerrorserrorfile 403 /403.httphaproxyhttp-errors myerrorserrorfile 403 /403.httpYou can define different error page files for different response status codes:
haproxyhttp-errors myerrorserrorfile 403 /etc/haproxy/errorfiles/403.httperrorfile 404 /etc/haproxy/errorfiles/404.httperrorfile 500 /etc/haproxy/errorfiles/500.httphaproxyhttp-errors myerrorserrorfile 403 /etc/haproxy/errorfiles/403.httperrorfile 404 /etc/haproxy/errorfiles/404.httperrorfile 500 /etc/haproxy/errorfiles/500.http -
To use custom error pages, reference the name of the
http-errorssection by using anerrorfilesdirective in yourfrontend:haproxyfrontend wwwbind :80default_backend webserverserrorfiles myerrorshaproxyfrontend wwwbind :80default_backend webserverserrorfiles myerrors
Using an older version of the load balancer?
Support for the http-errors directive, useful for declaring multiple groups of HTTP errors which can be used in separate proxy sections, that is, frontends, backends, and so on, was added in version 2.1. Older versions can use the explicit errorfile directive within proxies instead of declaring the HTTP error files within an http-errors section. For versions 2.0 and earlier, place the errorfile directives directly in the frontend definition:
haproxyfrontend wwwbind :80default_backend webserverserrorfile 403 /etc/haproxy/errorfiles/403.httperrorfile 404 /etc/haproxy/errorfiles/404.httperrorfile 500 /etc/haproxy/errorfiles/500.http
haproxyfrontend wwwbind :80default_backend webserverserrorfile 403 /etc/haproxy/errorfiles/403.httperrorfile 404 /etc/haproxy/errorfiles/404.httperrorfile 500 /etc/haproxy/errorfiles/500.http
Use an error template file Jump to heading
This section applies to:
- HAProxy 2.2 and newer
- HAProxy Enterprise 2.2r1 and newer
- HAProxy ALOHA 12.5 and newer
You can use the http-error status directive to set template files for load-balancer-generated errors using its lf-file parameter. Consider the following example:
-
We create a
.httperror template file named504.httpto cover the case where the load balancer timed out waiting to receive a response from a server. Because it is a template file, it can render load balancer variables and fetch methods:504.htmlhtmlHTTP/1.1 504 Gateway TimeoutCache-Control: no-cacheConnection: closeContent-Type: text/html<!DOCTYPE html><html lang="en"><head><meta charset="utf-8" /><title>504 Gateway Timeout</title></head><body><main><h1>504 Gateway Timeout</h1>The server that timed out is : %[srv_name]</main></body></html>504.htmlhtmlHTTP/1.1 504 Gateway TimeoutCache-Control: no-cacheConnection: closeContent-Type: text/html<!DOCTYPE html><html lang="en"><head><meta charset="utf-8" /><title>504 Gateway Timeout</title></head><body><main><h1>504 Gateway Timeout</h1>The server that timed out is : %[srv_name]</main></body></html>We define in the file:
- HTTP metadata at the top of the file for the
504 Gateway Timeout. - The HTML page data to return upon receiving a
504 Gateway Timeouterror.- The page data includes a reference to the sample fetch
srv_nameto help identify the potentially troublesome server.
- The page data includes a reference to the sample fetch
- HTTP metadata at the top of the file for the
-
We reference this template file in our frontend. The syntax for this directive is:
http-error status <code> content-type <content-type> lf-file <.http custom error file path>.
haproxyfrontend wwwbind :80default_backend web_servershttp-error status 504 content-type "text/html; charset=utf-8" lf-file /etc/haproxy/errorfiles/504.httpbackend web_serversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkserver s3 192.168.1.27:80 checkhaproxyfrontend wwwbind :80default_backend web_servershttp-error status 504 content-type "text/html; charset=utf-8" lf-file /etc/haproxy/errorfiles/504.httpbackend web_serversmode httpbalance roundrobinserver s1 192.168.1.25:80 checkserver s2 192.168.1.26:80 checkserver s3 192.168.1.27:80 check- You must set the
content-typeof the file; in this case it istext/html; charset=utf-8. - Note that we use
lf-fileinstead offile, as we want the load balancer to be able to render the content that contains the sample fetch. When you usefile, the content is treated as a raw string and is rendered as such.
The error page returned will name the server that timed out. For example:
text504 Gateway TimeoutThe server that timed out is : s1
text504 Gateway TimeoutThe server that timed out is : s1
http-error status and errorfile
If you specify the errorfile or errorfiles arguments on the http-error status line, you don’t need to set the content-type. You also don’t need to set content-type if you are returning an empty payload as your error page.
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-errorssection with one or moreerrorfiledirectives, 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 Founderrors:haproxyhttp-errors myerrorserrorfile 404 /404.httphaproxyhttp-errors myerrorserrorfile 404 /404.http -
In your
frontend, add anerrorfilesdirective that refers to yourhttp-errorssection. Also, add anhttp-response returndirective to intercept any response from the server that has a 404 status. It then, by specifyingdefault-errorfiles, returns the 404errorfilethat was defined in themyerrorssection: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-errorfilesargument instructs the load balancer to use the error files specified by theerrorfilesdirective within the frontend. Alternatively, you can indicate a different set of error files to use by setting theerrorfilesargument on thehttp-response returnline. In the following example, we use the 404 page from thealternative_errorsif the status is 404 and theHostheader isexample.com:haproxyhttp-errors myerrorserrorfile 403 /etc/haproxy/errorfiles/403.httperrorfile 404 /etc/haproxy/errorfiles/404.httperrorfile 500 /etc/haproxy/errorfiles/500.httphttp-errors alternative_errorserrorfile 403 /etc/haproxy/errorfiles/403-alt.httperrorfile 404 /etc/haproxy/errorfiles/404-alt.httperrorfile 500 /etc/haproxy/errorfiles/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 /etc/haproxy/errorfiles/403.httperrorfile 404 /etc/haproxy/errorfiles/404.httperrorfile 500 /etc/haproxy/errorfiles/500.httphttp-errors alternative_errorserrorfile 403 /etc/haproxy/errorfiles/403-alt.httperrorfile 404 /etc/haproxy/errorfiles/404-alt.httperrorfile 500 /etc/haproxy/errorfiles/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 }default-errorfiles in a backend
The preceding examples use
default-errorfilesin a frontend. If you usedefault-errorfilesin a backend, the error message that the load balancer returns is the error message you have defined in the frontend instead of the error message the backend received from the server.
See also Jump to heading
For complete information on the directives used in defining error pages, see these topics in the HAProxy Configuration Manual:
- To import error files, see errorfiles reference.
- To define a custom error message, see http-error status reference.
- To define HTTP error files, see HTTP-errors reference.
- To fetch a server’s name for use in an error file, see srv_name reference.
Do you have any suggestions on how we can improve the content of this page?