我在VMware Ubuntu OS中配置了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) });