HAProxy – How to Use Custom 503 Error Document

haproxyhttp-status-code

I would like haproxy to use its own 503 document page when back server (backend) sends HTTP 503 code. Is it possible?

Have seen something like "monitor fail" conditions but don't know how to add it to the frontend.

Best Answer

You can use the errorfile directive and then a custom .http text file. So for example:

errorfile 503 /etc/haproxy/errors/503-mycustom.http

Content of the file would then be something like:

HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html> 
  <head>
    <title>RARRR!!!!!</title>
  </head> 
  <body style="font-family:Arial,Helvetica,sans-serif;">
    <div style="margin: 0 auto; width: 960px;"> 
          <h2 >RAWR RAWR RAWR</h2>
    </div>
  </body> 
</html>

The errorfile directive can be specific to a backend as well.

The "errorfile" setting cannot be used to change a response sent by HAProxy if nodes are online. This setting only affects HAProxy when all nodes are offline.

It is important to understand that this keyword is not meant to rewrite errors returned by the server, but errors detected and returned by HAProxy. This is why the list of supported errors is limited to a small set.