HaProxy configuration examples

I want to consider a few examples of setting HaProxy for balancing traffic in this article.

As I mentioned in a previous article all configuration is located in /etc/haproxy/haproxy.cfg file.

The standard configuration has multiple examples of configuration.

Also it contains the “Global” section, that contains configuration parameters that are global for all sections (listen, frontend, backend).

Example of global

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

In order to enable loging, please refer to this article.

Next comes the defaults section.

Example of defaults

    log         global
    option      dontlognull
    option      forwardfor
    option      redispatch
    timeout connect 10000 # default 10 second time out if a backend is not found
    timeout client 300000
    timeout server 300000
    maxconn     60000
    retries     3

I’ll start my first example with the more sophisticated approach: description of Frontend-Backend.

This approach can be very useful if you have multiple servers’ groups responsible for different tasks, in my example – 2 sites are located on the 2 pairs of servers. The group of servers are gathered in backends accordingly. The conditions of delivery of requests to them are determined using the frontend’s acl section.

# HTTP section
frontend http-proxy
 bind *:80
 mode http
 acl web1 hdr_beg(host) www.website1.com
 acl web2 hdr_beg(host) www.website2.com
 use_backend http-web2 if web2
 default_backend http-web1

# Description of the web1 servers
backend http-web1
 mode http
 balance roundrobin
 option httpclose
 option forwardfor
 option httpchk OPTIONS * HTTP/1.1rnHost: www
server web01 check inter 2000 fall 3
server web02 check inter 2000 fall 3

# Description of the web2 servers
backend http-web2
 balance roundrobin
 mode http
 option httpclose
 option forwardfor
 option httpchk OPTIONS * HTTP/1.1rnHost: www
server app01 check inter 2000 fall 3
server app02 check inter 2000 fall 3

The load balancing of the HTTPS traffic occurs in the tcp mode. Please find the example below.
By analogy with the previous example, multiple servers can be described using the approach frontend-backend.

In the tcp mode HaProxy simply delivers all traffic on the server behind the balancer.

# HTTPs section
listen https-proxy *:443
 mode tcp
 balance source
 option httpclose
 option forwardfor
server web01 check port 443
server web02 check port 443
Share Button

Leave a Reply