Answer the question
In order to leave comments, you need to log in
How to set up nginx+nodejs+socket.io load balancer?
Current nginx settings:
upstream socket_nodes {
server xxx:4000 weight=5;
server xxx:4001 weight=5;
}
server {
listen xxx:80;
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://socket_nodes;
}
error_log /var/www/chat/chat.log;
}
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
var env = {port: 4000+i};
cluster.fork(env);
}
cluster.on('fork', function(worker) {
console.log('Worker %s created', worker.id);
});
cluster.on('online', function(worker) {
console.log('Worker %s online', worker.id);
});
cluster.on('listening', function(worker, addr) {
console.log('Worker %s listening %s:%d', worker.id, addr.address, addr.port);
});
cluster.on('disconnect', function(worker) {
console.log('Worker %s disconnect', worker.id);
});
cluster.on('exit', function(worker, code, signal) {
console.log('worker %d died (%s). restarting...',
worker.process.pid, signal || code);
cluster.fork();
});
} else {
var http = require('http');
var express = require('express'), app = express();
var server = http.createServer(app);
// Настройки
var io = require('socket.io').listen(server);
// Слушаем
server.listen(process.env.port);
// Открываем коннект
io.sockets.on('connection', function (socket) {
console.log('user connect to '+process.env.port);
});
}
WebSocket connection to 'ws://xxx/socket.io/?user_id=2&auth_key=5b69f2ada406ba31d561e879fb198888&username=y0rker&mode=2&tm=1448303203&sig=4acb2ef78bf49092769a388516fbc920&EIO=3&transport=websocket&sid=gFEC-jJ0eP5vwJ0PAAAm' failed: WebSocket is closed before the connection is established.
socket.io.js:2 POST http://xxx/socket.io/?user_id=2&auth_key=5b69f2ada406ba31d561e879…bc920&EIO=3&transport=polling&t=1448303270818-119&sid=gFEC-jJ0eP5vwJ0PAAAm 400 (Bad Request)Request.create @ socket.io.js:2Request @ socket.io.js:2XHR.request @ socket.io.js:2XHR.doWrite @ socket.io.js:2(anonymous function) @ socket.io.js:2(anonymous function) @ socket.io.js:2proxy @ socket.io.js:2(anonymous function) @ socket.io.js:2(anonymous function) @ socket.io.js:2exports.encodePacket @ socket.io.js:2encodeOne @ socket.io.js:2eachWithIndex @ socket.io.js:2map @ socket.io.js:2exports.encodePayload @ socket.io.js:2Polling.write @ socket.io.js:2close @ socket.io.js:2Polling.doClose @ socket.io.js:2Transport.close @ socket.io.js:1Socket.onClose @ socket.io.js:1Socket.onError @ socket.io.js:1(anonymous function) @ socket.io.js:1Emitter.emit @ socket.io.js:1Transport.onError @ socket.io.js:1(anonymous function) @ socket.io.js:2Emitter.emit @ socket.io.js:1Request.onError @ socket.io.js:2(anonymous function) @ socket.io.js:2
socket.io.js:2 GET http://xxx/socket.io/?user_id=2&auth_key=5b69f2ada406ba31d561e879…bc920&EIO=3&transport=polling&t=1448303272070-121&sid=nv1y09x6u9s6pduSAAAn 400 (Bad Request)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question