环境
-
需要四台服务器,两台作为web服务器,一台作为haproxy,一台作为客户端.
-
该实验的架构图为:
-
关闭防火墙和selinux
systemctl stop firewalld && setenforce 0
部署web服务器(两台一样的操作)
-
安装nginx
如果你想要安装nginx,请你点击nginx—编译安装或nginx—yum安装 -
添加验证信息
echo "验证信息" >/usr/share/nginx/html/index.html
- 本机检测
curl localhost
部署haproxy
- 下载安装haproxy并启动
yum -y install haproxy
systemctl start haproxy
- 如果想知道配置文件详情,请点击haproxy配置文件详解
- 修改配置文件
vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local3 info
maxconn 4096
uid nobody
# uid 99
gid nobody
# gid 99
daemon
nbproc 1
defaults
log global
mode http
maxconn 2048
retries 3
option redispatch
stats uri /haproxy
stats auth hualaotou:123
contimeout 5000
clitimeout 50000
srvtimeout 50000
#timeout connect 5000
#timeout client 50000
#timeout server 50000
frontend http-in
bind 0.0.0.0:80
mode http
log global
option httplog
option httpclose
acl html url_reg -i \.html$
use_backend html-server if html
default_backend html-server
backend html-server
mode http
balance roundrobin
option httpchk GET /index.html
cookie SERVERID insert indirect nocache
server html-A 192.168.92.128:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
server html-B 192.168.92.129:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5
- 重新启动haproxy
systemctl restart haproxy
检测
- 在客户端通过IP访问
curl http://192.168.92.130
- 在浏览器访问
http://192.168.92.130/
有回话保持,
用另一个浏览器再访问
http://192.168.92.130/haproxy