HAProxy As HTTP Load Balancing

Installation HAProxy and configure Load Balancing Server.
This example based on the environment:
| Frontend | | Web Server | | Web Server |
| HAProxy |     | 1 |                   | 2 |
Install HAProxy.
yum -y install haproxy

Configure HAProxy.

mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.org
vi /etc/haproxy/haproxy.cfg
# create new
# for logging section
log local2 info
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
# max per-process number of connections
maxconn 256
# process’ user and group
user haproxy
group haproxy
# makes the process fork into background

# running mode
mode http
# use global settings
log global
# get HTTP request log
option httplog
# timeout if backends do not reply
timeout connect 10s
# timeout on client side
timeout client 30s
# timeout on server side
timeout server 30s

# define frontend ( set any name for “http-in” section )
frontend http-in
# listen 80
bind *:80
# set default backend
default_backend backend_servers
# send X-Forwarded-For header
option forwardfor

# define backend
backend backend_servers
# balance with roundrobin
balance roundrobin
# define backend servers
server web1 check
server web2 check

service haproxy start
Starting haproxy: [ OK ]
chkconfig haproxy on

Configure Rsyslog to get logs for HAProxy.

vi /etc/rsyslog.conf
line 13,14: uncomment, line 15: add
$ModLoad imudp
$UDPServerRun 514
$AllowedSender UDP,
line 42: change like follows
.info;mail.none;authpriv.none;cron.none,local2.none /var/log/messages
service rsyslog restart
Change httpd settings on Backends to logging X-Forwarded-For header.
vi /etc/httpd/conf/httpd.conf
line 497: change like follows
LogFormat “\”%{X-Forwarded-For}i\” %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” combined
service httpd restart

Be the first to comment

Leave a Reply

Your email address will not be published.