目录
一、haproxy
1.1 haproxy简介
haproxy是由法国开发者威利塔罗(Willy Tarreau)在2000年使用C语言开发的一个开源软件,是一款具备高并发、高性能的TCP和TTTP负载均衡器,并支持基于cookie的持久性,自动故障切换,支持正则表达式以及web状态的统计。
企业版和社区版功能对比:
1.2 haproxy的安装和服务器信息
架构流程:用户通过防火墙进入网络内部,访问到HAPROXY,然后HAPROXY通过设定将流量发送到后端的Real Server 主机上
实验演示图:
实验环境部署
[server1]
[root@server1 ~]# dnf install nginx -y
[root@server1 ~]# echo webserver1 - 172.25.254.10 > /usr/share/nginx/html/index.html
[root@server1 ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[server2]
[root@server1 ~]# dnf install nginx -y
[root@server1 ~]# echo webserver1 - 172.25.254.20 > /usr/share/nginx/html/index.html
[root@server1 ~]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[haproxy]
#测试
[root@haproxy ~]# curl 172.25.254.10
webserver1 - 172.25.254.10
[root@haproxy ~]# curl 172.25.254.20
webserver2 - 172.25.254.20
1.3 haproxy 基本配置信息
haproxy的配置问件haproxy.cfg由两大部分组成,分别是global和proxies
global:全局配置段
-
进程及安全配置相关的参数
-
性能调整相关参数
-
Debug参数
proxies:代理配置段
-
defaults:为frontend,backend,listen 提供默认配置
-
frontend:前端,相当于nginx的server0
-
backend : 后端,相当于nginx中的upstream 0
-
listen: 同时拥有前端和后端配置,配置简单,生产推荐使用
1.4 haproxy 功能介绍
(1)负载均衡:HAProxy能够将客户端的请求分发到多台后端服务器上,实现负载均衡,提高整体服务的处理能力和响应速度。支持多种负载均衡算法,如轮询调度(Round Robin)、加权轮询(Weighted Round Robin)、最少连接者先处理(Least Connections)等,以适应不同的应用场景和需求。
(2)高可用性:通过健康检查和故障转移机制,确保服务的连续性。HAProxy会定期检测后端服务器的状态,一旦发现故障服务器,会自动将其从负载均衡列表中摘除,并将请求转发到其他健康的服务器上。支持会话保持(Session Persistence),确保同一客户端的请求在一定时间内始终被转发到同一台后端服务器上,以提高应用的稳定性和用户体验。
(3)代理服务:支持基于TCP和HTTP的应用代理,适用于各种网络协议和服务类型。在HTTP代理模式下,HAProxy能够分析应用层协议,对请求和响应进行精细控制,如添加、修改或删除HTTP头部信息等。
(4)安全性:HAProxy的运行模式可以保护后端服务器不被直接暴露在网络上,增加系统的安全性。支持SSL/TLS加密,确保数据传输过程中的安全性。
1.5 harpoxy 特点
(1)高性能:HAProxy采用事件驱动模型,能够处理大量并发连接。其单进程模型相比多进程或多线程模型具有更好的资源和时间管理能力,适用于高负载场景。支持高并发连接数,最高可同时维护数万甚至数十万个并发连接。
(2)灵活性强:配置简单灵活,支持丰富的配置选项和指令,满足不同用户的需求。支持动态配置更新,无需重启服务即可生效,提高系统的可用性和灵活性。
(3)可靠性高:经过多年的发展和完善,HAProxy已经成为一款成熟稳定的软件产品,被广泛应用于各类生产环境中。与硬件级的负载均衡设备相比,HAProxy在性能和可靠性方面表现出色,且成本更低。
二、haproxy 算法
在HAProxy中,负载均衡算法被分为静态算法、动态算法以及其他算法,每种算法都有其特定的描述、使用场景和语法。以下是对这些算法的详细解析:
2.1 静态算法
静态算法在运行时不会根据后端服务器的实时状态(如负载、连接数等)进行调整,而是按照预先设定的规则进行调度。
2.1.1Static-RR(静态轮询)
描述:基于权重的轮询调度,但不支持运行时权重的动态调整及后端服务器的慢启动。每个后端服务器根据其在列表中的位置和权重轮流接收请求,但算法会忽略服务器的当前负载和连接数。
使用场景:适用于服务器性能相对固定,且不需要根据实时负载动态调整权重的场景。
语法:在配置文件中,通过balance static-rr指令指定此算法。服务器配置示例:
listen web_host
bind 10.0.0.7:80
mode http
balance static-rr
server web1 10.0.0.17:80 weight 1 check inter 3000 fall 2 rise 5
server web2 10.0.0.27:80 weight 2 check inter 3000 fall 2 rise 5
2.1.2 First(优先调度)
描述:根据服务器在列表中的位置,自上而下进行调度。只有当列表中前面的服务器连接数达到上限时,新请求才会分配给后面的服务器。此算法会忽略服务器的权重设置。
使用场景:使用较少,因为它不考虑服务器的权重和当前负载。
语法:通过balance first指令指定。
2.2 动态算法
动态算法会根据后端服务器的实时状态(如负载、连接数等)动态调整调度策略,以优化请求分配。
2.2.1 Round Robin(轮询)
描述:基于权重的轮询动态调度算法,支持权重的运行时调整。与Static-RR不同,Round Robin支持慢启动(新加入的服务器会逐渐增加转发数)。
使用场景:广泛使用的默认调度算法,适用于大多数需要负载均衡的场景。
语法:通过balance roundrobin指令指定。
2.2.2 Leastconn(最少连接)
描述:加权的最少连接数算法,支持权重的运行时调整和慢启动。根据当前连接数最少的后端服务器进行优先调度,适合长连接场景。
使用场景:适用于MySQL等需要长连接的场景。
语法:通过balance leastconn指令指定。
2.2.3. Random(随机)
描述:基于随机数的负载平衡算法,从1.9版本开始引入。支持权重的动态调整,权重较大的主机有更大概率获取新请求。
使用场景:对于大型服务器场或经常添加或删除服务器的环境非常有用。
语法:通过balance random指令指定。
2.3 其他算法
2.3.1 Source(源地址hash)
描述:基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一个后端服务器。默认为静态方式,但可以通过hash-type选项更改。
使用场景:适用于需要保持会话一致性的场景,如购物车、用户登录等。
语法:通过balance source和hash-type指令配置。
2.3.2. URI(URI hash)
描述:基于用户请求的URI的左半部分或整个URI做hash,再将hash结果对总权重进行取模后,将请求转发到后端指定服务器。适用于后端是缓存服务器的场景。
使用场景:适用于缓存服务器等需要根据URI分发请求的场景。
语法:通过balance uri和hash-type指令配置。
三、haproxy 应用场景和实例分析
HAProxy广泛应用于各类企业中,特别是在需要处理大量并发请求、实现高可用性和负载均衡的场景下。例如,它可以用于构建高可用的Web服务器集群、数据库的读写分离和负载均衡、以及需要处理大量并发连接的应用程序等。
综上所述,HAProxy是一款功能强大、性能高效、灵活可靠的高可用性负载均衡器和代理服务器软件。
实验部分
3.1 haproxy基本配置信息
3.1.1 haproxy环境部署
#环境准备
[root@haproxy ~]# dnf install haproxy -y
#haproxy
[root@haproxy ~]# rpm -qc haproxy
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80
mode http
use_backend webcluster-host
backend webcluster-host
balance rountrobin
server web1 172.25.254.10:80
server web2 172.25.254.20:80
[root@haproxy ~]# systemctl restart haproxy.service
#如果报错
[root@haproxy ~]# > /var/log/messages
[root@haproxy ~]# cat /var/log/messages
# 测试:
[root@haproxy ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
#当我们停止时nginx时,就不能访问
[root@webserver1 ~]# systemctl stop nginx
[root@haproxy ~]# curl 172.25.254.100
webserver1 - 172.25.254.20
3.1.2 haproxy全局配
#多进程
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
nbproc 2
cpu-map 1 0
cpu-map 2 1
[root@haproxy ~]# systemctl restart haproxy.service
#查看多进程信息
[root@haproxy ~]# pstree -p | grep haproxy
|-haproxy(31828)-+-haproxy(31831)
| `-haproxy(31832)
# 启用多线程
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
nbthread 2
#定义日志
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
log 127.0.0.1 local2
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# vim /etc/rsyslog.conf
module(load="lmudp") #udp的这个打开
input(type="imudp" port="514")
local2.* /var/log/haproxy.log
#查看多线程信息
[root@haproxy ~]# pstree -p | grep haproxy
|-haproxy(31841)---haproxy(31843)---{haproxy}(31844)
3.1.3 haproxy代理参数
#关掉webserver1\2的,可以访问100的
[root@webserver1 ~]# systemctl stop nginx.service
[root@webserver2 ~]# systemctl stop nginx.service
[root@haproxy ~]# dnf install httpd -y
[root@haproxy ~]# vim /etc/httpd/conf/httpd.conf
#backup --sorryserver 的端口
Listen 8080
[root@haproxy ~]# systemctl enable --now httpd
[root@haproxy ~]# echo sorry > /var/www/html/index.html
[root@haproxy ~]# curl 172.25.254.100
sorry
#如果是开启的话,需在文件里注释掉
[root@webserver1 ~]# systemctl start nginx.service
[root@webserver2 ~]# systemctl start nginx.service
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
balance roundrobin
#server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
#server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 2
server web_sorry 172.25.254.100:8080 backup
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver1 - 172.25.254.10
webserver1 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.20
#下线指定realserver
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
balance roundrobin
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2 disabled
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 2
server web_sorry 172.25.254.100:8080 backup
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver1 - 172.25.254.20
webserver1 - 172.25.254.20
#网页重定向
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
balance roundrobin
redirect prefix http://www.baidu.com/
#server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
#server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 2
#server web_sorry 172.25.254.100:8080 backup
[root@haproxy ~]# systemctl restart haproxy.service
3.2 haproxy热处理
[root@haproxy ~]# dnf install socat -y
[root@haproxy ~]# echo "set weight webcluster/web1 1" | socat stdio /var/lib/haproxy/stats
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
[root@haproxy ~]# echo "set weight webcluster/web1 2" | socat stdio /var/lib/haproxy/stats
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
[root@haproxy ~]# echo "disable server webcluster/web1 " | socat stdio /var/lib/haproxy/stats
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
[root@haproxy ~]# echo "enable server webcluster/web1 " | socat stdio /var/lib/haproxy/stats
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
#haproxy多进程如何热处理
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
global
stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1
stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2
# utilize system-wide crypto-policies
ssl-default-bind-ciphers PROFILE=SYSTEM
ssl-default-server-ciphers PROFILE=SYSTEM
nbproc 2
cpu-map 1 0
cpu-map 2 1
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# ls /var/lib/haproxy/*
/var/lib/haproxy/stats /var/lib/haproxy/stats1 /var/lib/haproxy/stats2
3.3 haproxy算法
3.3.1 静态算法
static-rr:不能通过socat修改权重
# 不要多进程
stats socket /var/lib/haproxy/stats mode 600 level admin
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
#balance roundrobin
balance static-rr
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
# 测试
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.102、first
其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务
其会忽略服务器的权重设置
不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
#balance roundrobin
#balance static-rr
balance first
server web1 172.25.254.10:80 maxconn 1 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
# 测试: 多台主机执行死循环,可看到
[root@webserver1 ~]# while true ; do curl 172.25.254.100; sleep 0.1 ; done
[root@webserver2 ~]# while true ; do curl 172.25.254.100; sleep 0.1 ; done
3.3.2 动态算法
(1)roundrobin:给权重高负载小的,基于权重的轮询动态调度算法,支持权重的运行时调整,不同于lvs中的rr轮训模式,HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数。
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
balance roundrobin
#balance static-rr
#balance first
server web1 172.25.254.10:80 maxconn 1 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
#测试
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
(2)leastconn:谁链接最少给谁
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
#balance static-rr
#balance first
#balance roundrobin
balance leastconn
server web1 172.25.254.10:80 maxconn 1 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
#测试
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
webserver1 - 172.25.254.10
webserver2 - 172.25.254.20
3.3.3 其他算法
其他算法有hash-type contsistent是动态,没有是静态
(1)source
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
#balance static-rr
#balance first
#balance roundrobin
#balance leastconn
balance source
server web1 172.25.254.10:80 maxconn 1 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
#测试
[root@haproxy ~]# for i in {1..10}; do curl 172.25.254.100; done
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
webserver2 - 172.25.254.20
(2)uri
#webserver1上
[root@webserver1 ~]# echo 172.25.254.10 - index1.html > /usr/share/nginx/html/index1.html
[root@webserver1 ~]# echo 172.25.254.10 - index2.html > /usr/share/nginx/html/index2.html
[root@webserver1 ~]# echo 172.25.254.10 - index3.html > /usr/share/nginx/html/index3.html
#webserver2
[root@webserver2 ~]# echo 172.25.254.20 - index1.html > /usr/share/nginx/html/index1.html
[root@webserver2 ~]# echo 172.25.254.20 - index2.html > /usr/share/nginx/html/index2.html
[root@webserver2 ~]# echo 172.25.254.20 - index3.html > /usr/share/nginx/html/index3.html
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
#balance roundrobin
#balance leastconn
#balance static-rr
#balance first
#balance source
balance uri
hash-type consistent
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
#测试
[root@haproxy ~]# curl 172.25.254.100/index1.html
172.25.254.10 - index1.html
[root@haproxy ~]# curl 172.25.254.100/index2.html
172.25.254.20 - index2.html
[root@haproxy ~]# curl 172.25.254.100/index3.html
172.25.254.10 - index3.html
(3)url_param
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
#balance roundrobin
#balance leastconn
#balance static-rr
#balance first
#balance source
#balance uri
balance url_param name,userid
hash-type consistent
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
#测试
[root@haproxy ~]# curl 172.25.254.100/index1.html?name=test
172.25.254.10 - index1.html
[root@haproxy ~]# curl 172.25.254.100/index1.html?name=haha
172.25.254.20 - index1.html
[root@haproxy ~]# curl 172.25.254.100/index1.html?name=xixi
172.25.254.10 - index1.html
[root@haproxy ~]# curl 172.25.254.100/index1.html?name=test
172.25.254.10 - index1.html
[root@haproxy ~]# curl 172.25.254.100/index1.html?name=haha
172.25.254.20 - index1.html
(4)hdr
不同浏览器访问不同
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
#balance roundrobin
#balance leastconn
#balance static-rr
#balance first
#balance source
#balance uri
#balance url_param name,userid
balance hdr(User-Agent)
hash-type consistent
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
#测试 -vA 伪装浏览器
[root@haproxy ~]# curl -vA "firefox" 172.25.254.100/index.html
3.4 haproxy的高级功能
3.4.1 haproxy的状态页
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen stats
mode http
bind *:9999
stats enable
stats refresh 3
stats uri /status #自定义stats page uri
stats auth lee:lee #认证,可出现多次
访问浏览器:172.25.254.100:9999/status
3.4.2 基于cookie的会话保持
在一个浏览器访问后,会记住选择,之后刷新一直是该后端主机,另一个浏览器访问则是另一个后端主机
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
balance roundrobin
cookie WEBCOOKIE insert nocache indirect
server web1 172.25.254.10:80 cookie lee1 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 cookie lee2 check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# curl -b WEBCOOKIE=lee1 172.25.254.100
webserver1 - 172.25.254.10
[root@haproxy ~]# curl -b WEBCOOKIE=lee2 172.25.254.100
webserver2 - 172.25.254.20
3.4.3 IP透传
七层代理 mode--->http
#webserver1
[root@webserver1 ~]# systemctl disable nginx
[root@webserver1 ~]# systemctl stop nginx
[root@webserver1 ~]# dnf install httpd -y
[root@webserver1 ~]# echo webserver1 - 172.25.254.10 > /var/www/html/index.html
[root@webserver1 ~]# vim /etc/httpd/conf/httpd.conf
如下图标注
%{X-Forwarded-For}i
[root@webserver1 ~]# systemctl enable --now httpd
#测试
[root@webserver1 ~]# tail -n 3 /etc/httpd/logs/access_log
[root@webserver2 ~]# tail -3 /var/log/nginx/access.log
有IP地址
# 如果把option forwardfor except 127.0.0.0/8 注释掉则没有IP地址
四层代理
四层代理mode--->tcp
看不到IP地址
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
mode tcp
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 send-proxy check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
#webserver2
[root@webserver2 ~]#
vim /etc/nginx/nginx.conf
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request"'
' "$proxy_protocol_addr"'
server {
listen 80 proxy_protocol;
[root@webserver2 ~]# systemctl restart nginx
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2
server web2 172.25.254.20:80 send-proxy check inter 2 fall 3 rise 5 weight 1
[root@haproxy ~]# systemctl restart haproxy.service
# 测试
[root@webserver2 ~]# tail -n 3 /var/log/nginx/access.log
[root@webserver1 ~]# tail -n 3 /etc/httpd/logs/access_log
再次访问后,查看日志可以看到地址
3.4.4 ACL常用参数
#匹配域名
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
bind *:80
mode http
acl test hdr_dom(host) -i www.timinglee.org #判断规则
use_backend webcluster-host if test #是访问webcluster-host
default_backend default-host #不是访问default-host
backend webcluster-host
mode http
server web1 172.25.254.10:80 check inter 2 fall 2 rise 5
backend default-host
mode http
server web2 172.25.254.20:80 check inter 2 fall 2 rise 5
[root@haproxy ~]# systemctl restart haproxy.service
#注意 做这个要把四层代理proxy_protocol这个参数删掉
# 测试
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl www.timinglee.org
webserver1 - 172.25.254.10
以什么什么结尾
frontend webcluster
bind *:80
mode http
acl test hdr_end(host) -i .org
use_backend webcluster-host if test
default_backend default-host
#测试
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@haproxy ~]# curl www.timinglee.org
webserver1 - 172.25.254.10
[root@haproxy ~]# curl www.timinglee.com
webserver2 - 172.25.254.20
以什么什么开头
frontend webcluster
bind *:80
mode http
acl test hdr_beg(host) -i bbs
use_backend webcluster-host if test
default_backend default-host
#测试
[root@haproxy ~]# curl www.timinglee.org
webserver2 - 172.25.254.20
[root@haproxy ~]# curl bbs.timinglee.org
webserver1 - 172.25.254.10
base
frontend webcluster
bind *:80
mode http
#acl test hdr_beg(host) -i bbs
acl test base_sub -m sub lee #只要包含lee就是匹配成功
use_backend webcluster-host if test
default_backend default-host
[root@webserver1 ~]# mkdir /var/www/html/lee -p
[root@webserver1 ~]# echo 172.25.254.10 lee > /var/www/html/lee/index.html
[root@webserver1 ~]# curl 172.25.254.10/lee/
172.25.254.10 lee
#测试
[root@haproxy ~]# curl www.timinglee.com
webserver1 - 172.25.254.10
[root@haproxy ~]# curl bbs.timinglee.org
webserver1 - 172.25.254.10
[root@haproxy ~]# curl www.test.com
webserver2 - 172.25.254.20
[root@haproxy ~]# curl www.test.com/lee/
172.25.254.10 lee
3.4.5 ACL应用实例
基于源IP或子网调度访问,基于本地解析,所以本地解析一定要做。
frontend webcluster
bind *:80
mode http
acl domain hdr_dom(host) -i www.timinglee.org
use_backend webcluster-host if domain
default_backend default-host
#测试
[root@haproxy ~]# curl www.test.com
webserver2 - 172.25.254.20
[root@haproxy ~]# curl www.timinglee.org
webserver1 - 172.25.254.10
基于源地址的访问控制
拒绝指定IP或者IP范围访问
frontend webcluster
bind *:80
mode http
acl ctrl_ip src 172.25.254.1 172.25.254.20
use_backend webcluster-host if ctrl_ip
default_backend default-host
#测试
[root@haproxy ~]# curl 172.25.254.100
webserver2 - 172.25.254.20
[root@webserver2 ~]# curl 172.25.254.100
webserver1 - 172.25.254.10
匹配浏览器类型
匹配客户端浏览器,将不同类型的浏览器调动至不同的服务器组、
范例: 拒绝curl和wget的访问
frontend webcluster
bind *:80
mode http
acl badwebrowers hdr_sub(User-Agent) -i curl wget
#use_backend webcluster-host if webrowers
http-request deny if badwebrowers
default_backend default-host
# 测试
去浏览器访问
基于文件后缀名实现动静分离
[root@webserver1 ~]# dnf install php -y
[root@webserver1 ~]# systemctl restart httpd
[root@webserver1 ~]# vim /var/www/html/index.php
[root@webserver1 ~]# cat /var/www/html/index.php
<?php
phpinfo();
?>
#haproxy
frontend webcluster
bind *:80
mode http
acl static path_end -i .html .jpg .png .css .js
acl php path_end -i .php
use_backend webcluster-host if php
default_backend default-host
# 测试
浏览器进行测试
3.5 自定义HAProxy错误界面
基于自定义的错误页面文件
#webserver1\2主机上
system stop httpd/nginx
#haproxy主机上
[root@haproxy ~]# mkdir /etc/haproxy/errorpage -p
[root@haproxy ~]# vim /etc/haproxy/errorpage/503.http
HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html;charset=UTF-8
<html><body><h1>什么动物生气最安静</h1>
大猩猩!!
</body></html>
这次haproxy学习中实验较多,所以代码部分可能会很多,为了更好理解,我也会去优化代码布局和流程,并在最近这段时间重新将这次的博客整理一下。