(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
Haproxy 192.168.100.100
Nginx 01 192.168.100.201
Nginx 02 192.168.100.202
window10 (for testing) 192.168.100.50
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: First:
Second:
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 global log 127.0.0.1 local0 log 127.0.0.1 local1 notice #log loghost local0 info maxconn 4096 uid 99 gid 99 daemon #debug #quiet defaults log global mode http option httplog option dontlognull retries 3 maxconn 2000 contimeout 5000 clitimeout 50000 srvtimeout 50000 listen webcluster 0.0.0.0:80 option httpchk GET /test.html balance roundrobin server inst1 192.168.100.201:80 check inter 2000 fall 3 server inst2 192.168.100.202:80 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 (192.168.100.100/test.html)
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
vim/etc/haproxy/haproxy.cfg //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