如何使用haproxy负载平衡器启动websocket连接?

如何使用haproxy负载平衡器启动websocket连接?,websocket,haproxy,Websocket,Haproxy,我在我的VMware Ubuntu操作系统中配置了Haproxy负载平衡器,在我的windows 10机器中配置了一个简单的websocket服务器(nodeJS ws)。我想问的问题是如何设置Haproxy和websocket服务器之间的连接 下面是我的Haproxy配置 global daemon maxconn 4096 defaults mode http stats enable stats uri /haproxy?status balance roundr

我在我的VMware Ubuntu操作系统中配置了Haproxy负载平衡器,在我的windows 10机器中配置了一个简单的websocket服务器(nodeJS ws)。我想问的问题是如何设置Haproxy和websocket服务器之间的连接

下面是我的Haproxy配置

global
  daemon
  maxconn 4096

defaults
  mode http
  stats enable
  stats uri /haproxy?status
  balance roundrobin
  option redispatch
  option forwardfor

  timeout connect 5s
  timeout queue 5s
  timeout client 50s
  timeout server 50s

frontend http-in
  bind *:8000
  default_backend servers

  # Any URL beginning with socket.io will be flagged as 'is_websocket'
  acl is_websocket path_beg /socket.io
  acl is_websocket hdr(Upgrade) -i WebSocket
  acl is_websocket hdr_beg(Host) -i ws

  # The connection to use if 'is_websocket' is flagged
  use_backend websockets if is_websocket

backend servers
  server server1 192.168.0.201:80
  server server2 192.168.0.201:80

backend websockets
  balance leastconn
  option http-server-close
  option forceclose
  server ws-server1 192.168.0.252:4080 weight 1 maxconn 1024 check
  server ws-server2 192.168.0.14:8080 weight 1 maxconn 1024 check
这是我的Websocket服务器脚本

var server = require('http').createServer()
  , url = require('url')
  , WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({ server: server })
  , express = require('express')
  , app = express()
  , port = 4080;

app.use(function (req, res) {
  res.send({ msg: "hello" });
});

wss.on('connection', function connection(ws) {
  var location = url.parse(ws.upgradeReq.url, true);
  // you might use location.query.access_token to authenticate or share sessions 
  // or ws.upgradeReq.headers.cookie (see http://stackoverflow.com/a/16395220/151312) 

  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });

  ws.send('something from sevrer');
});

server.on('request', app);
server.listen({port: port, host: '192.168.0.252' }, function () { console.log('Listening on ' + server.address().port) });

粗略地看一下,您的配置似乎有效,所以我不明白您所说的“如何设置连接”是什么意思。连接是在浏览器要求时建立的。粗略地看,您的配置似乎有效,所以我不明白您所说的“如何设置连接”是什么意思。当浏览器请求连接时,就会建立连接。