我有HAProxy作为负载均衡器和动态重定向器到我的web服务器和websocket服务器,以便它们可以在同一个端口上运行。我的Web套接字服务器需要在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代理在运行时会执行以下操作: 在“use_backend”规则之后放置的“重定向”规则仍将在之前处理。
任何帮助都将不胜感激。
谢谢,
答案 0 :(得分:4)
解决方案如下:
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连接,则重定向。