Appearance
WebSocket 代理配置
HAProxy 完全支持 WebSocket 代理,需要注意长连接和超时配置。
基础 WebSocket 配置
haproxy
global
maxconn 4096
defaults
mode http
timeout client 120s
timeout server 120s
timeout tunnel 3600s
frontend ws_front
bind *:443 ssl crt /etc/ssl/certs/ alpn h2
mode http
# 检测 WebSocket 握手
acl is_websocket hdr(Upgrade) -i websocket
use_backend ws_backend if is_websocket
default_backend web_backend
backend ws_backend
mode http
option http-server-close
option forceclose
timeout server 3600s
server ws1 10.0.0.11:9000 check inter 10sSocket.IO 配置
haproxy
acl is_socketio path_beg /socket.io
timeout client 300s
timeout tunnel 600s
use_backend socketio_backend if is_socketio连接数限制
haproxy
stick-table type ip size 100k expire 5m store http_req_rate(1m)
acl too_many req.rate_limit(gt) 5
http-request deny if too_many常见问题
| 问题 | 解决方案 |
|---|---|
| 连接被断开 | 增大 timeout tunnel |
| 502 Bad Gateway | 检查健康检查配置 |
| 连接数爆炸 | 添加 stick-table 限制 |