用于HTTP的HAProxy SSL重定向,但不用于WebSockets

问题描述 投票:0回答:1

我将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”规则之后放置的“重定向”规则仍将在此之前进行处理。

任何帮助将不胜感激。

谢谢,

ssl websocket port haproxy
1个回答
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连接,则将其重定向。

© www.soinside.com 2019 - 2024. All rights reserved.