配置文件
haproxy 的每个版本的变化都相对比较大,可能一个功能或一个参数在上一个版本还可以使用 但换其其他版本就不能用了,所以最权威的还是要看官方文档
- GitHub官方帮助文档:http://cbonte.github.io/haproxy-dconv/
- 官方帮助文档:https://www.haproxy.org/#docs
配置文件结构
# 全局配置段,包括进程及安全配置相关参数、性能调整相关参数、Debug参数等
global
...
# 代理配置段
defaults # 为 frontend,backend,listen 提供默认配置
...
frontend # 前端配置,相当于nginx中的server{}
...
backend # 后端配置,相当于nginx中的upstream{}
...
listen # 前端和后端配置的组合 选择 frontend+backend 还是 listen?
- 对于简单的 TCP 转发(如数据库)或单一出口的 HTTP 服务,推荐使用
listen,配置紧凑且直观。 - 对于需要域名/路径分发、多证书管理或后端复用的复杂业务,推荐使用
frontend+backend实现逻辑解耦。
一句话总结:简单链路用 listen,复杂网关选 frontend/backend。
global 配置段
global
log 127.0.0.1 local2 info # 定义全局的syslog服务器;日志服务器需要开启UDP协议,最多可以定义两个
pidfile /var/run/haproxy.pid # pid文件路径
chroot /var/lib/haproxy # 锁定haproxy的运行目录,更加安全
daemon # 以守护进程方式运行(后台执行)
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners # socket文件(socket文件可以实现本机与本机之间应用程序的通信)
user haproxy # 运行haproxy的用户身份
group haproxy # 运行haproxy的组身份
nbthread 8 # 启动8个线程(与实际CPU核心数匹配),这样每个线程可以独占一个核心,避免线程竞争,提高性能。
cpu-map auto:1/1-8 0-7 # 使用自动映射(auto:),将线程1绑定到CPU 0、线程2到CPU 1、...、线程8到CPU 7。
maxconn 100000 # 每个haproxy进程的最大并发连接数
maxsslconn 50000 # 每个haproxy进程 ssl最大连接数,用于haproxy配置了证书的场景下
maxsslrate 10000 # 每个进程每秒创建的最大连接数,用于haproxy配置了证书的场景下,此值根据测试调整
spread-checks 3 # 后端server状态check随机提前或延迟百分比时间,建议2-5(20%-50%)之间,默认0 多核性能优化:弃“进程”选“线程”
https://cbonte.github.io/haproxy-dconv/2.0/configuration.html#nbthread
HAProxy 2.0中,推荐使用多线程模式(nbthread)来利用多核,而不是旧的多进程模式(nbproc)。线程模式共享资源更好,同步开销更小,适合大多数场景。
同时配合 cpu-map 绑核(CPU亲和性 affinity ),将线程绑定到固定 CPU 核心,以减少上下文切换并显著提升处理性能。
defaults 配置段
defaults
log global # 继承 global 部分定义的日志设置
mode http # 工作模式,http 或 tcp,默认 http
option httplog # 开启http日志记录
option dontlognull # 不记录空连接日志。空连接指:客户端连接上来但没发送任何数据就断开(常见于健康检查失败、扫描器探测)。开启后能大幅减少无意义的日志量,避免日志被刷爆。
option http-keep-alive # 客户端长连接(开启与客户端的 Keep-Alive 持久连接,提升客户端性能)
option http-server-close # 后端短连接(每次请求后主动关闭后端连接,避免长连接占用后端资源)
option forwardfor # 透传客户端真实IP至后端web服务器
retries 3 # 连接后端失败时的重试次数(默认 1)。失败包括:连接建立失败、读/写超时、502/503/504 等网关错误。合理值通常为 2~3,避免过多重试导致延迟放大,同时提升可用性。
option redispatch # 当retries重试失败后,重新派发到其他健康的服务器
timeout connect 5s # 到后端服务器建立连接的超时时间,如果 5 秒内连不上后端,就返回 502/504 错误。
timeout client 30s # 客户端(浏览器)空闲超时,如果客户端连接保持但 30 秒内没发送数据(或没读完响应),HAProxy 主动断开。防止客户端异常占用连接。
timeout server 30s # 后端服务器空闲超时,如果后端服务器连接保持但 30 秒内没发送数据(或没读完响应),HAProxy 主动断开。防止后端异常占用连接。
# 针对不同状态码返回自定义错误页面
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.httpfrontend + backend 配置段
TCP
- 四层代理模式下,HAProxy 仅转发字节流,不解析请求内容。主要用于数据库、消息队列、SSL Passthrough 等场景。
MySQL
frontend mysql-in
bind *:3306 # 监听标准 MySQL 端口
mode tcp # 必须设置为 tcp
option tcplog # 使用 TCP 日志格式(比 httplog 更合适)
# 可选:根据源 IP 或其他 TCP 特性进行简单路由(ACL 有限)
# acl src_internal src 192.168.0.0/16
# use_backend mysql_readonly if src_internal
default_backend mysql_servers
backend mysql_servers
mode tcp # 必须与 frontend mysql-in 一致
balance leastconn # 长连接数据库推荐 leastconn
option mysql-check user haproxy_check # 内置 MySQL 健康检查(需后端创建 haproxy_check 用户)
server mysql1 192.168.2.10:3306 check port 3306 weight 10
server mysql2 192.168.2.11:3306 check port 3306 weight 10 backupHTTP
- HTTP代理模式下,HAProxy 会解析 HTTP 请求头、URL、Cookie 等,支持高级功能如:URL 重写、ACL、header 操作、内容切换、HTTP/2 等。
# HTTP 入口前端(支持 HTTP 和 HTTPS)
frontend http-in
bind *:80 # 监听所有 IP 的 80 端口(HTTP)
bind *:443 ssl crt /etc/haproxy/certs/example.com.pem alpn h2,http/1.1 # 监听 443 端口(HTTPS),加载证书,支持 HTTP/2
# 可选:HTTP 自动跳转到 HTTPS(推荐生产环境开启)
# http-request redirect scheme https code 301 if !{ ssl_fc }
mode http # HTTP 模式(继承 defaults 中的 mode http,可省略)
# ACL 示例:根据 Host 头部进行域名分发
acl host_www hdr(host) -i www.example.com
acl host_api hdr(host) -i api.example.com
acl host_static path_beg /static /images /css /js
# 根据 ACL 路由到不同 backend
use_backend static_servers if host_www host_static
use_backend app_servers if host_www
use_backend api_servers if host_api
default_backend app_servers # 默认后端(未匹配 ACL 时使用)
# HTTPS 专用前端(如果想把 HTTP 和 HTTPS 分开处理,可单独定义)
# frontend https-in
# bind *:443 ssl crt /etc/haproxy/certs/example.com.pem alpn h2,http/1.1
# mode http
# # 这里可以添加更多 HTTPS 专属规则,如 HSTS 头部
# http-response set-header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
# default_backend app_servers
# 静态资源后端(适合 CDN 或轻量级 Nginx 服务器)
backend static_servers
balance roundrobin # 轮询算法(也可选 leastconn、source 等)
option forwardfor # 透传客户端真实 IP(继承自 defaults,可省略)
# 健康检查:推荐使用 HTTP 检查而非单纯 TCP
option httpchk GET /health HTTP/1.1\r\nHost:\ www.example.com
server static1 192.168.1.10:80 check maxconn 1000
server static2 192.168.1.11:80 check maxconn 1000 backup # backup 表示备用服务器,仅在主服务器全部 down 时使用
# 应用服务器后端(动态内容,如 Java/Tomcat、PHP 等)
backend app_servers
balance leastconn # 最小连接算法,适合长连接场景
option redispatch # 重试失败后重新分发(继承自 defaults)
# 可选:开启 cookie-based 粘性会话(session persistence)
# cookie SERVERID insert indirect nocache
# server app1 192.168.1.20:8080 check cookie app1
# server app2 192.168.1.21:8080 check cookie app2
server app1 192.168.1.20:8080 check inter 2000 rise 2 fall 3
server app2 192.168.1.21:8080 check inter 2000 rise 2 fall 3
# API 后端(独立的后端,便于单独限流、监控或使用不同算法)
backend api_servers
balance source # 源 IP 哈希,适合需要会话保持的 API
http-request set-header X-Forwarded-Proto https if { ssl_fc } # 透传协议
server api1 192.168.1.30:9000 check ssl verify none
server api2 192.168.1.31:9000 check ssl verify nonelisten 配置段
开启统计页面
# 开启统计页面,监听所有IP的1936端口
listen stats
bind *:1936 # 监听所有IP的1936端口
mode http
stats enable # 开启统计页面
stats uri /stats # 统计页面URL路径
stats auth admin:admin123 # 统计页面登录用户名和密码主从 DNS 配置
# 主从 DNS 配置
listen dns_tcp
bind 172.16.0.100:53
mode tcp
balance roundrobin
# 增加以下三行来消除警告
timeout connect 5s # 连接后端服务器的超时时间
timeout client 30s # 客户端空闲超时时间
timeout server 30s # 服务端空闲超时时间
# 健康检查
server dns_master 172.16.0.223:53 check
server dns_slave 172.16.0.225:53 check检查配置文件语法与重载配置文件
# 检查配置文件语法是否正确,返回 `Configuration file is valid` 则表示配置文件语法正确。
haproxy -c -f /etc/haproxy/haproxy.cfg
# 启动新进程加载新配置,旧进程继续处理已有连接,直到所有连接自然结束或超时后退出。新连接立即走新配置。
# 等价于向 master 进程发送 SIGUSR2 信号。
systemctl reload haproxy七层代理配置示例:
四层代理配置示例:
实现子配置文件
...
# 添加 -f /etc/haproxy/conf.d/
Environment="CONFIG=/etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d/" "PIDFILE=/run/haproxy.pid" "EXTRAOPTS=-S /run/haproxy-master.sock"
...
# 创建子配置文件目录
mkdir /etc/haproxy/conf.d/
# 重新加载 systemd 配置
systemctl daemon-reload