dsp_099
dsp_099

Reputation: 6121

Linking NGINX to Node.js Cluster using proxy_pass

I have set up a NGINX server as well as a node.js process. The node.js code looks something like this:

function startCluster() {
  var numCPUs = require('os').cpus().length;
  if (cluster.isMaster) {
    // Fork workers.
    for (var i = 0; i < numCPUs; i++) {
      cluster.fork();
    }

    cluster.on('exit', function(worker, code, signal) {
      console.log('worker ' + worker.process.pid + ' died');
    });
  } else {
    // begin
    var server = app.listen(3000, function() {
      console.log('Listening on port %d', server.address().port);
    });
  }
}

startCluster();

The NGINX config is using proxy_pass to relay all incoming requests to node on port 3000.

When a server starts I have some 3-4 NGINX processes and 5-6 Node processes. (though I imagine that number might increase or decrease depending on the server load)

Question: How does node's cluster work with NGINX's proxy_pass and does it require any extra configuration? Or does NGINX simply pass requests to port 3000 and then nodejs takes over the rest?

Upvotes: 0

Views: 251

Answers (1)

aembke
aembke

Reputation: 2649

Nginx will blindly pass HTTP requests to another host/port without knowing anything about the backing application. Node's cluster module allows for multiple node processes to bind to a single port and incoming requests to this port will be evenly distributed among the available node processes.

Upvotes: 2

Related Questions