HaProxy configuration examples
Following up on the first article, in this one I want to look at a few examples of configuring HaProxy for balancing traffic.
The whole HaProxy configuration is stored in the file /etc/haproxy/haproxy.cfg
The default config has several examples of describing backend, frontend and listen sections.
It also contains the **Global** section, which describes the options that are global. Again everything is very simple and clear for anyone who knows English at the I read with a dictionary level. Unfold the spoiler:
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
To enable logging use this article.
Next comes the defaults section, which holds the settings common to everything.
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 will start my example with the more complicated approach: the Frontend-Backend description. This approach can be very useful if you have several groups of servers responsible for different tasks, in my example - 2 sites live on 2 pairs of servers. So the groups of servers are joined into backends, and the conditions for delivering the requests to them are defined with acl in the frontend 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 192.168.10.20:80 check inter 2000 fall 3
server web02 192.168.10.16:80 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 192.168.10.13:80 check inter 2000 fall 3
server app02 192.168.10.14:80 check inter 2000 fall 3
Balancing https traffic happens in tcp mode. Below is an example of a listen section. By analogy with the previous example, several servers can be described with the frontend-backend approach.
In tcp mode HaProxy simply sends all the traffic to the servers behind the balancer.
####
# HTTPs section
####
listen https-proxy *:443
mode tcp
balance source
option httpclose
option forwardfor
server web01 192.168.10.20:443 check port 443
server web02 192.168.10.16:443 check port 443
In tcp mode you can also balance MySQL or MSSQL traffic, if you have master-master replication configured. In the case of master-slave replication I recommend using mysql-proxy to spread the select, update, insert queries between the servers
#### # Description of the sql servers #### listen mysql-proxy *:3306 mode tcp balance roundrobin option tcplog server SQL01 192.168.10.21:3306 check port 3306 server SQL02 192.168.10.22:3306 check port 3306 server SQL02 192.168.10.23:3306 check port 3306</div> </div>