解决 HAProxy 报错 SSL handshake failure alert bad certificate
分析 HAProxy 双向认证报 SSL handshake failure alert bad certificate 根因,详解 ca-file 根证书链、客户端证书格式与校验调优。
问题现象
客户端通过 mTLS(双向 SSL 认证)连接 HAProxy 代理时,握手被强制中断并报错:
curl: (35) error:14094412:SSL routines:ssl3_read_bytes:sslv3 alert bad certificate
HAProxy 日志中输出:
[WARNING] 318/111000 (15421) : Connect from 192.168.1.50:54120 to 10.0.0.1:443 failed: SSL handshake failure, alert 'bad certificate'
触发根因
- 客户端提供的证书未被 HAProxy 配置的
ca-file信任:客户端证书由其他 CA 签发,或 Intermediate 中级证书缺失。 - 客户端证书已过期或尚未到生效期。
- 未提供证书:配置了
verify required但客户端发起了普通单向 HTTPS 握手。
诊断排查步骤
- 检查客户端证书有效期与签发者:
openssl x509 -in client.crt -noout -issuer -subject -dates
- 检查 HAProxy 的
bind配置段落:
bind 0.0.0.0:443 ssl crt /etc/haproxy/server.pem ca-file /etc/haproxy/ca.crt verify required
解决步骤与操作命令
步骤一:确保 ca-file 包含完整的根 CA 与中级 CA 证书链
将根 CA 与中间 CA 合并为一个完整的 bundle 文件:
cat intermediate-ca.crt root-ca.crt > /etc/haproxy/ca-bundle.crt
更新配置:
bind 0.0.0.0:443 ssl crt /etc/haproxy/server.pem ca-file /etc/haproxy/ca-bundle.crt verify required
步骤二:若允许部分客户端免证书验证,改为 verify optional
bind 0.0.0.0:443 ssl crt /etc/haproxy/server.pem ca-file /etc/haproxy/ca-bundle.crt verify optional
步骤三:平滑重载 HAProxy
systemctl reload haproxy
验证方法
客户端带上匹配的证书发起测试:
curl -i https://example.com/ --cert client.crt --key client.key --cacert ca.crt
顺利通过 mTLS 握手并返回 HTTP 200。


