我将 HAProxy 作为我的网络服务器和网络套接字服务器的负载平衡器和动态重定向器,以便它们可以在同一端口上运行。我的网络套接字服务器需要在 ha 代理上终止 SSL。
我想配置 HAProxy,以便将 http 流量重定向到 https,但 websockets 在 bot 端口 80 和 443(ws 和 wss)上工作。这可能吗?
我当前的配置是:
global
maxconn 50000
user root
group root
stats socket /tmp/haproxy
node lb1
nbproc 1
#daemon
#debug
defaults
log global
retries 3
option dontlog-normal
timeout connect 10000ms
timeout client 10000ms
timeout server 10000ms
timeout tunnel 24h
maxconn 50000
mode http
option http-server-close
backend wwwServers
mode http
balance roundrobin
option httpchk HEAD / HTTP/1.1
server www1 127.0.0.1:1138 check
backend wsServers
server ws1 127.0.0.1:1137 check
frontend secured
bind :443 ssl crt /cert/cert.pem
reqadd X-Forwarded-Proto:\ https
default_backend wwwServers
frontend unsecured
bind :80
acl is_websocket hdr(Upgrade) -i WebSocket
use_backend wsServers if is_websocket
redirect scheme https if !{ ssl_fc }
default_backend wwwServers
但这会在升级前重定向 websocket 连接,因为 ha proxy 在我运行它时会执行以下操作: 放置在“use_backend”规则之后的“重定向”规则仍将在之前处理。
如有任何帮助,我们将不胜感激。
谢谢,
最佳答案
解决方法如下:
frontend secured
bind :443 ssl crt /path/to/certificate.pem
reqadd X-Forwarded-Proto:\ https
acl is_websocket hdr(Upgrade) -i WebSocket
use_backend wsServers if is_websocket
default_backend wwwServers
frontend unsecured
bind :81,:80
acl is_websocket hdr(Upgrade) -i WebSocket
redirect scheme https if !{ ssl_fc } !is_websocket
use_backend wsServers if is_websocket
default_backend wwwServers
如果建立了非 ssl 非 websocket 连接,则它会被重定向。
关于ssl - 用于 HTTP 而非 WebSocket 的 HAProxy SSL 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20763978/