Haproxy Builds a Web Cluster

Article Directory

1. Common Web cluster schedulers:

2. Haproxy application analysis:

3. The principle of Haproxy scheduling algorithm:


Step 1: Set up two Nginx servers

Step 2: Set up Haproxy

Step 3: Use a browser to access the dispatch server on win10 (

Step 4: Optimize the log

1. Common Web cluster schedulers:

At present, the common Web cluster scheduler is divided into software and hardware. Software usually uses open source LVS, Haproxy, Nginx. Hardware usually uses F5 more. Many people also use some domestic products, such as pike, green league, etc.

2. Haproxy application analysis:

(1) LVS has a strong anti-load capability in enterprise applications, but it has some shortcomings:

LVS does not support regular processing and cannot achieve static-dynamic separation.

For large websites, LVS implementation configuration is complex and maintenance costs are relatively high

(2) Haproxy is a software that provides high availability, load balancing, and proxy based on TCP and HTTP applications

Especially for Web sites with very heavy loads;

Running on current hardware can support tens of thousands of concurrent connection requests

3. The principle of Haproxy scheduling algorithm:

Haproxy supports a variety of scheduling algorithms, the three most commonly used are:


RR algorithm is the simplest and most commonly used algorithm, namely polling scheduling.


LC algorithm is the minimum number of connections algorithm, which dynamically allocates front-end requests according to the number of connections between nodes in the back-end.


SH is a source-based access scheduling algorithm, which is used in some server-side scenarios where Session sessions are recorded. It can do cluster scheduling based on source IP, cookies, etc.


(1) Environmental preparation:

Two Nginx servers, one Haproxy dispatch server and one window for testing;

Write two test pages on two Nginx servers;

All host network cards are set to host-only mode, modifying network card information;

Clients can access the dispatch server to access two node servers without having to access the real server address.

Role IP Address


Nginx 01

Nginx 02

window10 (for testing)

Step 1: Set up two Nginx servers

The details are not written here anymore. You can see the previous blog, which has detailed construction process and details.

After setting up the Nginx service, add a test home page to the Nginx home page html to verify the results of the experiment:



Step 2: Set up Haproxy

(1) Install compilation tools:

yum install bzip2-devel pcre-devel gcc gcc-c++ make -y

(2) The haproxy package can be mounted locally by remote mounting, and then decompressed:

 tar zxvf haproxy-1.5.19.tar.gz -C /opt/
 //Unzip to / opt directory

(3) Compilation:

make TARGET=linux3100
 Note: For this version number, you can use uname-a command to view the version number

make install

(4) Create files to make backup changes:

mkdir /etc/haproxy
cp examples/haproxy.cfg /etc/haproxy/

vim /etc/haproxy/haproxy.cfg

        log   local0
        log   local1 notice
        #log loghost    local0 info
        maxconn 4096
        uid 99
        gid 99

        log     global
        mode    http
        option  httplog
        option  dontlognull
        retries 3
        maxconn 2000
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen webcluster
       option httpchk GET /test.html
       balance roundrobin
       server inst1 check inter 2000 fall 3
       server inst2 check inter 2000 fall 3

(5) Startup script:

cp examples/haproxy.init /etc/init.d/haproxy
 //Copy to init.d boot process

chmod +x /etc/init.d/haproxy
 //Add Execution Rights

chkconfig --add haproxy 
 //Add to service for easy identification

ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
 //Create Soft Connections

(6) Opening services:

service haproxy start

Turn off environments such as firewalls

[root@Haproxy haproxy-1.5.19]# systemctl stop firewalld.service 
[root@Haproxy haproxy-1.5.19]# setenforce 0

Step 3: Use a browser to access the dispatch server on win7 (

Step 4: Optimize the log

By optimizing and modifying the configuration file of the dispatcher, normal access information and error information can be stored in separate log files for easy management; Haproxy's log is output to syslog of the system by default, which is generally defined separately in production environments.

(1) Modify the main profile


//Modify the global segment:
log /dev/log    local0 info      //Place where information is normally accessed
log /dev/log    local0 notice    //Place to store alerts, warnings, etc.

(2) Restart the service

service haproxy restart

(3) Create files and add rules

[root@Haproxy haproxy]# touch /etc/rsyslog.d/haproxy.conf
[root@Haproxy haproxy]# cd /etc/rsyslog.d/

[root@Haproxy rsyslog.d]# vim haproxy.conf 

if ($programname == 'haproxy' and $syslogseverity-text == 'info')
then -/var/log/haproxy/haproxy-info.log

if ($programname == 'haproxy' and $syslogseverity-text == 'notice')
then -/var/log/haproxy/haproxy-notice.log

(4) Restart Log Service

systemctl restart rsyslog.service

(5) View the log file: /var/log/haproxy/haproxy-info.log

Tags: Nginx vim network Session

Posted on Sat, 08 Feb 2020 12:48:36 -0500 by jaxxstorm