Configuring Load Balancer with Nginx and Node.js

Load Balancing is needed to distribute load of work across multiple resources – in computer science terminology, these multiple resources can be pieces of software or hardware, where same work processes are waiting to be executed on processing request. So in web application, we will need to do load balance to control user requests to server. Complexity of load balance can be done in various formats (physical architecture). In this article we had simulated a load balancing architecture with nginx proxy server and node.js application servers. If reader want to know the integration of node.js and nginx, he/she can go through previous article.

We expect knowledge of reader in web technology at intermediate level.

Below is a picture, where load balancing scenario is drawn.

Load Balancer

Now to simulate this load balancing work, we had taken one nginx server and spawned 2 node.js server on same computer.

First, the main configurations in the nginx server –

....
            upstream sample {
	      server 127.0.0.1:3000;
	      server 127.0.0.1:3001;
	      keepalive 64;
	    }
            server {
	        listen 80;
	        ....
            server_name 127.0.0.1;

            ...

            location / {
               proxy_redirect off;
               proxy_set_header X-Real-IP $remote_addr;
               proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
               proxy_set_header X-Forwarded-Proto $scheme;
               proxy_set_header Host $http_host;
               proxy_set_header X-NginX-Proxy true;
               proxy_set_header Connection "";
               proxy_http_version 1.1;
               proxy_pass http://sample;
           }
        }

Here we have two node.js server processes in port 3000 and 3001 up and they are doing the same work. In the upstream section, we have added two node.js server in port 3000 and 3001. Also we have made proxy of our request in proxy_pass http://sample.

Now the node.js server set up -

var http = require('http');
var morgan       = require('morgan');

var server1 = http.createServer(function (req, res) {
  console.log("Request for:  " + req.url + "-- port 3000 ");
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Node.js\n');
}).listen(3000, "127.0.0.1");

var server2 = http.createServer(function (req, res) {
  console.log("Request for:  " + req.url + "-- port 3001 ");
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Node.js\n');
}).listen(3001, "127.0.0.1");

server1.once('listening', function() {
  console.log('Server running at http://127.0.0.1:3000/');
});

server2.once('listening', function() {
  console.log('Server running at http://127.0.0.1:3001/');
});

When we hit on the browser in http://127.0.0.1
We will have following output in console-


  Server running at http://127.0.0.1:3000/
  Server running at http://127.0.0.1:3001/
  Request for:  /-- port 3001 
  Request for:  /favicon.ico-- port 3000 
  Request for:  /favicon.ico-- port 3001 
  Request for:  /-- port 3000 
  Request for:  /favicon.ico-- port 3001 
  Request for:  /favicon.ico-- port 3000 
  Request for:  /-- port 3001 
  Request for:  /favicon.ico-- port 3000 
  Request for:  /favicon.ico-- port 3001 
  Request for:  /-- port 3000 
  Request for:  /favicon.ico-- port 3001 
  Request for:  /favicon.ico-- port 3000 

In above, we are viewing request on 3000 and 3001 port one after another, which is round robin load balancing scheme in nginx proxy server.

We will discuss different schemes of nginx load balancing in our next article.

If you find this article helpful, you can connect us in Google+ and Twitter for other updates.

3 thoughts on “Configuring Load Balancer with Nginx and Node.js

Leave a Reply

Your email address will not be published. Required fields are marked *