在多个端口上使用haproxy将WebSocket路由到socket.io
我在试图配置的ubuntu服务器上使用haproxy正确路由socket.io websocket握手时遇到了一个大问题。我在这里和网上都读了很多,但似乎找不到一个最终的解决办法。 我要达到的目标相当复杂。 我在同一台服务器上运行多个远程NodeJ,每个NodeJ位于不同的端口上,但我必须能够从浏览器的端口80上访问所有NodeJ(客户端防火墙问题)。 我设法实现的是用haproxy从在多个端口上使用haproxy将WebSocket路由到socket.io,websocket,socket.io,haproxy,Websocket,Socket.io,Haproxy,我在试图配置的ubuntu服务器上使用haproxy正确路由socket.io websocket握手时遇到了一个大问题。我在这里和网上都读了很多,但似乎找不到一个最终的解决办法。 我要达到的目标相当复杂。 我在同一台服务器上运行多个远程NodeJ,每个NodeJ位于不同的端口上,但我必须能够从浏览器的端口80上访问所有NodeJ(客户端防火墙问题)。 我设法实现的是用haproxy从 http://HOST/8081/ 到 在后端,这样每个请求都将被路由到正确的节点。 我现在遇到了套接字问题
http://HOST/8081/
到
在后端,这样每个请求都将被路由到正确的节点。
我现在遇到了套接字问题,因为为了让haproxy重写每个请求,我必须使用httpclose作为选项,但这会使WebSocket失败。
通过搜索,我找到了一种解决方法,将以/socket.io或/node开头的请求路由到具有正确端口的同一后端,但具有不同的选项。
这是可行的,但现在所有WebSocket请求都链接到同一个nodejs距离,这显然是一个问题,因为所有客户端现在都连接到一个服务器。
我已尽我所能创建和使用acl将WebSocket发送到正确的服务器,但在请求中找不到路由它们的任何内容。
这是我设法实现的最后一个配置,通过这个配置,websocket请求被路由到正确的服务器,但是websocket无法创建,socket.io回退到xhr轮询,浏览器上的错误是
到ws://HOST/socket.io/1/WebSocket/Wra1vIqoPcTqBNkRLTif'的WebSocket连接失败:意外响应代码:503
忘了说,我有
socket.io version 0.9.11
haproxy version 1.4.18
我认为您需要重命名一些ACL。例如,node和socket.io WebSocket的两个ACL可能需要不同。目前,它们都被命名为
is\u socket\u io
。将它们命名为is\u node
和is\u socket\u io
之后,需要将相关的use\u后端行更新为use\u backend socket\u io\u 8081,如果is\u node myacl\u refereer0
和use\u backend socket\u io\u 8090,如果is\u socket\u io myacl\u refereer0
。这就引出了myacl\u referer0 ACL。对于该ACL,您可能还需要有两个唯一命名的ACL。
global
log 127.0.0.1 local0 debug
maxconn 4096
user haproxy
group haproxy
daemon
defaults
log global
mode http
# httpclose is necessary otherwise haproxy will rewrite only the first request of the session
option httpclose
option httplog
option dontlognull
retries 3
option redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontend http
bind :80
option forwardfor
acl is_socket_io path_beg /node
acl is_socket_io path_beg /socket.io
#use_backend socket_io if is_socket_io
acl myacl_rewrite0 hdr(host) -i HOST
acl myacl_path0 path_dir -i 8081
acl myacl_referer0 hdr_beg(referer) http://HOST/8081/
use_backend socket_io_8081 if is_socket_io myacl_referer0
use_backend mysrv_rewrite0 if myacl_rewrite0 myacl_path0
acl myacl_rewrite1 hdr(host) -i HOST
acl myacl_path1 path_dir -i 8090
acl myacl_referer0 hdr_beg(referer) http://HOST/8090/
use_backend socket_io_8090 if is_socket_io myacl_referer0
use_backend mysrv_rewrite1 if myacl_rewrite1 myacl_path1
backend mysrv_rewrite0
reqirep ^([^\ :]*)\ /8081/(.*) \1\ /\2
server myorigin_rewrite0 127.0.0.1:8081
backend mysrv_rewrite1
reqirep ^([^\ :]*)\ /8090/(.*) \1\ /\2
server myorigin_rewrite1 127.0.0.1:8090
backend socket_io_8081
mode http
option httplog
# long timeout
timeout server 86400000
# check frequently to allow restarting
# the node backend
timeout check 1s
# add X-Forwarded-For
option forwardfor
# Do not use httpclose (= client and server
# connections get closed), since it will close
# Websockets connections
no option httpclose
# Use "option http-server-close" to preserve
# client persistent connections while handling
# every incoming request individually, dispatching
# them one after another to servers, in HTTP close mode
option http-server-close
option forceclose
server node1 127.0.0.1:8081 maxconn 2000 check
backend socket_io_8090
mode http
option httplog
# long timeout
timeout server 86400000
# check frequently to allow restarting
# the node backend
timeout check 1s
# add X-Forwarded-For
option forwardfor
# Do not use httpclose (= client and server
# connections get closed), since it will close
# Websockets connections
no option httpclose
# Use "option http-server-close" to preserve
# client persistent connections while handling
# every incoming request individually, dispatching
# them one after another to servers, in HTTP close mode
option http-server-close
option forceclose
server node2 127.0.0.1:8090 maxconn 2000 check
socket.io version 0.9.11
haproxy version 1.4.18