When it comes to ensuring that we need our website needs to be online at all times, we need to start looking at High Availability.

Today we will be demonstrating a basic setup of Layer 4 (transport layer) load balancing making use of HAProxy Server with 2 backend nodes using the round robin algorithm, which essentially means that the first backend node will respond on the first request, and the second backend node on the second request.

Our Setup:

We will have 1 haproxy server that will be listening on port 80 and also Python's SimpleHTTPServer running on port 8000, this will act as a lightweight web server to serve a maintenance page when both of our nodes are down.

Then for our back-end nodes, both servers will be listening on port 8080 and only allowing our haproxy to communicate with port 8080. Website configuration and content has been synchronized with each other so that we have the exact same content on both servers.


haproxy: (HAProxy)
node-web1: (LAMP)
node-web2: (LAMP)


$ yum install haproxy screen python openssl openssl-devel -y

HAProxy Configuration:

$ vi /etc/haproxy/haproxy.cfg

    log local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy

    stats socket /var/lib/haproxy/stats mode 600 level admin

    log                     global
    option                  httplog
    option                  dontlognull
    option                  http-server-close
    option                  forwardfor
    option                  redispatch
    retries                 3
    timeout http-request    20s
    timeout queue           86400
    timeout connect         86400
    timeout client          86400
    timeout server          86400
    timeout http-keep-alive 30s
    timeout check           20s
    maxconn                 50000

listen stats
    mode            http
    log             global
    maxconn 10
    timeout queue   100s
    stats enable
    stats hide-version
    stats refresh 30s
    stats show-node
    stats auth admin:sekret
    stats uri  /haproxy?stats

frontend http-in
    bind *:80
    mode http
    acl web hdr_end(host) -i domain.com
    acl www_web hdr_end(host) -i www.domain.com
    default_backend web-backend

backend web-backend
    mode http
    balance roundrobin
    option httpchk GET /status/healthcheck.html
    option httpclose
    option forwardfor
    server node-web1 check 
    server node-web2 check 
    server maintenance backup

Rsyslog Configuration:

$ vi /etc/rsyslog.conf

Uncomment the following:

$ModLoad imudp
$UDPServerRun 514

Backend Node Pages:

Set the following on your index.html for your backend nodes:


Response From: node-web1 []


Response From: node-web2 []

Maintenance Page:

Our maintenance page on Python Simple HTTP Server

$ mkdir -p /opt/python/simplehttpserver
$ cd /opt/python/simplehttpserver
$ vi index.html

<!doctype html>
<title>Site Maintenance</title>
  body { text-align: center; padding: 150px; }
  h1 { font-size: 50px; }
  body { font: 20px Helvetica, sans-serif; color: #333; }
  article { display: block; text-align: left; width: 650px; margin: 0 auto; }
  a { color: #dc8100; text-decoration: none; }
  a:hover { color: #333; text-decoration: none; }

    <h1>We&rsquo;ll be back soon!</h1>
        <p>Sorry for the inconvenience but we&rsquo;re performing some maintenance at the moment. If you need to reach out to us, you can always <a href="mailto:[email protected]">contact us</a>, otherwise we&rsquo;ll be back online, shortly!</p>
        <p>&mdash; Your Orginization </p>

Start the service in a screen session:

$ screen -S MaintenancePage -m -d sh -c "python –m SimpleHTTPServer"

Firewall Configuration:

For each node allow the HAProxy server address to communicate with port 8080/TCP:

$ iptables -A INPUT -p tcp -s [haproxy-source-address] --dport 8080 -j ACCEPT

Enable Services:

$ service rsyslog restart
$ service haproxy restart
$ chkconfig rsyslog on
$ chkconfig haproxy on


$ curl
Response From: node-web1 []

$ curl
Response From: node-web2 []