6

Is there an easy way to dissable All backends in haproxy, and instead serve an appropriate maintenance page (for http requests)?

I've read a little about the dissabled option, which I understand is per-server, but am wondering if there is a way to simply stop traffic to all backends?

UpTheCreek
  • 1,628
  • 10
  • 32
  • 48

4 Answers4

4

The backup keyword is what we use for this. See this example:

listen example_com 0.0.0.0:8001
...
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www.example.com\r\nUser-Agent:\ HAProxy
server  web01 10.1.31.21:80 cookie cookie_web01 check inter 5000 rise 2 fall 5 disabled
server  web02 10.1.31.22:80 cookie cookie_web02 check inter 5000 rise 2 fall 5 disabled
server  prx   10.1.31.10:9000  backup

Here both servers web01 and web02 are set to disabled, in which case the backup server prx on 10.1.31.10:9000 will be used, which serves a maintenance page. The prx server in our case is the HAProxy server itself and on port 9000 runs an Apache HTTPD, serving the maintenance content:

<VirtualHost *:9000>
    ServerName  example.com
    ServerAdmin webmaster@example.com

    DocumentRoot /var/www/example.com/errors/
    <Directory /var/www/example.com/errors/>
        Options -Indexes
    </Directory>

    RewriteEngine On
    RewriteCond %{DOCUMENT_ROOT}/systemDown.html -f
    RewriteCond %{SCRIPT_FILENAME} !systemDown.html
    RewriteRule ^.*$ /systemDown.html [R=503,L]
    ErrorDocument 503 /systemDown.html

</VirtualHost>
daff
  • 4,809
  • 2
  • 28
  • 27
  • I don't understand if I just need to use `disabled` and `backup` or if I also need to configure a process (different from HAProxy) that listens on a custom port. Probably it is the second one: it would be better to have a built in option in HAProxy! – collimarco Sep 27 '17 at 08:54
2

This blog post helped me: https://rimuhosting.com/knowledgebase/creating-a-maintenance-page-for-your-site

It suggests setting/customizing the 503 error handler:

errorfile 503 /var/www/503maintance.html

Note: you need to include the http headers in the error handler file:

HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html
dom
  • 107
  • 5
Joe J
  • 233
  • 1
  • 2
  • 6
1

What about redirect or redir

A simple redir example

server srv1 10.0.0.10:80 redir http://maintenance.domain.com check

redirect can be set in all by defaults option.

silviud
  • 2,687
  • 2
  • 18
  • 19
0

You may specify a back-end without a server directive and just an 503 error page.
The only thing you'll need to do is (temporary) change the default_backend option, or you can create a acl:

frontend public

    #  default_backend www
    default_backend www-maintenance

backend www-maintenance
    errorfile       503 /etc/haproxy/error/503.http

backend www
    server www      10.0.0.1:8080 check
Tim
  • 420
  • 5
  • 7