0

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?

2
  • to make a long answer short: yes, nginx does exactly what your last question suggests. nginx doesn't know anything about the node process, it just blindly passes http requests to another host/port. Commented Sep 16, 2014 at 17:44
  • Feel free to add that as the answer and I'll accept it Commented Sep 19, 2014 at 9:20

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.