HAProxy:高性能负载均衡实战指南

一,定义

它是一款开源的、高性能的四层 + 七层负载均衡器,也是生产环境中最常用的负载均衡工具之一,常用来解决高并发、高可用的流量分发问题。

HAProxy 核心解析

  1. 核心定位与优势

HAProxy 是纯用户态的负载均衡软件(区别于 LVS 的内核态),核心特点:

• 双协议支持:同时覆盖 OSI 四层(TCP/UDP)和七层(HTTP/HTTPS),兼顾 LVS 的高性能和 Nginx 的灵活特性;

• 高性能:虽然是用户态,但通过优化的事件驱动模型(epoll),单实例可支撑 10 万 + 并发连接;

• 功能丰富:支持会话保持、健康检查、流量限流、SSL 卸载、HTTP 重定向 / 重写、监控面板等;

• 轻量可靠:配置灵活,资源占用低,故障切换快,是中小规模集群的首选。

  1. 核心应用场景

• Web 服务负载均衡(HTTP/HTTPS 七层转发);

• TCP 服务负载均衡(如数据库、Redis、SSH 等四层转发);

• 配合 Keepalived 实现高可用,避免单点故障;

• 替代 Nginx 做更精细的七层流量控制(如按 URL、Cookie 分发)。

二,比较区别

总结:

  1. HAProxy 是四层 + 七层一体化的负载均衡器,兼顾性能与功能,是生产环境的主流选择;
  2. 核心优势是七层流量控制能力强(如 URL/Cookie 分发、监控面板),配置灵活;
  3. 小规模集群可单节点部署,大规模需配合 Keepalived 实现高可用,或与 LVS 分层使用(LVS 做四层入口,HAProxy 做七层分发)。

三,haproxy实验

1.环境设定

2.三台机子设定

1.haproxy主机

复制代码
[root@haproxy ~]# vmset.sh eth0 172.25.254.100 haproxy
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/7)
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:0c:6f:ee brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    altname ens160
    inet 172.25.254.100/24 brd 172.25.254.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe0c:6fee/64 scope link tentative noprefixroute
       valid_lft forever preferred_lft forever
haproxy
[root@haproxy ~]# vmset.sh eth1 192.168.0.100 haproxy norouter
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/8)
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:0c:6f:f8 brd ff:ff:ff:ff:ff:ff
    altname enp19s0
    altname ens224
    inet 192.168.0.100/24 brd 192.168.0.255 scope global noprefixroute eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::4ca7:8cde:1244:8df/64 scope link tentative noprefixroute
       valid_lft forever preferred_lft forever
haproxy


#配置内核路由功能
[root@haproxy ~]# echo net.ipv4.ip_forward=1 > /etc/sysctl.conf
[root@haproxy ~]# sysctl -p
net.ipv4.ip_forward = 1
实验截屏

2.webserver1

复制代码
[root@webserver1 ~]# vmset.sh eth0 192.168.0.10 webserver1 noroute
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:8c:96:72 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    altname ens160
    inet 192.168.0.10/24 brd 192.168.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe8c:9672/64 scope link tentative noprefixroute
       valid_lft forever preferred_lft forever
webserver1

