Skip to content

Instantly share code, notes, and snippets.

@haproxytechblog
Last active April 3, 2023 14:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haproxytechblog/8bbd22b00244b5d257fe0cb5ffc0c896 to your computer and use it in GitHub Desktop.
Save haproxytechblog/8bbd22b00244b5d257fe0cb5ffc0c896 to your computer and use it in GitHub Desktop.
Serve Dynamic Custom Error Pages with HAProxy
HTTP/1.1 404 Not Found
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>404 Not Found</title>
</head>
<body>
<main>
<h1>404 Not Found</h1>
This is my custom 404 Not Found page!
</main>
</body>
</html>
http-errors myerrors
errorfile 404 /etc/haproxy/errors/404.http
http-errors myerrors
errorfile 400 /etc/haproxy/errors/400.http
errorfile 401 /etc/haproxy/errors/401.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 404 /etc/haproxy/errors/404.http
http-errors myerrors
errorfile 404 /etc/haproxy/errors/404.http
frontend site1
bind :80
default_backend webservers
errorfiles myerrors
http-response return status 404 default-errorfiles if { status 404 }
http-errors site1
errorfile 404 /etc/haproxy/errors/site1-404.http
http-errors site2
errorfile 404 /etc/haproxy/errors/site2-404.http
frontend allsites
bind :80
default_backend site1-servers
use_backend site2-servers if { req.hdr(host) site2.com }
# Store host header in variable
http-request set-var(txn.host) req.hdr(host)
# Use site1 error page if site1.com
http-response return status 404 errorfiles site1 if { status 404 } { var(txn.host) -m str site1.com }
# Use site2 error page if site2.com
http-response return status 404 errorfiles site2 if { status 404 } { var(txn.host) -m str site2.com }
frontend site1
bind :80
default_backend webservers
errorfiles myerrors
unique-id-format %{+X}o\ %ci:%cp_%fi:%fp_%Ts_%rt:%pid
unique-id-header X-Unique-ID
log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r %[unique-id]"
http-response return status 404 content-type "text/html; charset=utf-8" lf-file /etc/haproxy/errors/404.html if { status 404 }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>404 Not Found</title>
</head>
<body>
<main>
<h1>404 Not Found</h1>
<p>This is my custom 404 Not Found page!</p>
<p>Unique ID: %[unique-id]</p>
</main>
</body>
</html>
HTTP/1.1 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: application/json
{ "errors" : [ { "status" : "503", "title" : "Service unavailable", "detail" : "No server is available to handle this request." } ] }
http-errors json
errorfile 404 /etc/haproxy/errors/404-json.http
errorfile 503 /etc/haproxy/errors/503-json.http
frontend api
bind :8080
default_backend apiservers
errorfiles json
http-response return status 404 default-errorfiles if { status 404 }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Site Undergoing Maintenance</title>
</head>
<body>
The site is undergoing scheduled maintenance.
</body>
</html>
frontend site1
bind :80
default_backend webservers
http-request return status 200 content-type text/html file /etc/haproxy/errors/maintenance.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment