我正在使用nginx作为Node JS Express服务器前面的反向代理。可以使用节点集群模块创建多个服务器进程,这些服务器进程像这样监听单个unix域套接字,
const cluster = require('cluster');
const http = require('http')
function start() {
const expressServer = require('./main')
const server = http.createServer(expressServer)
server.listen(`/tmp/nginx.socket`)
}
if(cluster.isMaster) {
// Master process: fork our child processes.
const numWorkers = process.env.WEB_CONCURRENCY || 1;
for (var i = 0; i < numWorkers; i += 1) {
console.log('** Booting new worker **');
cluster.fork();
}
// Respawn any child processes that die
cluster.on('exit', function(worker, code, signal) {
console.log('process %s died (%s). restarting...', worker.id, signal || code);
cluster.fork();
});
} else {
start()
}
或者,我可以生成每个进程,让它像这样在自己的域套接字上侦听,
const cluster = require('cluster');
const http = require('http')
function start() {
const expressServer = require('./main')
const server = http.createServer(expressServer)
server.listen(`/tmp/nginx-${process.env['WORKER_ID']}.socket`)
}
if(cluster.isMaster) {
// Master process: fork our child processes.
const numWorkers = process.env.WEB_CONCURRENCY || 1;
for (var i = 0; i < numWorkers; i += 1) {
console.log('** Booting new worker **');
cluster.fork({'WORKER_ID': (i + 1)});
}
// Respawn any child processes that die
cluster.on('exit', function(worker, code, signal) {
console.log('process %s died (%s). restarting...', worker.id, signal || code);
cluster.fork();
});
} else {
start()
}
在这里使用多个域套接字是否有令人信服的理由?