[root@webserver1 ~]# dnf install httpd -y
root@webserver1 ~]# echo webserver1 - 192.168.0.10 > /var/www/html/index.html
[root@webserver1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

2.webserver2

复制代码
[root@webserver2 ~]# vmset.sh eth0 192.168.0.20 webserver2 noroute
连接已成功激活(D-Bus 活动路径:/org/freedesktop/NetworkManager/ActiveConnection/4)
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:0c:29:8c:96:72 brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    altname ens160
    inet 192.168.0.20/24 brd 192.168.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe8c:9672/64 scope link tentative noprefixroute
       valid_lft forever preferred_lft forever
webserver2
[root@webserver2 ~]# dnf install httpd -y 
[root@webserver2 ~]#  echo webserver2 - 192.168.0.20 > /var/www/html/index.html
[root@webserver2 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

4.验证环境

复制代码
#在haproxy中访问
[root@haproxy ~]# curl  192.168.0.10
webserver1 - 192.168.0.10
[root@haproxy ~]# curl  192.168.0.20
webserver2 - 192.168.0.20

四,Haproxy的安装及配置参数

1.安装

复制代码
#在调度器(双网卡主机中)
[root@haproxy ~]# dnf install haproxy.x86_64 -y
[root@haproxy ~]# systemctl enable --now haproxy
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.

2.harpoxy的参数详解实验

1.实现最基本的负载

复制代码
#设定vim中tab键的空格个数
[root@haproxy ~]# vim ~/.vimrc
set ts=4 ai

#前后端分开设定
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

frontend webcluster
    bind            *:80
    mode            http
    use_backend     webserver-80

backend webserver-80
    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5
    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5

[root@haproxy ~]# systemctl restart haproxy.service

#测试:
[root@haproxy ~]# curl  172.25.254.100
webserver2 - 192.168.0.20
[root@haproxy ~]# curl  172.25.254.100
webserver1 - 192.168.0.10


#用listen方式书写负载均衡
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind        *:80
    mode        http
    server haha 192.168.0.10:80 check inter 3s fall 3 rise 5
    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5
    
[root@haproxy ~]# systemctl restart haproxy.service


#测试
[root@haproxy ~]# curl  172.25.254.100
webserver2 - 192.168.0.20
[root@haproxy ~]# curl  172.25.254.100
webserver1 - 192.168.0.10
实验截屏

2.log 127.0.0.1 local2

指定把日志发送到哪里实验。指定日志发送到192.168.0.10

复制代码
#在192.168.0.10 开启接受日志的端口
[root@webserver1 ~]# vim /etc/rsyslog.conf
 32 module(load="imudp") # needs to be done just once
 33 input(type="imudp" port="514")
 
[root@webserver1 ~]# systemctl restart rsyslog.service

#测试接受日志端口是否开启
[root@webserver1 ~]# netstat -antlupe | grep rsyslog
udp        0      0 0.0.0.0:514             0.0.0.0:*                           0          74140      30965/rsyslogd
udp6       0      0 :::514                  :::*                                0          74141      30965/rsyslogd

#在haproxy主机中设定日志发送信息
[root@haproxy haproxy]# vim haproxy.cfg

log         192.168.0.10 local2
[root@haproxy haproxy]# systemctl restart haproxy.service

#验证
─
[2026-01-23 15:13.54]  ~
[Administrator.DESKTOP-VJ307M3] ➤ curl 172.25.254.100
webserver1 - 192.168.0.10
                                                                                                    ✔
─────────────────────────────────────────────────────────────────────────────────────────────────────
[2026-01-23 15:19.05]  ~
[Administrator.DESKTOP-VJ307M3] ➤ curl 172.25.254.100
webserver2 - 192.168.0.20

[root@webserver1 ~]# cat /var/log/messages

Jan 23 15:19:06 192.168.0.100 haproxy[31310]: 172.25.254.1:9514 [23/Jan/2026:15:19:06.320] webcluster webcluster/haha 0/0/0/1/1 200 273 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
Jan 23 15:19:10 192.168.0.100 haproxy[31310]: 172.25.254.1:9519 [23/Jan/2026:15:19:10.095] webcluster webcluster/hehe 0/0/0/0/0 200 273 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"

3.实现haproxy的多进程

复制代码
#默认haproxy是单进程
[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(31439)---haproxy(31441)-+-{haproxy}(31442)
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
nbproc      2

[root@haproxy ~]# systemctl restart haproxy.service

#验证
[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(31549)-+-haproxy(31551)
           |                `-haproxy(31552)

#多进程cpu绑定
[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 ~]# systemctl stop haproxy.service
[root@haproxy ~]# rm -fr /var/lib/haproxy/stats
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
    #stats socket /var/lib/haproxy/stats
    stats socket /var/lib/haproxy/haproxy1  mode 600 level admin process 1
    stats socket /var/lib/haproxy/haporxy2  mode 660 level admin process 1

[root@haproxy ~]# systemctl restart haproxy.service

#效果
[root@haproxy ~]# ll /var/lib/haproxy/
总用量 0
srw-rw---- 1 root root 0  1月 23 15:41 haporxy2
srw------- 1 root root 0  1月 23 15:41 haproxy1
实验截屏

4.haproxy实现多线程

注意多线程不能和多进程同时启用

复制代码
#查看当前haproxy的进程信息
[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(31742)-+-haproxy(31744)
           |                `-haproxy(31745)
 #查看haproxy子进程的线程信息
[root@haproxy ~]# cat /proc/31744/status  | grep Threads
Threads:        1

#启用多线程
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

	#nbproc     2
    #cpu-map    1 0
    #cpu-map    2 1
    nbthread    2
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
    #stats socket /var/lib/haproxy/haproxy1  mode 600 level admin process 1
    #stats socket /var/lib/haproxy/haporxy2  mode 660 level admin process 1
    
[root@haproxy ~]# systemctl restart haproxy.service

#效果
[root@haproxy ~]# pstree -p | grep haproxy
           |-haproxy(31858)---haproxy(31860)---{haproxy}(31861)

[root@haproxy ~]# cat /proc/31860/status  | grep Threads
Threads:        2

五,haproxy七层代理

一.负载均衡

1.1.什么是负载均衡

负载均衡:Load Balance,简称LB,是一种服务或基于硬件设备等实现的高可用反向代理技术,负载均

衡将特定的业务(web服务、网络流量等)分担给指定的一个或多个后端特定的服务器或设备,从而提高了

公司业务的并发处理能力、保证了业务的高可用性、方便了业务后期的水平动态扩展

1.2.为什么用负载均衡

Web服务器的动态水平扩展-->对用户无感知

增加业务并发访问及处理能力-->解决单服务器瓶颈问题

节约公网IP地址-->降低IT支出成本

隐藏内部服务器IP-->提高内部服务器安全性

配置简单-->固定格式的配置文件

功能丰富-->支持四层和七层,支持动态下线主机

性能较强-->并发数万甚至数十万

1.3.负载均衡类型

F5 美国F5网络公司 https://f5.com/zh

Netscaler 美国思杰公司 https://www.citrix.com.cn/products/citrix-adc/、

Array 华耀 https://www.arraynetworks.com.cn/

AD-1000 深信服 http://www.sangfor.com.cn/

1.3.2.四层负载均衡

1.通过ip+port决定负载均衡的去向。

2.对流量请求进行NAT处理,转发至后台服务器。

3.记录tcp、udp流量分别是由哪台服务器处理,后续该请求连接的流量都通过该服务器处理。

4.支持四层的软件

lvs:重量级四层负载均衡器。

Nginx:轻量级四层负载均衡器,可缓存。(nginx四层是通过upstream模块)

Haproxy:模拟四层转发。

1.3.3.七层负载均衡

1.通过虚拟ur|或主机ip进行流量识别,根据应用层信息进行解析,决定是否需要进行负载均衡。

2.代理后台服务器与客户端建立连接,如nginx可代理前后端,与前端客户端tcp连接,与后端服务器建立tcp连接,

3.支持7层代理的软件:

  • Nginx:基于http协议(nginx七层是通过proxy_pass)

  • Haproxy:七层代理,会话保持、标记、路径转移等。

1.3.4 四层和七层的区别

所谓的四到七层负载均衡,就是在对后台的服务器进行负载均衡时,依据四层的信息或七层的信息来决定怎么样转发流量

四层的负载均衡,就是通过发布三层的IP地址(VIP),然后加四层的端口号,来决定哪些流量需要做负载均衡,对需要处理的流量进行NAT处理,转发至后台服务器,并记录下这个TCP或者UDP的流量是由哪台服务器处理的,后续这个连接的所有流量都同样转发到同一台服务器处理

七层的负载均衡,就是在四层的基础上(没有四层是绝对不可能有七层的),再考虑应用层的特征,比如同一个Web服务器的负载均衡,除了根据VIP加80端口辨别是否需要处理的流量,还可根据七层的URL、浏览器类别、语言来决定是否要进行负载均衡。

1.分层位置:四层负载均衡在传输层及以下,七层负载均衡在应用层及以下

2.性能 :四层负载均衡架构无需解析报文消息内容,在网络吞吐量与处理能力上较高:七层可支持解析应用层报文消息内容,识别URL、Cookie、HTTP header等信息。、

3.原理 :四层负载均衡是基于ip+port;七层是基于虚拟的URL或主机IP等。

4.功能类比:四层负载均衡类似于路由器;七层类似于代理服务器。

5.安全性:四层负载均衡无法识别DDoS攻击;七层可防御SYN Cookie/Flood攻击

二.haproxy简介

HAProxy是法国开发者 威利塔罗(Willy Tarreau) 在2000年使用C语言开发的一个开源软件

是一款具备高并发(万级以上)、高性能的TCP和HTTP负载均衡器

支持基于cookie的持久性,自动故障切换,支持正则表达式及web状态统计

企业版网站:https://www.haproxy.com

社区版网站:http://www.haproxy.org

github:https://github.com/haprox

企业版本和社区版功能对比:

三.haproxy的安装和服务信息

3.1.实验环境

3.2.软件安装

软件包下载地址:

https://github.com/haproxy/wiki/wiki/Packages

安装软件包:

复制代码
[haproxy ~]# dnf install haproxy -y

查看版本:

复制代码
[root@haproxy ~]# haproxy -v
HAProxy version 2.4.22-f8e3218 2023/02/14 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2026.
Known bugs: http://www.haproxy.org/bugs/bugs-2.4.22.html
Running on: Linux 5.14.0-427.13.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Apr 10 10:29:16 EDT 2024 x86_64

3.3.haproxy的基本配置信息

官方文档:http://cbonte.github.io/haproxy-dconv/

HAProxy 的配置文件haproxy.cfg由两大部分组成,分别是:

**global**:全局配置段

  • 进程及安全配置相关的参数

  • 性能调整相关参数

  • Debug参数

**proxies**:代理配置段

  • defaults:为frontend, backend, listen提供默认配置

  • frontend:前端,相当于nginx中的server {}

  • backend:后端,相当于nginx中的upstream {}

  • listen:同时拥有前端和后端配置,配置简单,生产推荐使用

3.3.1.global配置
3.3.1.1 global 配置参数说明
复制代码
global
    log         127.0.0.1 local2		#定义全局的syslog服务器;日志服务器需要开启UDP协议,最多可以定义两个
    chroot      /var/lib/haproxy		#锁定运行目录
    pidfile     /var/run/haproxy.pid	#指定pid文件	
    maxconn     100000					#指定最大连接数
    user        haproxy					#指定haproxy的运行用户
    group       haproxy					#指定haproxy的运行组
    daemon								#指定haproxy以守护进程方式运行
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats		#指定haproxy的套接字文件
    nbproc	2								#指定haproxy的work进程数量,默认是1个
    cpu-map	1 0								#指定第一个work绑定第一个cpu核心
    cpu-map 2 1								#指定第二个work绑定第二个cpu核心
    
    nbthread 2								#指定haproxy的线程数量,默认每个进程一个线程,此参数与nbproc互斥
    
    maxsslconn 100000						#每个haproxy进程ssl最大连接数,用于haproxy配置了证书的场景下
    maxconnrate	100							#指定每个客户端每秒建立连接的最大数量

3.3.1.2 多进程和线程

多进程和socket文件配置如下:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
global
	log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     100000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1	#启用多个sock文件
    stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2
    nbproc 2		#启用多进程
    cpu-map 1 0		#进程和cpu核心绑定防止cpu抖动从而减少系统资源消耗
    cpu-map 2 1		#2 表示第二个进程,1表示第二个cpu核心
    
...下面内容省略 ...

查看多进程信息:

复制代码
haproxy haproxy]# pstree -p | grep haproxy
           |-haproxy(4816)-+-haproxy(4820)
           |               `-haproxy(4821)

启用多线程:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     100000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1	#启用多个sock文件
    stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2
    #nbproc 2			
    #cpu-map 1 0		
    #cpu-map 2 1		
    nbthread 2			#启用多线程
    
...下面内容省略...

多线程对比:

复制代码
未开启多线程
haproxy ~]# cat /proc/xxxx(haproxy子进程id)/status
...上面内容省略...
Threads:        1
...下面内容省略...

开启后
haproxy ~]# cat /proc/xxxx(haproxy子进程id)/status
...上面内容省略...
Threads:        2
...下面内容省略...
3.3.2 proxies配置
3.3.2.1.proxies参数说明proxies

name字段只能使用大小写字母,数字,'-'(dash),'_'(underscore),'.' (dot)和 ':'(colon),并且严格区分大小写

3.3.2.2 Proxies配置-defaults
复制代码
defaults
    mode                    http				#HAProxy实例使用的连接协议			
    log                     global				#指定日志地址和记录日志条目的syslog/rsyslog日志设备
    											#此处的 global表示使用 global配置段中设定的log值。
    
    option                  httplog				#日志记录选项,httplog表示记录与 HTTP会话相关的各种属性值
    											#包括 HTTP请求、会话状态、连接数、源地址以及连接时间等
    
    option                  dontlognull			#dontlognull表示不记录空会话连接日志
    
    option http-server-close					#等待客户端完整HTTP请求的时间,此处为等待10s。
    
    option forwardfor       except 127.0.0.0/8  #透传客户端真实IP至后端web服务器
    											#在apache配置文件中加入:<br>%{X-Forwarded-For}i   
    											#后在webserver中看日志即可看到地址透传信息
    
    option                  redispatch			#当server Id对应的服务器挂掉后,强制定向到其他健康的服务器,重新派发
    
    option 					http-keep-alive		#开启与客户端的会话保持
    
    retries                 3					#连接后端服务器失败次数
    
    timeout http-request    10s					#等待客户端请求完全被接收和处理的最长时间
    
    timeout queue           1m					#设置删除连接和客户端收到503或服务不可用等提示信息前的等待时间
   
    timeout connect         120s				#设置等待服务器连接成功的时间
    
    timeout client          600s				#设置允许客户端处于非活动状态,即既不发送数据也不接收数据的时间
    
    timeout server          600s				#设置服务器超时时间,即允许服务器处于既不接收也不发送数据的非活动时间
    
    timeout http-keep-alive 60s					#session 会话保持超时时间,此时间段内会转发到相同的后端服务器
    
    timeout check           10s					#指定后端服务器健康检查的超时时间
    
    maxconn                 3000
	
	default-server inter 1000 weight 3			
3.3.2.3 Proxies配置-frontend

frontend 配置参数:

复制代码
bind:指定HAProxy的监听地址,可以是IPV4或IPV6,可以同时监听多个IP或端口,可同时用于listen字段中

#格式:
bind [<address>]:<port_range> [, ...] [param*]
#注意:如果需要绑定在非本机的IP,需要开启内核参数:net.ipv4.ip_nonlocal_bind=1

backlog <backlog> #针对所有server配置,当前端服务器的连接数达到上限后的后援队列长度,注意:不支持backend

frontend 配置示例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

frontend lee-webserver-80		
    bind 172.25.254.100:80
    mode http
    use_backend lee-webserver-80-RS	 #调用backend的名称
    
...下面内容省略...
3.3.2.4 Proxies配置-backend

定义一组后端服务器,backend服务器将被frontend进行调用。

注意: backend 的名称必须唯一,并且必须在listen或frontend中事先定义才可以使用,否则服务无法启动

mode http|tcp #指定负载协议类型,和对应的frontend必须一致

option #配置选项

server #定义后端real server,必须指定IP和端口

注意:option后面加 httpchk,smtpchk,mysql-check,pgsql-check,ssl-hello-chk方法,可用于实现更多应用层检测功能。

server 配置

复制代码
#针对一个server配置
check 			#对指定real进行健康状态检查,如果不加此设置,默认不开启检查,只有check后面没有其它配置也可以启用检查功能
 				#默认对相应的后端服务器IP和端口,利用TCP连接进行周期性健康性检查,注意必须指定端口才能实现健康性检查

addr <IP>       #可指定的健康状态监测IP,可以是专门的数据网段,减少业务网络的流量
port <num>      #指定的健康状态监测端口
inter <num>     #健康状态检查间隔时间,默认2000 ms
fall <num>      #后端服务器从线上转为线下的检查的连续失效次数,默认为3
rise <num>      #后端服务器从下线恢复上线的检查的连续有效次数,默认为2
weight <weight> #默认为1,最大值为256,0(状态为蓝色)表示不参与负载均衡,但仍接受持久连接

backup          #将后端服务器标记为备份状态,只在所有非备份主机down机时提供服务,类似Sorry Server

disabled        #将后端服务器标记为不可用状态,即维护状态,除了持久模式
				#将不再接受连接,状态为深黄色,优雅下线,不再接受新用户的请求
				
redirect prefix http://www.baidu.com/   #将请求临时(302)重定向至其它URL,只适用于http模式

maxconn <maxconn>                       #当前后端server的最大并发连接数

代码示例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

backend lee-webserver-80-RS
    mode http
    server web1 192.168.0.101:80 check inter 3s fall 3 rise 5
    server web2 192.168.0.102:80 check inter 3s fall 3 rise 5

...上面内容省略...

测试效果:

复制代码
[Administrator.WIN-20240602BIS] ➤ for N in {1..6};do curl 172.25.254.100;done
RS1 server - 192.168.0.101
RS2 server - 192.168.0.102
RS1 server - 192.168.0.101
RS2 server - 192.168.0.102
RS1 server - 192.168.0.101
RS2 server - 192.168.0.102
3.3.2.5 Proxies配置-listen 简化配置

使用listen替换 frontend和backend的配置方式,可以简化设置,通常只用于TCP协议的应用

listen配置示例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

listen webserver_80
    bind 172.25.254.100:80
    mode http
    option forwardfor
    server webserver1 192.168.0.101:80 check inter 3s fall 3 rise 5
    server webserver2 192.168.0.102:80 check inter 3s fall 3 rise 5


...上面内容省略...

3.4.socat 工具

对服务器动态权重和其它状态可以利用 socat工具进行调整,Socat 是 Linux 下的一个多功能的网络工具,名字来由是Socket CAT,相当于netCAT的增强版.Socat 的主要特点就是在两个数据流之间建立双向通道,且支持众多协议和链接方式。如 IP、TCP、 UDP、IPv6、Socket文件等

范例:利用工具socat 对服务器动态权重调整

复制代码
#修改配置文件
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
stats socket /var/lib/haproxy/stats mode 600 level admin

#查看帮助
haproxy ~]# socat -h
haproxy ~]# echo "help" | socat stdio /var/lib/haproxy/stats
The following commands are valid at this level:
help           : this message
prompt         : toggle interactive mode with prompt
quit           : disconnect
 。。。省略 。。。
enable server : enable a disabled server (use 'set server' instead)   #启用服务器
set maxconn server : change a server's maxconn setting
set server     : change a server's state, weight or address           #设置服务器  	
get weight     : report a server's current weight 					  #查看权重
set weight     : change a server's weight (deprecated)				  #设置权重
show startup-logs : report logs emitted during HAProxy startup
how peers [peers section]: dump some information about all the peers or this peers section
set maxconn global : change the per-process maxconn setting
set rate-limit : change a rate limiting value
set severity-output [none|number|string] : set presence of severity level in
feedback information
set timeout   : change a timeout setting
show env [var] : dump environment variables known to the process
show cli sockets : dump list of cli sockets
show cli level   : display the level of the current CLI session
show fd [num] : dump list of file descriptors in use
 。。。省略 。。。

常用示例:

复制代码
#查看haproxy状态
[root@haproxy ~]# echo "show info" | socat stdio /var/lib/haproxy/stats
Name: HAProxy
Version: 2.4.22-f8e3218
Release_date: 2023/02/14
Nbthread: 1
Nbproc: 1
Process_num: 1
Pid: 33542
Uptime: 0d 0h03m43s
Uptime_sec: 223
Memmax_MB: 0
PoolAlloc_MB: 0

#查看集群状态
[root@haproxy ~]# echo "show servers state" | socat stdio /var/lib/haproxy/stats
1
# be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweight srv_iweight srv_time_since_last_change srv_check_status srv_check_result srv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_id srv_fqdn srv_port srvrecord srv_use_ssl srv_check_port srv_check_addr srv_agent_addr srv_agent_port
2 webcluster 1 web1 172.25.254.20 2 0 2 2 188 6 3 7 6 0 0 0 - 80 - 0 0 - - 0
2 webcluster 2 web2 172.25.254.30 2 0 1 1 188 6 3 7 6 0 0 0 - 80 - 0 0 - - 0
4 static 1 static 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 4331 - 0 0 - - 0
5 app 1 app1 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 5001 - 0 0 - - 0
5 app 2 app2 127.0.0.1 0 0 1 1 187 8 2 0 6 0 0 0 - 5002 - 0 0 - - 0
5 app 3 app3 127.0.0.1 0 0 1 1 186 8 2 0 6 0 0 0 - 5003 - 0 0 - - 0
5 app 4 app4 127.0.0.1 0 0 1 1 186 8 2 0 6 0 0 0 - 5004 - 0 0 - - 0

#查看集群权重
[root@haproxy ~]# echo get weight webcluster/web1 | socat stdio /var/lib/haproxy/stats
2 (initial 2)

[root@haproxy ~]# echo get weight webcluster/web2 | socat stdio /var/lib/haproxy/stats
1 (initial 1)

#设置权重
[root@haproxy ~]# echo "set  weight  webcluster/web1 1 " | socat stdio /var/lib/haproxy/stats

[root@haproxy ~]# echo "set  weight  webcluster/web1 2 " | socat stdio /var/lib/haproxy/stats

#下线后端服务器
[root@haproxy ~]# echo "disable server  webserver_80/webserver1 "  | socat stdio /var/lib/haproxy/stats

#上线后端服务器
[root@haproxy ~]# echo "enable server  webserver_80/webserver1 "  | socat stdio  /var/lib/haproxy/stats

针对多进程处理方法

如果开启多进程那么我们在对进程的sock文件进行操作时其对进程的操作时随机的

如果需要指定操作进程那么需要用多soct文件方式来完成

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

    stats socket /var/lib/haproxy/stats1 mode 600 level admin process 1
    stats socket /var/lib/haproxy/stats2 mode 600 level admin process 2
    nbproc 2
    cpu-map 1 0
    cpu-map 2 1

...上面内容省略...

这样每个进程就会有单独的sock文件来进行单独管理

复制代码
[root@haproxy ~]# ll /var/lib/haproxy/
总用量 0
srw------- 1 root root 0  8月  8 13:43 stats
srw------- 1 root root 0  8月  8 13:46 stats1
srw------- 1 root root 0  8月  8 13:46 stats2

四.haproxy的算法

HAProxy通过固定参数 balance 指明对后端服务器的调度算法

balance参数可以配置在listen或backend选项中。

HAProxy的调度算法分为静态和动态调度算法

有些算法可以根据参数在静态和动态算法中相互转换。

4.1 静态算法

静态算法:按照事先定义好的规则轮询公平调度,不关心后端服务器的当前负载、连接数和响应速度 等,且无法实时修改权重(只能为0和1,不支持其它值),只能靠重启HAProxy生效。

4.1.1 static-rr:基于权重的轮询调度
  • 不支持运行时利用socat进行权重的动态调整(只支持0和1,不支持其它值)

  • 不支持端服务器慢启动

  • 其后端主机数量没有限制,相当于LVS中的 wrr

慢启动是指在服务器刚刚启动上不会把他所应该承担的访问压力全部给它,而是先给一部分,当没问题后在给一部分

示例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance static-rr
    server webserver1 192.168.0.101:80 weight 2 check inter 3s fall 3 rise 5
    server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

...上面内容省略...
4.1.2 first
  • 根据服务器在列表中的位置,自上而下进行调度

  • 其只会当第一台服务器的连接数达到上限,新请求才会分配给下一台服务

  • 其会忽略服务器的权重设置

  • 不支持用socat进行动态修改权重,可以设置0和1,可以设置其它值但无效

示例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance first                 
    server webserver1 192.168.0.101:80 maxconn 3 check inter 3s fall 3 rise 5 
    server webserver2 192.168.0.102:80 check inter 3s fall 3 rise 5


...上面内容省略...

测试效果:

复制代码
 #在两台主机上分别执行此循环,可以观察是否102被调度到
 while true;do curl  172.25.254.100 ; sleep 0.1;done

4.2 动态算法

  • 基于后端服务器状态进行调度适当调整,

  • 新请求将优先调度至当前负载较低的服务器

  • 权重可以在haproxy运行时动态调整无需重启

4.2.1 roundrobin
  1. 基于权重的轮询动态调度算法,

  2. 支持权重的运行时调整,不同于lvs中的rr轮训模式,

  3. HAProxy中的roundrobin支持慢启动(新加的服务器会逐渐增加转发数),

  4. 其每个后端backend中最多支持4095个real server,

  5. 支持对real server权重动态调整,

  6. roundrobin为默认调度算法,此算法使用广泛

示例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance roundrobin                 
    server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5 
    server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5


...上面内容省略...

动态调整权重

复制代码
[root@haproxy ~]# echo "set  weight webserver_80/webserver1 2" | socat stdio  /var/lib/haproxy/haproxy.sock
4.2.2 leastconn
  • leastconn加权的最少连接的动态

  • 支持权重的运行时调整和慢启动,即:根据当前连接最少的后端服务器而非权重进行优先调度(新客户端连接)

  • 比较适合长连接的场景使用,比如:MySQL等场景。

示例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance leastconn
    server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5 
    server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

...上面内容省略...

4.3 其他算法(混合算法)

其它算法即可作为静态算法,又可以通过选项成为动态算法

4.3.1 source

源地址hash,基于用户源地址hash并将请求转发到后端服务器,后续同一个源地址请求将被转发至同一个后端web服务器。此方式当后端服务器数据量发生变化时,会导致很多用户的请求转发至新的后端服务器,默认为静态方式,但是可以通过hash-type支持的选项更改这个算法一般是在不插入Cookie的TCP模式下使用,也可给拒绝会话cookie的客户提供最好的会话粘性,适用于session会话保持但不支持cookie和缓存的场景源地址有两种转发客户端请求到后端服务器的服务器选取计算方式,分别是取模法和一致性hash

示例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance source
    server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5 
    server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

...上面内容省略...

测试:

复制代码
[root@node10 ~]# for N in  {1..6}; do curl 172.25.254.100; done
RS1 server - 192.168.0.101
RS1 server - 192.168.0.101
RS1 server - 192.168.0.101
RS1 server - 192.168.0.101
RS1 server - 192.168.0.101
RS1 server - 192.168.0.101

如果访问客户端时一个家庭,那么所有的家庭的访问流量都会被定向到一台服务器,这时source算法的缺陷

4.3.1.1 map-base 取模法

map-based:取模法,对source地址进行hash计算,再基于服务器总权重的取模,最终结果决定将此请求转发至对应的后端服务器。

此方法是静态的,即不支持在线调整权重,不支持慢启动,可实现对后端服务器均衡调度

缺点是当服务器的总权重发生变化时,即有服务器上线或下线,都会因总权重发生变化而导致调度结果整体改变

hash-type 指定的默值为此算法

tips:

> 所谓取模运算,就是计算两个数相除之后的余数,10%7=3, 7%4=3

> map-based算法:基于权重取模,hash(source_ip)%所有后端服务器相加的总权重

>

>

> 比如当源hash值时1111,1112,1113,三台服务器a b c的权重均为1,

>

> 即abc的调度标签分别会被设定为 0 1 2(1111%3=1,1112%3=2,1113%3=0)

>

> 1111 ----- > nodeb

>

> 1112 ------> nodec

>

> 1113 ------> nodea

>

> 如果a下线后,权重数量发生变化

>

> 1111%2=1,1112%2=0,1113%2=1

>

> 1112和1113被调度到的主机都发生变化,这样会导致会话丢失

取模法配置示例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance source
    server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5 
    server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

...上面内容省略...


#不支持动态调整权重值
[root@haproxy ~]# echo "set weight webserver_80/webserver1 2" | socat stdio /var/lib/haproxy/haproxy.sock
Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.

#只能动态上线和下线
[root@haproxy ~]# echo "set weight webserver_80/webserver1 0" | socat stdio /var/lib/haproxy/haproxy.sock

[root@haproxy ~]# echo "get  weight webserver_80/webserver1" | socat stdio /var/lib/haproxy/haproxy.sock
0 (initial 1)
4.3.1.2 一致性hash

一致性哈希,当服务器的总权重发生变化时,对调度结果影响是局部的,不会引起大的变动hash(o)mod n

该hash算法是动态的,支持使用 socat等工具进行在线权重调整,支持慢启动

算法:

1、后端服务器哈希环点keyA=hash(后端服务器虚拟ip)%(2^32)

2、客户机哈希环点key1=hash(client_ip)%(2^32) 得到的值在[0---4294967295]之间,

3、将keyA和key1都放在hash环上,将用户请求调度到离key1最近的keyA对应的后端服务器

hash环偏斜问题:

增加虚拟服务器IP数量,比如:一个后端服务器根据权重为1生成1000个虚拟IP,再hash。而后端服务器权

重为2则生成2000的虚拟IP,再bash,最终在hash环上生成3000个节点,从而解决hash环偏斜问题

hash对象

一致性hash配置示例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance source
    hash-type consistent
    server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5 
    server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

...上面内容省略...
4.3.2 uri

基于对用户请求的URI的左半部分或整个uri做hash,再将hash结果对总权重进行取模后

根据最终结果将请求转发到后端指定服务器

适用于后端是缓存服务器场景

默认是静态算法,也可以通过hash-type指定map-based和consistent,来定义使用取模法还是一致性hash

注意:此算法基于应用层,所以只支持 mode http ,不支持 mode tcp

4.3.2.1 uri 取模法配置示例
复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...

listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance uri
    server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5 
    server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5

...上面内容省略...
4.3.2.2 uri 一致性hash配置示例
复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
​
listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance uri
    hash-type consistent
    server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5 
    server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5
​
...上面内容省略...
4.3.2.3 访问测试

访问不同的uri,确认可以将用户同样的请求转发至相同的服务器

复制代码
root@rs1 ~]# echo RS1  192.168.0.101 index1 > /var/www/html/index1.html
[root@rs1 ~]# echo RS1  192.168.0.101 index2 > /var/www/html/index2.html
[root@rs1 ~]# echo RS1  192.168.0.101 index3 > /var/www/html/index3.html
[root@rs2 ~]# echo RS1  192.168.0.102 index1 > /var/www/html/index1.html
[root@rs2 ~]# echo RS1  192.168.0.102 index2 > /var/www/html/index2.html
[root@rs2 ~]# echo RS1  192.168.0.102 index3 > /var/www/html/index3.html
​
[root@node10 ~]# curl  172.25.254.100/index.html
RS2 server - 192.168.0.102
[root@node10 ~]# curl  172.25.254.100/index1.html
RS1 192.168.0.101 index1
[root@node10 ~]# curl  172.25.254.100/index2.html
RS1 192.168.0.102 index2
[root@node10 ~]# curl  172.25.254.100/index3.html
RS1 192.168.0.101 index3
4.3.3 url_param

url_param对用户请求的url中的 params 部分中的一个参数key对应的value值作hash计算,并由服务器总权重相除以后派发至某挑出的服务器,后端搜索同一个数据会被调度到同一个服务器,多用与电商

通常用于追踪用户,以确保来自同一个用户的请求始终发往同一个real server

如果无没key,将按roundrobin算法

复制代码
#假设:
url = http://www.timinglee.com/foo/bar/index.php?key=value
​
#则:
host = "www.timinglee.com"
url_param = "key=value"
4.3.3.1 url_param取模法配置示例
复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
​
listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance url_param name,userid       #支持对多个url_param hash
    server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5 
    server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5
​
...上面内容省略...
4.3.3.2 url_param一致性hash配置示例
复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
​
listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance url_param name,userid       #支持对多个url_param hash
    hash-type consistent
    server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5 
    server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5
​
...上面内容省略...
4.3.3.3 测试访问
复制代码
[root@node10 ~]# curl  172.25.254.100/index.html?name=timinglee
RS1 192.168.0.101 index3
[root@node10 ~]# curl  172.25.254.100/index.html?name=timinglee
RS1 192.168.0.101 index3
[root@node10 ~]# curl  172.25.254.100/index.html?userid=lee
RS1 192.168.0.102 index3
[root@node10 ~]# curl  172.25.254.100/index.html?userid=lee
RS1 192.168.0.102 index3
4.3.4 hdr

针对用户每个http头部(header)请求中的指定信息做hash,

此处由 name 指定的http首部将会被取出并做hash计算,

然后由服务器总权重取模以后派发至某挑出的服务器,如果无有效值,则会使用默认的轮询调度。

4.3.4.1 hdr取模法配置示例
复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
​
listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance hdr(User-Agent)
    server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5 
    server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5
​
...上面内容省略...
4.3.4.2 一致性hash配置示例
复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
​
listen webserver_80
    bind 172.25.254.100:80
    mode http
    balance hdr(User-Agent)
    hash-type consistent
    server webserver1 192.168.0.101:80 weight 1 check inter 3s fall 3 rise 5 
    server webserver2 192.168.0.102:80 weight 1 check inter 3s fall 3 rise 5
​
...上面内容省略...
4.3.4.3 测试访问
复制代码
[root@node10 ~]# curl  -v 172.25.254.100
[root@node10 ~]# curl  -vA "firefox" 172.25.254.100
[root@node10 ~]# curl  -vA "sougou" 172.25.254.100
4.3.6 算法总结
复制代码
#静态
static-rr--------->tcp/http  
first------------->tcp/http  
#动态
roundrobin-------->tcp/http 
leastconn--------->tcp/http 
​
#以下静态和动态取决于hash_type是否consistent
source------------>tcp/http
Uri--------------->http
url_param--------->http     
hdr--------------->http
4.3.7 各算法使用场景
复制代码
first           #使用较少
static-rr       #做了session共享的web集群
roundrobin
leastconn       #数据库
source
 
#基于客户端公网IP的会话保持
Uri--------------->http     #缓存服务器,CDN服务商,蓝汛、百度、阿里云、腾讯
url_param--------->http     #可以实现session保持
hdr                         #基于客户端请求报文头部做下一步处理
​

五.高级功能及配置

介绍HAProxy高级配置及实用案例

5.1 基于cookie的会话保持

cookie value:为当前server指定cookie值,实现基于cookie的会话黏性,相对于基于 source 地址hash 调度算法对客户端的粒度更精准,但同时也加大了haproxy负载,目前此模式使用较少, 已经被session共享服务器代替

!NOTE

注意:不支持 tcp mode,使用 http mode

5.1.1 配置选项

cookie name [ rewrite | insert | prefix ][ indirect ] [ nocache ][ postonly ] [ preserve ][ httponly ] [ secure ][ domain ]* [ maxidle <idle> ][ maxlife ]

name: #cookie 的 key名称,用于实现持久连接

insert: #插入新的cookie,默认不插入cookie

indirect: #如果客户端已经有cookie,则不会再发送cookie信息

nocache: #当client和hapoxy之间有缓存服务器(如:CDN)时,不允许中间缓存器缓存cookie,

#因为这会导致很多经过同一个CDN的请求都发送到同一台后端服务器

5.1.2 配置示例
复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
​
listen webserver_80
    bind 172.25.254.100:80
    option forwardfor
    mode http
    balance roundrobin
    cookie WEBCOOKIE insert nocache indirect
    server webserver1 192.168.0.101:80  cookie web1 weight 1 check inter 3s fall 3 rise 5
    server webserver2 192.168.0.102:80  cookie web2 weight 1 check inter 3s fall 3 rise 5
​
...上面内容省略...
5.1.3 验证cookie信息

通过命令行验证:

复制代码
[root@node10 ~]# curl -i 172.25.254.100
HTTP/1.1 200 OK
date: Wed, 10 Jul 2024 16:36:17 GMT
server: Apache/2.4.6 (Red Hat Enterprise Linux) OpenSSL/1.0.2k-fips
last-modified: Thu, 04 Jul 2024 11:18:39 GMT
etag: "1b-61c6a1bd2408d"
accept-ranges: bytes
content-length: 27
content-type: text/html; charset=UTF-8
set-cookie: WEBCOOKIE=web2; path=/
cache-control: private
​
RS2 server - 192.168.0.102
​
[Administrator.WIN-20240602BIS] ➤ curl -i 172.25.254.100
HTTP/1.1 200 OK
server: nginx/1.20.1
date: Wed, 10 Jul 2024 08:36:43 GMT
content-type: text/html
content-length: 18
last-modified: Wed, 10 Jul 2024 04:09:05 GMT
etag: "668e0961-12"
accept-ranges: bytes
set-cookie: WEBCOOKIE=web1; path=/
cache-control: private
​
RS1 192.168.0.101
 
 #curl访问时指定cookie
[root@node10 ~]# curl  -b WEBCOOKIE=web1 172.25.254.100
RS1 server - 192.168.0.101
[root@node10 ~]# curl  -b WEBCOOKIE=web2 172.25.254.100
RS2 server - 192.168.0.102
​
​
[root@node10 ~]# curl  -vb WEBCOOKIE=web1 172.25.254.100
* About to connect() to 172.25.254.100 port 80 (#0)
*   Trying 172.25.254.100...
* Connected to 172.25.254.100 (172.25.254.100) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 172.25.254.100
> Accept: */*
> Cookie: WEBCOOKIE=web1
>
< HTTP/1.1 200 OK
< server: nginx/1.20.1
< date: Wed, 10 Jul 2024 08:38:25 GMT
< content-type: text/html
< content-length: 18
< last-modified: Wed, 10 Jul 2024 04:09:05 GMT
< etag: "668e0961-12"
< accept-ranges: bytes
<
RS1 192.168.0.101
* Connection #0 to host 172.25.254.100 left intact
复制代码

5.2 HAProxy状态页

通过web界面,显示当前HAProxy的运行状态

5.2.1 状态页配置项

stats enable #基于默认的参数启用stats page

stats hide-version #将状态页中haproxy版本隐藏

stats refresh <delay> #设定自动刷新时间间隔,默认不自动刷新

stats uri <prefix> #自定义stats page uri,默认值:/haproxy?stats

stats auth <user>:<passwd> #认证时的账号和密码,可定义多个用户,每行指定一个用户

#默认:no authentication

stats admin { if | unless } <cond> #启用stats page中的管理功能

5.2.2 启用状态页
复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
​
listen stats:
    mode http
    bind 0.0.0.0:8888
    stats enable
    log global
    stats uri /status                   #自定义stats page uri
    stats auth lee:lee                  #认证,此行可以出现多次
​
​
...上面内容省略...
​
测试:
浏览器访问:172.25.254.100:8888/status
5.2.3 登录状态页

#pid为当前pid号,process为当前进程号,nbproc和nbthread为一共多少进程和每个进程多少个线程

pid = 27134 (process #1, nbproc = 1, nbthread = 1)

#启动了多长时间

uptime = 0d 0h00m04s

#系统资源限制:内存/最大打开文件数/

system limits: memmax = unlimited; ulimit-n = 200029

#最大socket连接数/单进程最大连接数/最大管道数maxpipes

maxsock = 200029; maxconn = 100000; maxpipes = 0

#当前连接数/当前管道数/当前连接速率

current conns = 2; current pipes = 0/0; conn rate = 2/sec; bit rate = 0.000 kbps

#运行的任务/当前空闲率

Running tasks: 1/14; idle = 100 %

active UP: #在线服务器

backup UP: #标记为backup的服务器

active UP, going down: #监测未通过正在进入down过程

backup UP, going down: #备份服务器正在进入down过程

active DOWN, going up: #down的服务器正在进入up过程

backup DOWN, going up: #备份服务器正在进入up过程

active or backup DOWN: #在线的服务器或者是backup的服务器已经转换成了down状态

not checked: #标记为不监测的服务器

#active或者backup服务器人为下线的

active or backup DOWN for maintenance (MAINT)

#active或者backup被人为软下线(人为将weight改成0)

active or backup SOFT STOPPED for maintenance

5.2.4 backend server信息

session rate(每秒的连接会话信息): Errors(错误统计信息): cur:每秒的当前会话数量 : Req:错误请求量 max:每秒新的最大会话数量 conn:错误链接量 limit:每秒新的会话限制量 Resp:错误响应量 sessions(会话信息): Warnings(警告统计信息): cur:当前会话量 Retr:重新尝试次数 max:最大会话量 Redis:再次发送次数 limit: 限制会话量 Total:总共会话量 Server(real server信息): LBTot:选中一台服务器所用的总时间 Status:后端机的状态,包括UP和DOWN Last:和服务器的持续连接时间 LastChk:持续检查后端服务器的时间 Wght:权重 Bytes(流量统计): Act:活动链接数量 In:网络的字节输入总量 Bck:备份的服务器数量 Out:网络的字节输出总量 Chk:心跳检测时间 Dwn:后端服务器连接后都是DOWN的数量 Denied(拒绝统计信息): Dwntme:总的downtime时间 Req:拒绝请求量 Thrtle:server 状态 Resp:拒绝回复量

5.3 IP透传

web服务器中需要记录客户端的真实IP地址,用于做访问统计、安全防护、行为分析、区域排行等场景。

5.3.2 四层IP透传
复制代码
#未开启透传的四层代理
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
​
listen webserver_80
    bind 172.25.254.100:80
    mode tcp
    balance roundrobin
    server webserver1 192.168.0.101:80  weight 1 check inter 3s fall 3 rise 5
​
...上面内容省略...
​
#正常的nginx配置
[root@rs1 ~]# vim /etc/nginx/nginx.conf
。。。内容省略。。。
​
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request"'
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
。。。内容省略。。。
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
        。。。内容省略。。。
        }
}
​
​
#在访问haproxy后查看nginx日志
[root@rs1 ~]# tail -n 3 /var/log/nginx/access.log
192.168.0.10 - - [10/Jul/2024:15:21:00 +0800] "GET / HTTP/1.1"200 18 "-" "curl/7.29.0" "-"192.168.0.10 - - [10/Jul/2024:15:26:11 +0800] "GET / HTTP/1.1"200 18 "-" "curl/7.29.0" "-"
在此日志中是无法看到真实访问源地址的

开启四层透传

复制代码
#nginx 配置:在访问日志中通过变量$proxy_protocol_addr 记录透传过来的客户端IP
​
复制代码
[root@rs1 ~]# vim /etc/nginx/nginx.conf
。。。内容省略。。。
​
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request"'
                      ' "$proxy_protocol_addr"'
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
。。。内容省略。。。
    server {
        listen       80 proxy_protocol; #启用此项,将无法直接访问此网站,只能通过四层代理访问
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
        。。。内容省略。。。
        }
}
​
#修改haproxy
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
​
listen webserver_80
    bind 172.25.254.100:80
    mode tcp
    balance roundrobin                 
    server webserver1 192.168.0.101:80 send-proxy weight 1 check inter 3s fall 3 rise 5 
​
...上面内容省略...
​
#查看日志内容
[root@rs1 ~]# tail -n 3 /var/log/nginx/access.log
192.168.0.10 - - [10/Jul/2024:15:21:00 +0800] "GET / HTTP/1.1"200 18 "-" "curl/7.29.0" "-"
192.168.0.10 - - [10/Jul/2024:15:26:11 +0800] "GET / HTTP/1.1"200 18 "-" "curl/7.29.0" "-"
192.168.0.10 - - [10/Jul/2024:15:41:56 +0800] "GET / HTTP/1.1" "172.25.254.10"200 18 "-" "curl/7.29.0"
5.3.3 七层IP透传

当haproxy工作在七层的时候,也可以透传客户端真实IP至后端服务器

5.3.3.1 HAProxy配置

在由haproxy发往后端主机的请求报文中添加"X-Forwarded-For"首部,其值为前端客户端的地址;用于向后端主发送真实的客户端IP

复制代码
option forwardfor [ except <network> ] [ header <name> ] [ if-none ]
​
[ except <network> ]:请求报请来自此处指定的网络时不予添加此首部,如haproxy自身所在网络
[ header <name> ]:   使用自定义的首部名称,而非“X-Forwarded-For",示例:X-client
[ if-none ]           如果没有首部才添加首部,如果有使用默认值
​

示例:

#修改haproxy

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
​
listen webserver_80
    option forwardfor
   bind 172.25.254.100:80
   mode http
   balance roundrobin                
   server webserver1 192.168.0.101:80 send-proxy weight 1 check inter 3s fall 3 rise 5
   server webserver1 192.168.0.102:80  weight 1 check inter 3s fall 3 rise 5
​
...上面内容省略...
5.3.3.2 web服务器日志格式配置

配置web服务器,记录负载均衡透传的客户端IP地址

复制代码
#apache 配置:
LogFormat "%{X-Forwarded-For}i %a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
​
#nginx 日志格式:
$proxy_add_x_forwarded_for: 包括客户端IP和中间经过的所有代理的IP
$http_x_forwarded_For:      只有客户端IP
​
log_format main  '"$proxy_add_x_forwarded_for" - $remote_user [$time_local] 
"$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" $http_x_forwarded_For';
​
​
​
#查看日志如下:
[root@rs1 ~]# tail -n 3 /var/log/nginx/access.log
"172.25.254.10, 192.168.0.10" 192.168.0.10 - - [10/Jul/2024:16:15:00 +0800] "GET / HTTP/1.1"200 18 "-" "curl/7.29.0" "172.25.254.10"
​
[root@rs2 ~]# tail -n 3  /etc/httpd/logs/access_log
172.25.254.10 192.168.0.10 - - [11/Jul/2024:00:15:00 +0800] "GET / HTTP/1.1" 200 27 "-" "curl/7.29.0"
复制代码

5.4 ACL

访问控制列表(ACL,Access Control Lists)

是一种基于包过滤的访问控制技术

它可以根据设定的条件对经过服务器传输的数据包进行过滤(条件匹配)即对接收到的报文进行匹配和过滤,基于请求报文头部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息内容进行匹配并执行进一步操作,比如允许其通过或丢弃。

复制代码
#示例
frontend test_acl
    bind *:80
    mode http
    #acl bad_browers hdr_beg(User-Agent) -i curl
    #http-request deny  if bad_browers
​
    #acl test hdr_dom(host) -i www.timinglee.org
    #acl test hdr_end(host) -i .org
    #acl test base_sub -m sub  org
    #acl test path_sub -m sub  /a
    #acl test path_end -m sub  /a
    #acl test path_reg -i ^/t
    acl test url_sub -m sub lee
    acl  test path_dir -m sub a
    use_backend  test_web if test
​
    default_backend default_webserver
​
​
backend default_webserver
    mode http
    server web1 172.25.254.20:80 check inter 3 fall 3 rise 5
​
backend test_web
    mode http
    server web2 172.25.254.30:80 check inter 3 fall 3 rise 5
​
5.4.1 ACL配置选项

#用acl来定义或声明一个acl

acl <aclname> <criterion> [flags] [operator] [<value>]

acl 名称 匹配规范 匹配模式 具体操作符 操作对象类型

5.4.1.1 ACL-Name 名称

acl test path_end -m sub /a

#ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,并且严格区分大小写,比如:my_acl和My_Acl就是两个完全不同的acl5.8.1.2 ACL-criterion

5.4.1.2 ACL-criterion 匹配规范

定义ACL匹配规范,即:判断条件

hdr string,提取在一个HTTP请求报文的首部

hdr([<name> [,<occ>]]):完全匹配字符串,header的指定信息,<occ> 表示在多值中使用的值的出现次数

hdr_beg([<name> [,<occ>]]):前缀匹配,header中指定匹配内容的begin

hdr_end([<name> [,<occ>]]):后缀匹配,header中指定匹配内容end

hdr_dom([<name> [,<occ>]]):域匹配,header中的dom(host)

hdr_dir([<name> [,<occ>]]):路径匹配,header的uri路径

hdr_len([<name> [,<occ>]]):长度匹配,header的长度匹配

hdr_reg([<name> [,<occ>]]):正则表达式匹配,自定义表达式(regex)模糊匹配

hdr_sub([<name> [,<occ>]]):子串匹配,header中的uri模糊匹配 模糊匹配c 报文中a/b/c也会匹配

#示例:

hdr(<string>) 用于测试请求头部首部指定内容

hdr_dom(host) 请求的host名称,如 www.timinglee.org

hdr_beg(host) 请求的host开头,如 www. img. video. download. ftp.

hdr_end(host) 请求的host结尾,如 .com .net .cn

#示例:

acl bad_agent hdr_sub(User-Agent) -i curl wget

http-request deny if bad_agent

#有些功能是类似的,比如以下几个都是匹配用户请求报文中host的开头是不是www

acl short_form hdr_beg(host) www.

acl alternate1 hdr_beg(host) -m beg www.

acl alternate2 hdr_dom(host) -m beg www.

acl alternate3 hdr(host) -m beg www.

base : string

#返回第一个主机头和请求的路径部分的连接,该请求从主机名开始,并在问号之前结束,对虚拟主机有用

<scheme>://<user>:<password>@#<host>:<port>/<path>;<params>#?<query>#<frag>

base : exact string match

base_beg : prefix match

base_dir : subdir match

base_dom : domain match

base_end : suffix match

base_len : length match

base_reg : regex match

base_sub : substring match

path : string

#提取请求的URL路径,该路径从第一个斜杠开始,并在问号之前结束(无主机部分)

<scheme>://<user>:<password>@<host>:<port>#/<path>;<params>#?<query>#<frag>

path : exact string match

path_beg : prefix match #请求的URL开头,如/static、/images、/img、/css

path_end : suffix match #请求的URL中资源的结尾,如 .gif .png .css .js .jpg .jpeg

path_dom : domain match

path_dir : subdir match

path_len : length match

path_reg : regex match

path_sub : substring match

#示例:

path_beg -i /haproxy-status/

path_end .jpg .jpeg .png .gif

path_reg ^/images.*\.jpeg$

path_sub image

path_dir jpegs

path_dom timinglee

url : string

#提取请求中的整个URL。

url :exact string match

url_beg : prefix match

url_dir : subdir match

url_dom : domain match

url_end : suffix match

url_len : length match

url_reg : regex match

url_sub : substring match

dst #目标IP

dst_port #目标PORT

src #源IP

src_port #源PORT

#示例:

acl invalid_src src 10.0.0.7 192.168.1.0/24

acl invalid_src src 172.16.0.0/24

acl invalid_port src_port 0:1023

status : integer #返回在响应报文中的状态码

#七层协议

acl valid_method method GET HEAD

http-request deny if ! valid_method

5.4.1.3 ACL-flags 匹配模式

ACL匹配模式

-i 不区分大小写

-m 使用指定的正则表达式匹配方法

-n 不做DNS解析

-u 禁止acl重名,否则多个同名ACL匹配或关系

5.4.1.4 ACL-operator 具体操作符

ACL 操作符

整数比较:eq、ge、gt、le、lt

字符比较:

  • exact match (-m str) :字符串必须完全匹配模式

  • substring match (-m sub) :在提取的字符串中查找模式,如果其中任何一个被发现,ACL将匹配

  • prefix match (-m beg) :在提取的字符串首部中查找模式,如果其中任何一个被发现,ACL将匹配

  • suffix match (-m end) :将模式与提取字符串的尾部进行比较,如果其中任何一个匹配,则ACL进行匹配

  • subdir match (-m dir) :查看提取出来的用斜线分隔("/")的字符串,如其中任一个匹配,则ACL进行匹配

  • domain match (-m dom) :查找提取的用点(".")分隔字符串,如果其中任何一个匹配,则ACL进行匹配

5.4.1.5 ACL-value 操作对象

value的类型

The ACL engine can match these types against patterns of the following types :

  • Boolean #布尔值

  • integer or integer range #整数或整数范围,比如用于匹配端口范围

  • IP address / network #IP地址或IP范围, 192.168.0.1 ,192.168.0.1/24

  • string--> www.timinglee.org

exact #精确比较

substring #子串

suffix #后缀比较

prefix #前缀比较

subdir #路径, /wp-includes/js/jquery/jquery.js

domain #域名,www.timinglee.org

  • regular expression #正则表达式

  • hex block #16进制

5.4.2 多个ACL的组合调用方式

多个ACL的逻辑处理

与:隐式(默认)使用

或:使用"or" 或 "||"表示

否定:使用 "!" 表示

多个ACL调用方式:

#示例:

if valid_src valid_port #与关系,ACL中A和B都要满足为true,默认为与

if invalid_src || invalid_port #或,ACL中A或者B满足一个为true

if ! invalid_src #非,取反,不满足ACL才为true

5.4.3 ACL示例-域名匹配
复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
frontend testacl
    bind :80
    mode http
​
    ###########     ACL settings    #######################
    acl web_host hdr_dom(host) www.timinglee.org
​
    ###########     host        ###########################
    use_backend timinglee_host if web_host
​
    ###########     default server      ###################
    default_backend default_webserver
​
​
backend timinglee_host
    mode http
    server web1 192.168.0.101:80 check weight 1 inter 3s fall 3 rise 5
    server web2 192.168.0.102:80 check weight 1 inter 3s fall 3 rise 5
​
backend default_webserver
    mode http
    server web1 172.25.254.10:80 check weight 1 inter 3s fall 3 rise 5
​
​
...上面内容省略...
复制代码

测试结果:

复制代码
#在浏览器所在主机中做地址解析
复制代码
[root@node10 html]# vim /etc/hosts
172.25.254.100   www.timinglee.org
​
​
#测试结果
[root@node10 html]# curl  www.timinglee.org
RS1 192.168.0.101
[root@node10 html]# curl  www.timinglee.org
RS2 server - 192.168.0.102
[root@node10 html]# curl  172.25.254.100
default web server node10
​
复制代码
5.4.4 ACL示例-基于源IP或子网调度访问

将指定的源地址调度至指定的web服务器组。

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
frontend testacl
    bind :80
    mode http
​
    ###########     ACL settings    #######################
    acl ip_test src 172.25.254.1 192.168.0.0/24
​
    ###########     host        ###########################
    use_backend ip_test-host if ip_test
​
    ###########     default server      ###################
    default_backend default_webserver
​
​
backend ip_test-host
    mode http
    server web1 192.168.0.101:80 check weight 1 inter 3s fall 3 rise 5
​
backend default_webserver
    mode http
    server web1 172.25.254.10:80 check weight 1 inter 3s fall 3 rise 5
复制代码

测试结果

复制代码
[172.25.254.10 root@node10 html]# curl  172.25.254.100
default web server node10
​
[172.25.254.1 Administrator.WIN-20240602BIS] ➤ curl 172.25.254.100
RS1 192.168.0.101
​
[192.168.0.102 root@rs1 ~]# curl  192.168.0.101
RS1 192.168.0.101
复制代码
5.4.5 ACL示例-基于源地址的访问控制

拒绝指定IP或者IP范围访问

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
frontend testacl
    bind :80
    mode http
​
    ###########     ACL settings    #######################
    acl web_host hdr_dom(host) www.timinglee.org
    acl ip_test src 172.25.254.1 192.168.0.0/24
​
​
    ###########     host        ###########################
    http-request deny if web_host
​
    ###########     default server      ###################
    default_backend default_webserver
​
​
backend ip_test-host
    mode http
    server web1 192.168.0.101:80 check weight 1 inter 3s fall 3 rise 5
​
backend default_webserver
    mode http
    server web1 172.25.254.10:80 check weight 1 inter 3s fall 3 rise 5
复制代码

测试

复制代码
[root@node10 ~]# curl www.timinglee.org
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
​
​
[root@node10 ~]# curl 172.25.254.100
default web server node10
5.4.6 ACL示例-匹配浏览器类型

匹配客户端浏览器,将不同类型的浏览器调动至不同的服务器组、

范例: 拒绝curl和wget的访问

示例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
frontend testacl
    bind :80
    mode http
​
    ###########     ACL settings    #######################
    acl user_agent_block hdr_sub(User-Agent) -i curl wget
    acl user_agent_redirect hdr_sub(User-Agent) -i Mozilla/5.0
​
​
    ###########     host        ###########################
    http-request deny if user_agent_block
    redirect prefix https://www.baidu.com if user_agent_redirect
​
    ###########     default server      ###################
    default_backend default_webserver
​
​
backend ip_test-host
    mode http
    server web1 192.168.0.101:80 check weight 1 inter 3s fall 3 rise 5
​
backend default_webserver
    mode http
    server web1 172.25.254.10:80 check weight 1 inter 3s fall 3 rise 5
复制代码

测试:

复制代码
1。
[root@node10 ~]# curl 172.25.254.100
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
​
2.
[root@node10 ~]# wget http://172.25.254.100/index.html
--2024-07-11 23:04:36--  http://172.25.254.100/index.html
Connecting to 172.25.254.100:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2024-07-11 23:04:36 ERROR 403: Forbidden.
​
3.浏览器中测试
5.4.7 ACL示例-基于文件后缀名实现动静分离
复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
frontend testacl
    bind :80
    mode http
​
    ###########     ACL settings    #######################
    acl url_static  path_end -i .jpg .png .css .js .html
    acl url_php     path_end -i .php
​
​
    ###########     host        ###########################
    use_backend static_host if url_static
    use_backend php_host if url_php
​
    ###########     default server      ###################
    default_backend default_webserver
​
​
backend static_host
    mode http
    server web2 192.168.0.101:80 check weight 1 inter 3s fall 3 rise 5
​
backend php_host
    mode http
    server web1 192.168.0.102:80 check weight 1 inter 3s fall 3 rise 5
​
backend default_webserver
    mode http
    server web1 172.25.254.10:80 check weight 1 inter 3s fall 3 rise 5
复制代码

测试:

复制代码
[root@rs1 ~]# echo css 192.168.0.101 >  /usr/share/nginx/html/index.css
[root@rs2 ~]# echo php 192.168.0.102  > /var/www/html/index.php
​
[root@node10 ~]# curl 172.25.254.100/index.php
php 192.168.0.102
[root@node10 ~]# curl 172.25.254.100/index.css
css 192.168.0.101
​
5.4.8 ACL-匹配访问路径实现动静分离
复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
frontend testacl
    bind :80
    mode http
​
    ###########     ACL settings    #######################
    acl url_static  path_end -i .jpg .png .css .js .html
    acl url_static  path_end -m sub /static /images /javascript
    acl acl_app     path_beg -m sub /api
​
​
​
    ###########     host        ###########################
    use_backend static_host if url_static
    use_backend api_host if acl_app
​
​
    ###########     default server      ###################
    default_backend default_webserver
​
​
backend static_host
    mode http
    server web2 192.168.0.101:80 check weight 1 inter 3s fall 3 rise 5
​
backend api_host
    mode http
    server web1 192.168.0.102:80 check weight 1 inter 3s fall 3 rise 5
​
backend default_webserver
    mode http
    server web1 172.25.254.10:80 check weight 1 inter 3s fall 3 rise 5
​
复制代码
#创建相关文件
复制代码
[root@rs1 ~]# mkdir /usr/share/nginx/html/static
[root@rs1 ~]# echo static 192.168.0.101 > /usr/share/nginx/html/static/index.html
[root@rs2 ~]# mkdir  /var/www/html/api/
[root@rs2 ~]# echo api 192.168.0.102 > /var/www/html/api/index.html
​
复制代码
#测试访问
复制代码
[root@node10 ~]# curl 172.25.254.100/api/
api 192.168.0.102
[root@node10 ~]# curl 172.25.254.100/static/
static 192.168.0.101

5.5 自定义HAProxy 错误界面

对指定的报错进行重定向,进行优雅的显示错误页面

使用errorfile和errorloc指令的两种方法,可以实现自定义各种错误页面

复制代码
#haproxy默认使用的错误错误页面
复制代码
[root@haproxy ~]# rpm -ql  haproxy24z-2.4.27-1.el7.zenetys.x86_64 | grep -E http$
/usr/share/haproxy/400.http
/usr/share/haproxy/403.http
/usr/share/haproxy/408.http
/usr/share/haproxy/500.http
/usr/share/haproxy/502.http
/usr/share/haproxy/503.http
/usr/share/haproxy/504.http
复制代码
5.5.1 基于自定义的错误页面文件

#自定义错误页

errorfile <code> <file>

<code> #HTTP status code.支持200, 400, 403, 405, 408, 425, 429, 500, 502,503,504

<file> #包含完整HTTP响应头的错误页文件的绝对路径。 建议后缀".http",以和一般的html文件相区分

#示例:

errorfile 503 /haproxy/errorpages/503page.http

示例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
defaults
    mode                    http
​
    ...内容省略...
​
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 1000000
​
    errorfile 503 /haproxy/errorpages/503page.http
​
[root@haproxy ~]# mkdir /haproxy/errorpages/ -p
[root@haproxy ~]# cp /usr/share/haproxy/503.http /haproxy/errorpages/503page.http
​
[root@haproxy ~]# vim /haproxy/errorpages/503page.http
HTTP/1.0 503 Service Unavailable
Cache-Control: no-cache
Connection: close
Content-Type: text/html;charset=UTF-8^M
​
<html><body><h1>什么动物生气最安静</h1>
大猩猩!!
</body></html>

测试:

关闭后端的RS主机

然后用浏览器去访问172.25.254.100

5.5.2 基于http重定向错误页面

#错误页面重定向

errorloc <code> <url>

#相当于errorloc302 <code> <url>,利用302重定向至指URL

#示例:

errorloc 503 https://www.baidu.com

范例:

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
defaults
    mode                    http
​
    ...内容省略...
​
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 1000000
​
    errorloc 503 https://www.baidu.com
​

#浏览器访问172.25.254.100 自动跳转到百度

5.6 HAProxy 四层负载

针对除HTTP以外的TCP协议应用服务访问的应用场景

MySQL

Redis

Memcache

RabbitMQ

四层负载示例

注意:如果使用frontend和backend,一定在 frontend 和 backend 段中都指定mode tcp

listen mysql-port

bind 10.0.0.7:6379

mode tcp

balance leastconn

server server1 10.0.0.17:3306 check

server server2 10.0.0.27:3306 check backup

范例:对 MySQL 服务实现四层负载

复制代码
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
listen mysql_port
    bind :3306
    mode tcp
    balance leastconn
    server mysql1 192.168.0.101:3306 check
    server mysql2 192.168.0.102:3306 check
​
​
#或者使用frontend和backend实现
haproxy ~]# vim /etc/haproxy/haproxy.cfg
...上面内容省略...
frontend mysql_port
    bind :3306
    mode tcp
    use_backend mysql_rs
​
backend mysql_rs
    mode tcp
    balance leastconn
    server mysql1 192.168.0.101:3306 check
    server mysql2 192.168.0.102:3306 check
​
​
haproxy ~]# systemctl restart haproxy.service
​
​
#在后端服务器安装和配置mariadb服务
rs1 ~]# yum install mariadb-server  -y
rs2 ~]# yum install mariadb-server  -y
​
rs1 ~]# vim /etc/my.cnf
[mysqld]
server-id=1 #在另一台主机为
​
rs2 ~]# vim /etc/my.cnf
[mysqld]
server-id=2 #在另一台主机为
​
rs1 ~]# systemctl start mariadb
rs2 ~]# systemctl start mariadb
​
rs1 ~]# mysql -e "grant all on *.* to lee@'%' identified by 'lee';"
rs2 ~]# mysql -e "grant all on *.* to lee@'%' identified by 'lee';"

#测试

复制代码
[root@node10 ~]# mysql -ulee -plee   -h 172.25.254.100 -e "show variables like 'hostname'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| hostname      | rs2   |
+---------------+-------+
[root@node10 ~]# mysql -ulee -plee   -h 172.25.254.100 -e "show variables like 'hostname'"
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| hostname      | rs1   |
+---------------+-------+
[root@node10 ~]# mysql -ulee -plee   -h172.25.254.100 -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
|           1 |
+-------------+
[root@node10 ~]# mysql -ulee -plee   -h172.25.254.100 -e "select @@server_id"
+-------------+
| @@server_id |
+-------------+
|           2 |
+-------------+
复制代码

5.7 HAProxy https 实现

haproxy可以实现https的证书安全,从用户到haproxy为https,从haproxy到后端服务器用http通信

但基于性能考虑,生产中证书都是在后端服务器比如nginx上实现

#配置HAProxy支持https协议,支持ssl会话;

bind *:443 ssl crt /PATH/TO/SOME_PEM_FILE

#指令 crt 后证书文件为PEM格式,需要同时包含证书和所有私钥

cat demo.key demo.crt > demo.pem

#把80端口的请求重向定443

bind *:80

redirect scheme https if !{ ssl_fc }

5.7.1 证书制作
复制代码
haproxy ~]# mkdir   /etc/haproxy/certs/
​
haproxy ~]#  openssl req -newkey rsa:2048 \ 
-nodes -sha256 --keyout /etc/haproxy/certs/timinglee.org.key \ 
-x509 -days 365 -out /etc/haproxy/certs/timinglee.org.crt
​
5.7.2 https配置示例
复制代码
haproxy ~]#  vim /etc/haproxy/haproxy.cfg
frontend webserver
    bind *:80
    redirect scheme https if !{ ssl_fc }
    mode http
    use_backend webcluster
frontend webserver-https
    bind *:443 ssl  crt  /etc/haproxy/timinglee.org.pem
    mode http
    use_backend webcluster
backend  webcluster
    mode http
    balance roundrobin
    server web1 172.25.254.200:80 check inter 3s fall 3 rise 5
    server web2 172.25.254.201:80 check inter 3s fall 3 rise 5
复制代码
复制代码
[root@客户端 ~]#curl -IkL http://172.25.254.100
HTTP/1.1 302 Found
content-length: 0
location: https://www.timinglee.org/
cache-control: no-cache
HTTP/1.1 200 OK
date: Sat, 04 Apr 2020 02:31:31 GMT
server: Apache/2.4.6 (CentOS) PHP/5.4.16
last-modified: Thu, 02 Apr 2020 01:44:13 GMT
etag: "a-5a244f01f8adc"
accept-ranges: bytes
content-length: 10
content-type: text/html; charset=UTF-8
[root@centos6 ~]#curl -Ik https://www.timinglee.org
HTTP/1.1 200 OK
date: Sat, 04 Apr 2020 02:31:50 GMT
server: Apache/2.4.6 (CentOS) PHP/5.4.16
last-modified: Thu, 02 Apr 2020 01:44:28 GMT
etag: "a-5a244f0fd5175"
accept-ranges: bytes
content-length: 10
content-type: text/html; charset=UTF-8
​

5.8测试页面

六,Haproxy全站加密实验

1.制作证书

复制代码
[root@haproxy ~]# mkdir /etc/haproxy/certs/
[root@haproxy ~]# openssl req -newkey rsa:2048 -nodes -sha256  -keyout /etc/haproxy/certs/timinglee.org.key -x509 -days 365 -out /etc/haproxy/certs/timinglee.org.crt


You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Shaanxi
Locality Name (eg, city) [Default City]:Xi'an
Organization Name (eg, company) [Default Company Ltd]:timinglee
Organizational Unit Name (eg, section) []:linux
Common Name (eg, your name or your server's hostname) []:www.timinglee.org
Email Address []:admin@timinglee.org
[root@haproxy ~]# ls /etc/haproxy/certs/
timinglee.org.crt  timinglee.org.key

[root@haproxy ~]# cat /etc/haproxy/certs/timinglee.org.{key,crt} > /etc/haproxy/certs/timinglee.pem

2.全站加密

复制代码
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster-http
    bind        *:80
    redirect scheme https if ! { ssl_fc }

listen webcluster-https
    bind        *:443 ssl  crt /etc/haproxy/certs/timinglee.pem
    mode        http
    balance     roundrobin
    server haha 192.168.0.10:80  check inter 3s fall 3 rise 5 weight 1
    server hehe 192.168.0.20:80  check inter 3s fall 3 rise 5 weight 1
    
    
[root@haproxy ~]# systemctl restart haproxy.service

#测试:
[Administrator.DESKTOP-VJ307M3] ➤ curl -v -k -L http://172.25.254.100
*   Trying 172.25.254.100:80...
* TCP_NODELAY set
* Connected to 172.25.254.100 (172.25.254.100) port 80 (#0)
> GET / HTTP/1.1
> Host: 172.25.254.100
> User-Agent: curl/7.65.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 302 Found
< content-length: 0
< location: https://172.25.254.100/					#转换信息体现
< cache-control: no-cache
<
* Connection #0 to host 172.25.254.100 left intact
* Issue another request to this URL: 'https://172.25.254.100/'
*   Trying 172.25.254.100:443...
* TCP_NODELAY set
* Connected to 172.25.254.100 (172.25.254.100) port 443 (#1)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server did not agree to a protocol
* Server certificate:
*  subject: C=CN; ST=Shaanxi; L=Xi'an; O=timinglee; OU=linux; CN=www.timinglee.org; emailAddress=admin@timinglee.org
*  start date: Jan 26 08:38:57 2026 GMT
*  expire date: Jan 26 08:38:57 2027 GMT
*  issuer: C=CN; ST=Shaanxi; L=Xi'an; O=timinglee; OU=linux; CN=www.timinglee.org; emailAddress=admin@timinglee.org
*  SSL certificate verify result: self signed certificate (18), continuing anyway.
> GET / HTTP/1.1
> Host: 172.25.254.100
> User-Agent: curl/7.65.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< date: Mon, 26 Jan 2026 08:48:34 GMT
< server: Apache/2.4.62 (Red Hat Enterprise Linux)
< last-modified: Fri, 23 Jan 2026 03:52:02 GMT
< etag: "1a-64906147d3d6a"
< accept-ranges: bytes
< content-length: 26
< content-type: text/html; charset=UTF-8
<
webserver2 - 192.168.0.20
* Connection #1 to host 172.25.254.100 left intact

七,Haproxy算法实验

1.static-rr

复制代码
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind        *:80
    balance     static-rr
    server haha 192.168.0.10:80 check inter 3s fall 3 rise 5 weight 2
    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1


[root@haproxy ~]# systemctl restart haproxy.service

#测试
[Administrator.DESKTOP-VJ307M3] ➤ for i in {1..10}; do curl 172.25.254.100; done
webserver1 - 192.168.0.10
webserver1 - 192.168.0.10
webserver2 - 192.168.0.20
webserver1 - 192.168.0.10
webserver1 - 192.168.0.10
webserver2 - 192.168.0.20
webserver1 - 192.168.0.10
webserver1 - 192.168.0.10
webserver2 - 192.168.0.20
webserver1 - 192.168.0.10


#检测是否支持热更新
[root@haproxy ~]# echo "get  weight webcluster/haha" | socat  stdio /var/lib/haproxy/stats
2 (initial 2)

[root@haproxy ~]# echo "set  weight  webcluster/haha 1  " | socat stdio /var/lib/haproxy/stats       Backend is using a static LB algorithm and only accepts weights '0%' and '100%'

2.first

复制代码
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
    bind        *:80
    balance     first
    server haha 192.168.0.10:80 maxconn 1 check inter 3s fall 3 rise 5 weight 2
    server hehe 192.168.0.20:80 check inter 3s fall 3 rise 5 weight 1


[root@haproxy ~]# systemctl restart haproxy.service


#测试:在一个shell中执行持续访问
[Administrator.DESKTOP-VJ307M3] ➤ while true; do curl 172.25.254.100; done
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
.... .....

#在其他设立了中建立持续访问并观察
Administrator.DESKTOP-VJ307M3] ➤  while true; do curl 172.25.254.100; done
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver2 - 192.168.0.20
webserver1 - 192.168.0.10						#此处出现10信息
webserver2 - 192.168.0.20

八,HAProxy状态页

复制代码
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen stats
    mode        http
    bind 0.0.0.0:4321
    stats       enable
    log         global
#   stats       refresh
    stats uri   /status
    stats auth  lee:lee
[root@haproxy ~]# systemctl restart haproxy.service

登录测试:

开启自动刷新

复制代码
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen stats
    mode        http
    bind 0.0.0.0:4321
    stats       enable
    log         global
    stats       refresh   1
    stats uri   /status
    stats auth  lee:lee
[root@haproxy ~]# systemctl restart haproxy.service

模拟设备下线

九,Harproxy的四层负载

1.环境设定

复制代码
#部署mariadb数据库
[root@webserver1+2 ~]# dnf install mariadb-server mariadb  -y
[root@webserver1+1 ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server_id=10			#设定数据库所在主机的id标识,在20上设定id为20
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mariadb/mariadb.log
pid-file=/run/mariadb/mariadb.pid


#建立远程登录用户并授权
[root@webserver2+1 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>  CREATE USER 'lee'@'%' identified by 'lee';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> CREATE USER 'lee'@'%' identified by 'lee';
MariaDB [(none)]> GRANT ALL ON *.* TO 'lee'@'%';
Query OK, 0 rows affected (0.000 sec)

#测试
[root@webserver2 ~]# mysql -ulee -plee -h 192.168.0.20   #测试10时修改ip即可
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

2.四层负载操作

复制代码
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen mariadbcluster
    bind        *:6663
    mode        tcp
    balance     roundrobin
    server haha 192.168.0.10:3306  check inter 3s fall 3 rise 5 weight 1
    server hehe 192.168.0.20:3306  check inter 3s fall 3 rise 5 weight 1
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

#检测端口
[root@haproxy ~]# netstat -antlupe  | grep haproxy
tcp        0      0 0.0.0.0:6663            0.0.0.0:*               LISTEN      0          44430      2136/haproxy
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          44429      2136/haproxy
tcp        0      0 0.0.0.0:4321            0.0.0.0:*               LISTEN      0          44431      2136/haproxy


#测试:
[Administrator.DESKTOP-VJ307M3] ➤ mysql -ulee -plee -h172.25.254.100 -P 6663
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SELECT @@server_id;
+-------------+
| @@server_id |
+-------------+
|          20 |
+-------------+
1 row in set (0.00 sec)

MariaDB [(none)]> quit
Bye
                                                                                                    ✔
─────────────────────────────────────────────────────────────────────────────────────────────────────
[2026-01-26 11:39.31]  ~
[Administrator.DESKTOP-VJ307M3] ➤ mysql -ulee -plee -h172.25.254.100 -P 6663
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SELECT @@server_id;
+-------------+
| @@server_id |
+-------------+
|          10 |
+-------------+
1 row in set (0.00 sec)

MariaDB [(none)]>

backup参数

复制代码
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen mariadbcluster
    bind        *:6663
    mode        tcp
    balance     roundrobin
    server haha 192.168.0.10:3306  check inter 3s fall 3 rise 5 weight 1
    server hehe 192.168.0.20:3306  check inter 3s fall 3 rise 5 weight 1 backup


[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg

#测试
[Administrator.DESKTOP-VJ307M3] ➤ mysql -ulee -plee -h172.25.254.100 -P 6663
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SELECT @@server_id;
+-------------+
| @@server_id |
+-------------+
|          10 |
+-------------+
1 row in set (0.00 sec)

MariaDB [(none)]> quit
Bye

[Administrator.DESKTOP-VJ307M3] ➤ mysql -ulee -plee -h172.25.254.100 -P 6663
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SELECT @@server_id;
+-------------+
| @@server_id |
+-------------+
|          10 |
+-------------+
1 row in set (0.00 sec)

MariaDB [(none)]> quit
Bye


#关闭10的mariadb并等待1分钟
[root@webserver1 ~]# systemctl stop mariadb
[Administrator.DESKTOP-VJ307M3] ➤ mysql -ulee -plee -h172.25.254.100 -P 6663
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 1 "Operation not permitted"    #标识haproxy 没有完成故障转换,需要等待
  
  
[Administrator.DESKTOP-VJ307M3] ➤ mysql -ulee -plee -h172.25.254.100 -P 6663
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SELECT @@server_id;
+-------------+
| @@server_id |
+-------------+
|          20 |
+-------------+
1 row in set (0.00 sec)

MariaDB [(none)]>


#还原故障主机等待片刻
[root@webserver1 ~]# systemctl start mariadb

[Administrator.DESKTOP-VJ307M3] ➤ mysql -ulee -plee -h172.25.254.100 -P 6663
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.27-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SELECT @@server_id;
+-------------+
| @@server_id |
+-------------+
|          10 |
+-------------+
1 row in set (0.00 sec)

MariaDB [(none)]>

十,Haproxy ACL访问控制

1.实验素材

#在浏览器或者curl主机中设定本地解析

在windows中设定解析

复制代码
#在Linux中设定解析
[Administrator.DESKTOP-VJ307M3] ➤ vim /etc/hosts
172.25.254.100  www.timinglee.org     bbs.timinglee.org    news.timinglee.org   login.timinglee.org  www.lee.org   www.lee.com

#测试
ping  bbs.timinglee.org

2.设定基础的haproxy实验配置

复制代码
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
    bind            *:80
    mode            http
    use_backend     webserver-80-web1

backend webserver-80-web1
    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5

backend webserver-80-web2
    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5


[root@haproxy ~]# systemctl restart haproxy.service

3.基础acl示例

复制代码
#在访问的网址中,所有以.com  结尾的访问10,其他访问20
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
    bind            *:80
    mode            http
    
    acl test hdr_end(host) -i .com			#acl列表
    
    use_backend  webserver-80-web1 if test	#acl列表访问匹配
    default_backend webserver-80-web2		#acl列表访问不匹配

backend webserver-80-web1
    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5

backend webserver-80-web2
    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5


#测试
[2026-01-26 15:50.45]  ~
[Administrator.DESKTOP-VJ307M3] ➤ curl www.lee.com
webserver1 - 192.168.0.10
                                                                                                    ✔
─────────────────────────────────────────────────────────────────────────────────────────────────────
[2026-01-26 15:50.45]  ~
[Administrator.DESKTOP-VJ307M3] ➤ curl www.lee.org
webserver2 - 192.168.0.20



#基于访问头部
    acl head hdr_beg(host) -i bbs.
    use_backend  webserver-80-web1 if head
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
    bind            *:80
    mode            http
    
    acl test hdr_end(host) -i .com			#acl列表
    
    acl head hdr_beg(host) -i bbs.
    use_backend  webserver-80-web1 if head
    default_backend webserver-80-web2

backend webserver-80-web1
    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5

backend webserver-80-web2
    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5


#测试效果


#base参数acl
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
    bind            *:80
    mode            http
    
    acl pathdir base_dir -i /lee
    use_backend  webserver-80-web1 if pathdir
    default_backend webserver-80-web2		#acl列表访问不匹配

backend webserver-80-web1
    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5

backend webserver-80-web2
    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5

[root@webserver1+2 ~]# mkdir -p /var/www/html/lee/
[root@webserver1+2 ~]#  mkdir -p /var/www/html/lee/test/


[root@webserver1 ~]# echo lee - 192.168.0.10  > /var/www/html/lee/index.html
[root@webserver1 ~]# echo lee/test - 192.168.0.10 > /var/www/html/lee/test/index.html
[root@webserver2 ~]# echo lee - 192.168.0.20  > /var/www/html/lee/index.html
[root@webserver2 ~]# echo lee/test - 192.168.0.10 > /var/www/html/lee/test/index.html


#测试
[2026-01-26 16:01.56]  ~
[Administrator.DESKTOP-VJ307M3] ➤ curl 172.25.254.100/lee/
lee - 192.168.0.10
                                                                                                    ✔
─────────────────────────────────────────────────────────────────────────────────────────────────────
[2026-01-26 16:01.57]  ~
[Administrator.DESKTOP-VJ307M3] ➤ curl 172.25.254.100/lee/test/
lee/test - 192.168.0.10
                                                                                                    ✔
─────────────────────────────────────────────────────────────────────────────────────────────────────
[2026-01-26 16:02.01]  ~
[Administrator.DESKTOP-VJ307M3] ➤ curl 172.25.254.100/index.html
webserver2 - 192.168.0.20


#acl禁止列表黑名单
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
    bind            *:80
    mode            http
    
    acl test hdr_end(host) -i .com			#acl列表
    
    use_backend  webserver-80-web1 if test	#acl列表访问匹配
    default_backend webserver-80-web2		#acl列表访问不匹配

	acl invalid_src src 172.25.254.1
    http-request deny if invalid_src

backend webserver-80-web1
    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5

backend webserver-80-web2
    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5
    
#测试:
[Administrator.DESKTOP-VJ307M3] ➤ curl 172.25.254.100
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>


#禁止列表白名单
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
frontend webcluster
    bind            *:80
    mode            http
    
    acl test hdr_end(host) -i .com			#acl列表
    
    use_backend  webserver-80-web1 if test	#acl列表访问匹配
    default_backend webserver-80-web2		#acl列表访问不匹配

	acl invalid_src src 172.25.254.1
    http-request deny if ! invalid_src

backend webserver-80-web1
    server web1 192.168.0.10:80 check inter 3s fall 3 rise 5

backend webserver-80-web2
    server web2 192.168.0.20:80 check inter 3s fall 3 rise 5
    
#测试:
[Administrator.DESKTOP-VJ307M3] ➤ curl 172.25.254.100
webserver2 - 192.168.0.20

[root@haproxy ~]# curl  172.25.254.100
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
相关推荐
野指针YZZ1 小时前
Gstreamer插入第三方plugins流程:rgaconvert
linux·音视频·rk3588
快快起来写代码1 小时前
Arrays.asList方法踩坑
linux
JZC_xiaozhong1 小时前
DCS分散控制系统与MES集成:实现制造过程数据贯通的关键路径
大数据·运维·制造·etl工程师·bpm·数据集成与应用集成·业务流程管理
難釋懷1 小时前
基于Redis的Stream结构作为消息队列,实现异步秒杀下单
数据库·redis·缓存
忙碌5441 小时前
云原生时代的智能路由:Istio服务网格实战深度解析
运维
TDengine (老段)2 小时前
TDengine IDMP 数据可视化——状态时间线
大数据·数据库·ai·信息可视化·时序数据库·tdengine·涛思数据
DolphinDB智臾科技2 小时前
V3.00.5 & 2.00.18 更新!TPC-H 性能跃升,MPP 引擎来了…
大数据·数据库·时序数据库·dolphindb
xing-xing2 小时前
Spring Data项目
数据库·spring
济6172 小时前
ARM Linux 驱动开发篇--- pinctrl 子系统详解-- Ubuntu20.04
linux·嵌入式·嵌入式linux驱动开发