For single-server websites, I’m very happy with what I can accomplish with NGINX as the first thing that is handling requests server side. It can serve static files fast and act as the perfect reverse proxy for my application server.

But when it comes to multiple-server websites, I do not like using NGINX as the point of first contact. This is primarily because in this scenario I have to use NGINX as a load balancer.

Whatever I do while accomplishing that, my config files end up being messy. That is perhaps because I (try to) have one server act as the router in addition to it being one of the backends. To save myself the trouble, I often end up creating a new server which acts solely as the router / balancer.

Fortunately HAProxy is here to help me (us) out.

High Availability Proxy offers easily-understandable configurations which allow for a tidy load balancing setup.

To get started, we install HAProxy. On Debian/Ubuntu this is:

# linux terminal code

sudo apt-get install haproxy

followed by enabling it:

# file: /etc/default/haproxy

Next, we edit its config file to specify process username, maximum connections, timeout values, backend server IP addresses, etc.:

# file: /etc/haproxy/haproxy.cfg

  user system-username
  maxconn 512

  mode http
  timeout connect 5s
  timeout client 10s
  timeout server 10s

listen sample-app-name
  bind :80
  balance roundrobin
  server serv1 localhost:5000 maxconn 256
  server serv2 maxconn 256

After executing sudo service haproxy start, that config will use the Round Robin strategy to balance incoming traffic between the local server’s port 5000 and another IP’s port 80.

Needless to say, HAProxy offers numerous configuration options for advanced load balancing requirements. For a deeper look, give it a try and find out first hand how much friendlier it is than NGINX in this area.