HAProxy命令速查
启动校验、配置结构、ACL 路由、负载均衡、健康检查、运行时 API、日志统计与故障排查等 153 条命令
共 153 条命令 · 11 类场景 · 点击右侧图标可复制
| 命令 / 配置 | 说明 | 复制 |
|---|---|---|
haproxy -v | 查看版本号 | |
haproxy -vv | 查看编译参数与可选特性(SSL、Lua、线程、pcre2) | |
haproxy -c -f /etc/haproxy/haproxy.cfg | 校验配置文件语法,改配置后必须先执行 | |
haproxy -c -V -f /etc/haproxy/haproxy.cfg | 校验并输出更详细的解析过程 | |
haproxy -f /etc/haproxy/haproxy.cfg -D -p /var/run/haproxy.pid | 以守护进程启动并写 pid 文件 | |
haproxy -f /etc/haproxy/haproxy.cfg -db | 前台运行且不后台化,便于观察输出 | |
haproxy -f /etc/haproxy/haproxy.cfg -d | 调试模式,启动阶段打印更多信息 | |
haproxy -f /etc/haproxy/haproxy.cfg -W -S /var/run/haproxy.sock | master-worker 模式并开启运行时 socket(2.5+ 推荐) | |
systemctl status haproxy | 查看服务状态 | |
systemctl reload haproxy | 平滑重载配置,不中断已有连接 | |
ss -lntp | grep haproxy | 确认监听端口与进程是否正常 | |
haproxy -n 4096 -f /etc/haproxy/haproxy.cfg | 启动时限制最大连接数(临时压制流量) |
| 命令 / 配置 | 说明 | 复制 |
|---|---|---|
global | 全局段:进程、日志、运行时 API、性能调优都在这里配置 | |
defaults | 默认段:为后续 frontend/backend 提供公共参数与超时 | |
frontend http-in / backend app / listen combo | 前端接流量、后端定义服务器,listen 为二者合一写法 | |
maxconn 4096 | 全局最大并发连接,需同步 ulimit -n | |
nbthread 4 | 线程数,通常等于 CPU 核数 | |
mode http | 七层模式,可用 ACL 匹配 URL 与 Header;tcp 为四层透传 | |
timeout connect 5s 与 timeout client 30s 与 timeout server 30s | 三段超时,缺省会导致连接堆积或 504 | |
stats socket /var/run/haproxy.sock mode 660 level admin expose-fd listeners | 开启运行时管理 socket,运维排障的核心入口 | |
stats timeout 30s | 运行时 socket 的超时设置 | |
log /dev/log local0 info | 日志输出目标与级别,需配合 rsyslog 收集 | |
option httplog 与 option tcplog | 选择日志格式,HTTP 场景用 httplog 才有请求细节 | |
daemon 与 user haproxy 与 group haproxy | 后台运行与降权用户,安全基线要求 | |
chroot /var/lib/haproxy | 运行在受限目录,降低被利用风险 | |
pidfile /var/run/haproxy.pid | 指定 pid 文件,平滑重启时要用到 | |
default-server inter 2s fall 3 rise 2 | 在 defaults 中统一健康检查参数 | |
include /etc/haproxy/conf.d/*.cfg | 拆分配置,便于按业务独立维护 |
| 命令 / 配置 | 说明 | 复制 |
|---|---|---|
frontend http-in bind *:80 | 定义前端监听 80 端口 | |
bind *:443 ssl crt /etc/haproxy/certs/site.pem alpn h2,http/1.1 | 监听 443 并启用 TLS 与 HTTP/2 | |
acl is_api path_beg /api | 按路径前缀匹配 | |
acl host_api hdr(host) -i api.example.com | 按 Host 头匹配(-i 忽略大小写) | |
acl is_static path_end .jpg .css .js .png | 按后缀匹配静态资源 | |
acl is_post method POST | 按请求方法匹配 | |
acl intranet src 10.0.0.0/8 192.168.0.0/16 | 按来源网段匹配 | |
use_backend api_back if is_api | 命中 ACL 时转发到指定后端 | |
use_backend static_back if is_static host_api | 多 ACL 与逻辑,等价于 and | |
use_backend a_back if is_api || is_static | 支持 || 与 ! 逻辑运算 | |
default_backend web_back | 未命中任何规则时的默认后端 | |
http-request redirect scheme https code 301 if !{ ssl_fc } | 强制跳转 HTTPS | |
http-request deny if { src 10.0.0.5 } | 直接拒绝指定来源 | |
http-request set-header X-Forwarded-Proto https if { ssl_fc } | 向后端传递协议信息 | |
http-request return status 200 content-type text/plain string OK | 直接返回响应,不经过后端(健康检查探针常用) | |
use_backend tls_a if { req_ssl_sni -i a.example.com } | TCP 模式下按 SNI 分流,需配置 tcp-request inspect-delay |
| 命令 / 配置 | 说明 | 复制 |
|---|---|---|
backend app | 定义后端服务器组 | |
server s1 10.0.0.5:8080 check | 添加服务器并开启健康检查 | |
server s2 10.0.0.6:8080 check weight 10 maxconn 200 | 设置权重与单机最大连接数 | |
server s3 10.0.0.7:8080 check backup | 备份节点,仅在主节点全部不可用时启用 | |
balance roundrobin | 轮询,默认算法 | |
balance leastconn | 最少连接,长连接与慢请求场景更均衡 | |
balance source | 按源 IP 哈希,实现简单会话保持 | |
balance uri 与 balance hdr(host) | 按 URI 或 Host 哈希,提升缓存命中率 | |
balance random(2) | 随机两个再择优,兼顾均衡与开销 | |
option httpchk GET /health | HTTP 健康检查,返回 2xx/3xx 视为健康 | |
option httpchk HEAD /health HTTP/1.1 与 http-check expect status 200 | 精确校验状态码 | |
resolvers dns 与 server-template app 1-10 api.example.com:8080 check resolvers dns | 动态 DNS 服务发现,节点变化自动更新 | |
init-addr last,libc,none | DNS 不可用时的地址初始化策略 | |
retries 3 与 option redispatch | 失败重试并重新选择服务器,避免打到同一台故障机 | |
cookie SERVERID insert indirect nocache | 七层会话保持,按 cookie 固定后端 | |
stick-table type ip size 100k expire 30s store conn_rate(10s) 与 stick on src | 基于源 IP 的连接限速与会话粘性 | |
option http-keep-alive 与 option http-server-close | 长连接策略,按后端能力选择 |
| 命令 / 配置 | 说明 | 复制 |
|---|---|---|
option tcp-check | 四层握手检查,适合非 HTTP 服务 | |
option httpchk get /health | 七层检查,能真实反映业务可用性 | |
http-check send meth GET uri /health ver HTTP/1.1 hdr Host example.com | 自定义检查请求内容 | |
http-check expect status 200 | 只认可指定状态码 | |
http-check expect string "status":"ok" | 校验响应体关键字,识别假健康 | |
http-check expect rstatus 2xx | 按状态码区间通过 | |
check inter 2s fall 3 rise 2 | 每 2 秒检查,连续失败 3 次下线,成功 2 次上线 | |
on-marked-down shutdown-sessions | 节点下线时中断其上的连接,快速止损 | |
errorfile 503 /etc/haproxy/errors/503.http | 自定义错误页,避免暴露默认提示 | |
option allbackups | 所有备份节点参与轮询,而不只在主节点全挂时启用 | |
server s1 10.0.0.5:8080 check disabled | 初始为维护状态,手动 enable 后加入 |
| 命令 / 配置 | 说明 | 复制 |
|---|---|---|
echo "show info" | socat stdio /var/run/haproxy.sock | 查看进程信息、连接数、速率与运行时长 | |
echo "show stat" | socat stdio /var/run/haproxy.sock | 输出全部前后端与服务器统计(CSV 格式) | |
echo "show stat" | socat stdio /var/run/haproxy.sock | cut -d, -f1,2,5,18,19,20 | 裁剪关键列:状态、当前连接、错误数 | |
echo "show backend" | socat stdio /var/run/haproxy.sock | 列出所有后端名称 | |
echo "show servers state" | socat stdio /var/run/haproxy.sock | 查看服务器状态机与权重 | |
echo "show sess" | socat stdio /var/run/haproxy.sock | 列出当前会话,定位长连接堆积 | |
echo "show sess <id>" | socat stdio /var/run/haproxy.sock | 查看单个会话详情 | |
echo "disable server app/s1" | socat stdio /var/run/haproxy.sock | 手动下线节点,配合健康检查做灰度 | |
echo "enable server app/s1" | socat stdio /var/run/haproxy.sock | 重新上线节点 | |
echo "set server app/s1 state drain" | socat stdio /var/run/haproxy.sock | 排空模式:不再接新连接,等旧连接结束(2.0+) | |
echo "set server app/s1 weight 0" | socat stdio /var/run/haproxy.sock | 权重归零,平滑摘除流量 | |
echo "shutdown sessions server app/s1" | socat stdio /var/run/haproxy.sock | 强制中断该节点的全部会话 | |
echo "set maxconn global 5000" | socat stdio /var/run/haproxy.sock | 动态调整全局最大连接数 | |
echo "set maxconn frontend http-in 1000" | socat stdio /var/run/haproxy.sock | 动态调整前端并发上限 | |
echo "show errors" | socat stdio /var/run/haproxy.sock | 查看最近的请求/响应错误,排障利器 | |
echo "clear counters" | socat stdio /var/run/haproxy.sock | 清零统计计数,便于观察区间数据 | |
echo "show table" | socat stdio /var/run/haproxy.sock | 查看 stick table 内容与命中情况 | |
echo "show pools" | socat stdio /var/run/haproxy.sock | 查看内存池使用,排查内存增长 | |
echo "show fd" | socat stdio /var/run/haproxy.sock | 查看文件句柄使用,句柄耗尽会拒绝新连接 | |
echo "reload" | socat stdio /var/run/haproxy.sock | 通过 sockpair 触发平滑重载(master-worker 模式) |
| 命令 / 配置 | 说明 | 复制 |
|---|---|---|
log /dev/log local0 info | 日志输出到 syslog,配合 rsyslog 落文件 | |
option httplog | 记录方法、路径、状态码、耗时等字段 | |
option dontlognull | 不记录空连接,减少探测噪音 | |
option log-health-checks | 记录健康检查变化,便于回溯 | |
capture request header Host len 40 | 在日志中捕获 Host 头 | |
capture response header Content-Length len 16 | 捕获响应长度,判断响应体大小 | |
stats enable 与 stats uri /haproxy?stats | 开启内置统计页面 | |
stats auth admin:密码 | 为统计页面加基础认证 | |
stats refresh 10s | 页面自动刷新间隔 | |
stats show-legends | 显示字段说明,便于理解各项指标 | |
stats admin if TRUE | 允许在页面上启停服务器(需谨慎授权) | |
log-format %ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B | 自定义日志格式,Tr 为后端耗时、Ta 为总耗时 | |
http-request capture req.hdr(User-Agent) len 64 | 按请求捕获头信息写入日志 | |
option forwardfor | 透传客户端 IP,后端可通过 X-Forwarded-For 获取 |
| 命令 / 配置 | 说明 | 复制 |
|---|---|---|
timeout connect 5s | 连接后端超时,过大导致请求堆积 | |
timeout client 30s | 客户端空闲超时 | |
timeout server 30s | 后端响应超时,超时即返回 504 | |
timeout http-request 10s | 完整请求头接收超时,防御慢速攻击 | |
timeout http-keep-alive 10s | 空闲长连接保持时间 | |
timeout queue 1m | 排队等待超时,队列满后直接拒绝 | |
timeout tunnel 1h | WebSocket 与隧道场景需单独放宽 | |
maxconn 4096 与 ulimit -n 65535 | 连接数与句柄需匹配,否则报 too many open files | |
tune.bufsize 16384 与 tune.maxrewrite 1024 | 缓冲区大小,改大后需重新评估内存占用 | |
http-reuse safe 与 http-reuse always | 后端连接复用策略,减少握手开销 | |
fullconn 1000 | 达到该连接数后按权重动态分配 | |
option splice-auto | 零拷贝转发,提升大流量吞吐 |
| 命令 / 配置 | 说明 | 复制 |
|---|---|---|
cat cert.pem key.pem fullchain.pem > site.pem | 合并证书与私钥,HAProxy 要求单文件 PEM | |
bind *:443 ssl crt /etc/haproxy/certs/ alpn h2,http/1.1 | 目录方式加载多证书,按 SNI 自动匹配 | |
ssl-default-bind-options ssl-min-ver TLSv1.2 | 禁用低版本协议 | |
ssl-default-bind-ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384 | 限制加密套件,满足安全基线 | |
http-request set-header X-Forwarded-For %[src] | 手动注入真实来源 IP | |
acl bad_bot hdr_sub(user-agent) -i sqlmap nikto | 按 UA 识别扫描器 | |
http-request deny deny_status 403 if bad_bot | 拒绝恶意请求并指定状态码 | |
stick-table type ip size 100k expire 60s store http_req_rate(10s) | 按 IP 统计请求速率 | |
http-request deny deny_status 429 if { sc_http_req_rate(0) gt 100 } | 超阈值直接限流返回 429 | |
http-response set-header Strict-Transport-Security "max-age=31536000; includeSubDomains" | 下发 HSTS 头 | |
stats socket /var/run/haproxy.sock mode 660 level admin | 运行时 socket 必须限制权限,等同管理后台 | |
option httponly 与 cookie SERVERID insert indirect httponly secure | 会话 cookie 加固 |
| 命令 / 配置 | 说明 | 复制 |
|---|---|---|
systemctl reload haproxy | 标准平滑重载方式(发行版 unit 已包含 -sf) | |
haproxy -f /etc/haproxy/haproxy.cfg -sf $(cat /var/run/haproxy.pid) | 手动平滑重载:新进程接管后旧进程优雅退出 | |
haproxy -f /etc/haproxy/haproxy.cfg -st $(cat /var/run/haproxy.pid) | 强制终止旧进程,可能中断连接 | |
haproxy -f /etc/haproxy/haproxy.cfg -x /var/run/haproxy.sock -sf $(cat /var/run/haproxy.pid) | 共享运行时 socket,重载后无需重新配置监控 | |
haproxy -W -S /var/run/haproxy.sock -f /etc/haproxy/haproxy.cfg | master-worker 模式,支持 reload 命令与程序托管 | |
hard-stop-after 30s | 限制旧进程最长存活时间,避免连接长期悬挂 | |
haproxy -c -f /etc/haproxy/haproxy.cfg && systemctl reload haproxy | 先校验再重载,防止带错误上线 | |
cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.$(date +%F) | 重载前备份配置,便于回滚 | |
echo "show info" | socat stdio /var/run/haproxy.sock | grep Uptime | 重载后 Uptime 归零说明进程已切换成功 | |
echo "show stat" | socat stdio /var/run/haproxy.sock | grep -c DOWN | 重载后确认没有节点异常下线 |
| 命令 / 配置 | 说明 | 复制 |
|---|---|---|
503 Service Unavailable | 后端没有可用服务器:看 show stat 中 check 状态与 fall 次数 | |
502 Bad Gateway | 后端提前关闭连接或返回非法响应,检查后端日志与超时 | |
504 Gateway Timeout | 后端超时:调大 timeout server,或排查后端慢查询 | |
echo "show errors" | socat stdio /var/run/haproxy.sock | 查看最近的请求/响应异常详情 | |
echo "show stat" | socat stdio /var/run/haproxy.sock | grep -i down | 快速筛出处于 DOWN 的服务器 | |
echo "show sess" | socat stdio /var/run/haproxy.sock | wc -l | 会话数异常增长说明存在连接泄漏或慢后端 | |
echo "show info" | socat stdio /var/run/haproxy.sock | grep -E "ConnRate|SslRate|MaxConn" | 查看连接速率与上限,判断是否打满 | |
curl -v --resolve api.example.com:443:10.0.0.5 https://api.example.com/health | 绕过 DNS 直连指定节点验证服务 | |
ss -lntp | grep haproxy | 确认端口监听与进程存活 | |
grep -iE "alert|error" /var/log/haproxy.log | tail -50 | 提取日志中的严重告警 | |
tcpdump -i eth0 -nn "tcp port 8080" -c 50 | 抓包确认请求是否真正到达后端(可配合 tcpdump 速查) | |
too many open files | 句柄耗尽:提高 ulimit 与 maxconn,并检查连接泄漏 | |
配置报错 unknown keyword | 多半是版本不支持该指令,用 haproxy -vv 确认编译特性 |
运维常用组合
show stat → show errors → show info(连接数/速率)set server app/s1 state drain → 等连接归零 → disable server app/s1haproxy -c -f cfg → systemctl reload haproxy → show stat 确认无 DOWNstick-table store http_req_rate(10s) → http-request deny deny_status 429 if { sc_http_req_rate(0) gt 100 }cat cert key > site.pem → bind *:443 ssl crt ... → echo | openssl s_client -connectresolvers dns → server-template app 1-10 api.example.com:8080 check resolvers dns常见问题排查
1. 改配置 503
先 haproxy -c -f 校验语法,再确认 backend 名称拼写与 server 地址端口,最后看 show stat 的 check 状态
2. 节点摘除不干净
用 set server x/y state drain 排空连接,直接 disable 会切断存量请求
3. 长连接导致不均衡
leastconn 比 roundrobin 更适合长连接;必要时缩短 timeout client 或使用 http-reuse
4. WebSocket 频繁断开
默认 timeout server 会掐断空闲隧道,需单独设置 timeout tunnel 为小时级
5. 统计页面看不到数据
确认 stats enable 在正确的 frontend/listen 内,且 stats uri 未被 ACL 拦截
6. 重载后监控断连
用 -x 共享运行时 socket,或在 systemd 中固定 socket 路径,避免重载后 socket inode 变化
相关工具
Linux命令速查
文件、文本、权限、进程、网络、磁盘、systemd与防火墙等 Linux 常用命令速查手册
Docker命令速查
镜像、容器、网络、数据卷、Compose、Swarm、清理与排障等 Docker 常用命令速查手册
Nginx命令速查
服务管理、配置指令、反向代理、HTTPS、限流缓存、日志排查等 Nginx 常用命令速查手册
K8S命令速查
集群上下文、Pod调试、工作负载、Service与Ingress、存储、权限、排障等 kubectl 常用命令速查手册
Windows CMD命令速查
文件目录、网络诊断、进程服务、系统信息、用户权限、注册表、批处理等 Windows CMD 常用命令速查手册
systemctl服务管理速查
服务启停、开机自启、unit文件编写、日志查看、定时器、依赖分析与排障等 systemctl 命令速查手册
关于HAProxy命令速查
在线 HAProxy 命令速查手册,按安装版本与启动、配置文件核心结构、前端 ACL 与路由、后端与负载均衡、健康检查与故障切换、运行时 API socket 命令、日志与统计页面、超时连接与性能调优、安全与 TLS、平滑重载与升级、常见故障排查等 11 类场景整理 140+ 条常用命令、配置指令与 socat 运行时命令,每条附中文说明与可直接复制的写法,支持关键词搜索、分类折叠和一键复制,打开网页即可查阅。涵盖 haproxy -v 与 -vv 编译特性、-c -f 配置校验、-D 与 -db 与 -d、-W -S master-worker、-sf 与 -st 平滑重载、-x 共享 socket、systemctl reload haproxy、global 与 defaults 与 frontend 与 backend 与 listen 结构、maxconn 与 nbthread 与 mode http/tcp、timeout connect/client/server/http-request/queue/tunnel、stats socket level admin、log /dev/log local0 与 option httplog、bind *:443 ssl crt 与 alpn h2、acl path_beg 与 hdr(host) 与 path_end 与 src 与 method、use_backend 与 default_backend、http-request redirect/deny/set-header/return、server ... check weight maxconn backup、balance roundrobin 与 leastconn 与 source 与 uri、option httpchk 与 http-check expect status/string/rstatus、check inter fall rise、on-marked-down shutdown-sessions、errorfile 503、socat 执行 show info 与 show stat 与 show servers state 与 show sess 与 show errors 与 show table 与 show pools 与 show fd、disable/enable server 与 set server state drain 与 weight 0 与 shutdown sessions、set maxconn global、clear counters、reload、stats enable/uri/auth/refresh/show-legends/admin、log-format %Tr %Ta、capture request header、option forwardfor、http-reuse safe、tune.bufsize、option splice-auto、cat cert key fullchain 合并 PEM、ssl-default-bind-options 与 ciphers、stick-table store http_req_rate 与 sc_http_req_rate 限流、Strict-Transport-Security、hard-stop-after、503/502/504 与 too many open files 排查。适用于 HAProxy 反向代理与七层负载均衡运维、灰度摘流、限流防刷、TLS 证书配置、平滑重载与线上排障,以及运维面试复习。
核心能力
- 涵盖 haproxy -v 与 -vv 编译特性、-c -f 配置校验、-D 与 -db 与 -d、-W -S master-worker、-sf 与 -st 平滑重载、-x 共享 socket、systemctl reload haproxy、global 与 defaults 与 frontend 与 backend 与 listen 结构、maxconn 与 nbthread 与 mode http/tcp、timeout connect/client/server/http-request/queue/tunnel、stats socket level admin、log /dev/log local0 与 option httplog、bind *:443 ssl crt 与 alpn h2、acl path_beg 与 hdr(host) 与 path_end 与 src 与 method、use_backend 与 default_backend、http-request redirect/deny/set-header/return、server ... check weight maxconn backup、balance roundrobin 与 leastconn 与 source 与 uri、option httpchk 与 http-check expect status/string/rstatus、check inter fall rise、on-marked-down shutdown-sessions、errorfile 503、socat 执行 show info 与 show stat 与 show servers state 与 show sess 与 show errors 与 show table 与 show pools 与 show fd、disable/enable server 与 set server state drain 与 weight 0 与 shutdown sessions、set maxconn global、clear counters、reload、stats enable/uri/auth/refresh/show-legends/admin、log-format %Tr %Ta、capture request header、option forwardfor、http-reuse safe、tune.bufsize、option splice-auto、cat cert key fullchain 合并 PEM、ssl-default-bind-options 与 ciphers、stick-table store http_req_rate 与 sc_http_req_rate 限流、Strict-Transport-Security、hard-stop-after、503/502/504 与 too many open files 排查
适用场景
适用于 HAProxy 反向代理与七层负载均衡运维、灰度摘流、限流防刷、TLS 证书配置、平滑重载与线上排障,以及运维面试复习。HAProxy命令速查全程在浏览器中完成,无需安装插件,可放心用于日常办公与排障。
常见问题
- HAProxy命令速查是免费的吗?需要注册或安装吗?
- HAProxy命令速查完全免费,无需注册、无需下载安装任何软件,用浏览器打开网页即可直接使用,也不限制使用次数。
- HAProxy命令速查支持哪些功能?
- 涵盖 haproxy -v 与 -vv 编译特性、-c -f 配置校验、-D 与 -db 与 -d、-W -S master-worker、-sf 与 -st 平滑重载、-x 共享 socket、systemctl reload haproxy、global 与 defaults 与 frontend 与 backend 与 listen 结构、maxconn 与 nbthread 与 mode http/tcp、timeout connect/client/server/http-request/queue/tunnel、stats socket level admin、log /dev/log local0 与 option httplog、bind *:443 ssl crt 与 alpn h2、acl path_beg 与 hdr(host) 与 path_end 与 src 与 method、use_backend 与 default_backend、http-request redirect/deny/set-header/return、server ... check weight maxconn backup、balance roundrobin 与 leastconn 与 source 与 uri、option httpchk 与 http-check expect status/string/rstatus、check inter fall rise、on-marked-down shutdown-sessions、errorfile 503、socat 执行 show info 与 show stat 与 show servers state 与 show sess 与 show errors 与 show table 与 show pools 与 show fd、disable/enable server 与 set server state drain 与 weight 0 与 shutdown sessions、set maxconn global、clear counters、reload、stats enable/uri/auth/refresh/show-legends/admin、log-format %Tr %Ta、capture request header、option forwardfor、http-reuse safe、tune.bufsize、option splice-auto、cat cert key fullchain 合并 PEM、ssl-default-bind-options 与 ciphers、stick-table store http_req_rate 与 sc_http_req_rate 限流、Strict-Transport-Security、hard-stop-after、503/502/504 与 too many open files 排查。
- HAProxy命令速查适合哪些场景使用?
- 适用于 HAProxy 反向代理与七层负载均衡运维、灰度摘流、限流防刷、TLS 证书配置、平滑重载与线上排障,以及运维面试复习。