HAProxy 是高性能的开源负载均衡器,支持四层(TCP)与七层(HTTP)负载均衡,以其极低的资源占用和强大的配置能力,成为 Web 高可用架构的事实标准组件。
核心优点
- 性能极高:单进程即可支撑数万并发连接,内存占用小。
- L4/L7 双支持:既可做 TCP 转发,也可按 URL/Host 做七层路由。
- 健康检查:主动探测后端状态,自动摘除故障节点。
- 会话保持:按 Cookie 或 IP 保持会话,兼容多种业务。
- 状态页:内置 stats 页面实时查看各后端流量与健康。
快速上手
# 安装(Debian/Ubuntu)
apt install haproxy
# 最小配置 /etc/haproxy/haproxy.cfg
frontend web
bind *:80
default_backend servers
backend servers
balance roundrobin
server web1 192.168.1.10:8080 check
server web2 192.168.1.11:8080 check
常用操作
systemctl restart haproxy
haproxy -c -f /etc/haproxy/haproxy.cfg # 配置检查
echo "show stat" | socat stdio /var/run/haproxy.sock # 查看状态
适用场景
Web 集群负载均衡、数据库读写分离前置、TCP 服务高可用、配合 keepalived 实现主备漂移。
下载地址
- 官方网站:https://www.haproxy.org/
- GitHub 仓库:https://github.com/haproxy/haproxy