Haproxy的安装及配置参数
1.安装
bash
#在调度器(双网卡主机中)
[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.实现最基本的负载
bash
#设定vim中tab键的空格个数方便格式对齐
[root@haproxy ~]# vim ~/.vimrc
set ts=4 ai
#第一种配置方式:前后端frontend backend分开设定
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
#前端
frontend webcluster #前端名
bind *:80 #绑定端口
mode http #七层http/四层tcp
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
健康检查参数:
| 参数 | 示例值 | 作用详解 |
|---|---|---|
| check | 无值 | 开启主动健康检查;不写则仅被动故障检测 |
| inter | 3s | 健康检查探测间隔;单位 ms/s/m,默认 2s |
| fall | 3 | 连续失败 N 次,节点标记 down,停止转发流量 |
| rise | 5 | 故障节点恢复后,连续成功 N 次,标记 up 恢复调度 |
2.log 127.0.0.1 local2
远程发送日志
指定把日志发送到哪里
实验。指定日志发送到192.168.0.10
bash
#在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: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"
本地日志采集
bash
[root@web1 ~]# vim /etc/rsyslog.conf
module(load="imudp") # needs to be done just once
input(type="imudp" port="514")
local2.* action(type="omfile" file="/var/log/haproxy.log")
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
log 127.0.0.1 local2
[root@haproxy ~]# systemctl restart haproxy.service
[root@haproxy ~]# systemctl restart rsyslog.service
#测试:
[Administrator.DESKTOP-VJ307M3] ➤ curl 172.25.254.100
192.168.0.10 - web1
[root@haproxy ~]# cat /var/log/haproxy.log
Jul 27 09:36:58 localhost haproxy[31920]: 172.25.254.1:6823 [27/Jul/2026:09:36:58.266] webcluster webcluster/web1 0/0/0/0/0 200 219 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
3.实现haproxy的多进程(haproxy2.6版本以下的才支持)
bash
#默认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)
bash
#多进程cpu绑定
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
nbproc 2
cpu-map 1 0
cpu-map 2 1
[root@haproxy ~]# systemctl restart haproxy.service
bash
#为不同进程准备不同套接字
[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实现多线程
注意多线程不能和多进程同时启用
bash
#查看当前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
5.proxies代理配置参数
1.IP透传可以把用户IP传给后端服务器,以便做统计。
bash
#透传取消情况下
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
#option forwardfor except 127.0.0.0/8 #注释此行取消透传
[root@haproxy ~]# systemctl restart haproxy.service
#验证
[Administrator.DESKTOP-VJ307M3] ➤ curl 172.25.254.100
webserver1 - 192.168.0.10
[root@webserver1 ~]# cat /etc/httpd/logs/access_log
192.168.0.100 - - [23/Jan/2026:16:34:55 +0800] "GET / HTTP/1.1" 200 26 "-" "curl/7.65.0"
#开启透传
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
option forwardfor except 127.0.0.0/8 #取消注释开启透传
[root@haproxy ~]# systemctl restart haproxy.service
#设定webserver采集日志的模式
[root@webserver1 ~]# vim /etc/httpd/conf/httpd.conf
201 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-For}i \"" combined
[root@webserver1 ~]# systemctl restart httpd
#验证
[Administrator.DESKTOP-VJ307M3] ➤ curl 172.25.254.100
webserver1 - 192.168.0.10
[root@webserver1 ~]# cat /etc/httpd/logs/access_log
192.168.0.100 - - [23/Jan/2026:16:40:38 +0800] "GET / HTTP/1.1" 200 26 "-" "curl/7.65.0" "172.25.254.1"
| 占位符 | 名称 | 输出内容说明 |
|---|---|---|
%h |
远程主机 IP | 原始客户端 IP;若前端有代理(Nginx/Haproxy),不加 X-Forwarded-For 时这里是代理 IP |
%l |
ident 日志 | 远程用户 identd 身份,几乎没人用,固定输出 - |
%u |
认证用户 | HTTP 基础认证登录的用户名,无认证则 - |
%t |
请求时间 | 客户端发起请求的本地时间,格式 [01/Jan/2026:12:00:00 +0800] |
\"%r\" |
请求行 | 完整 HTTP 请求首行,包裹双引号,示例:"GET /index.html HTTP/1.1" |
%>s |
最终状态码 | 最终返回给客户端的 HTTP 状态码;%s 是内部临时状态,%>s 推荐使用 |
%b |
响应字节数 | 返回给客户端的响应体大小(不含 HTTP 响应头);无响应体时值为 - |
\"%{Referer}i\" |
来源页 | 请求头 Referer,记录用户从哪个页面跳转过来;无则 "-",i 代表 Incoming 请求头 |
\"%{User-Agent}i\" |
客户端 UA | 请求头 User-Agent,浏览器 / 设备 / 爬虫标识 |
\"%{X-Forwarded-For}i\" |
真实客户端 IP | 代理传递的 XFF 请求头,多层代理会打印一串 IP,用来获取真实用户 IP |
6.其他参数配置
bash
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
listen webcluster
bind *:80
mode http
balance roundrobin
server web1 192.168.0.10:80 weight 3 maxconn 2000 maxqueue 100 check inter 2s fall 3 rise 3
server web2 192.168.0.20:80 weight 1 maxconn 2000 maxqueue 100 check inter 2s fall 3 rise 3
server web3 192.168.0.100:8080 backup
redirect prefix http://www.baidu.com
redirect prefix http://www.baidu.com 当企业更换域名时可以快速重定向
| 参数 | 示例 | 作用 |
|---|---|---|
| weight | weight 100 | 负载均衡权重,范围 0~256,权重越高分到流量越多;weight 0 禁止新连接接入 |
| maxconn | maxconn 2000 | 当前后端节点最大并发连接上限,超出则不再调度流量到此节点 |
| maxqueue | maxqueue 100 | 节点满载时,允许排队等待的请求数量 |