This tutorial will teach you how to install Nginx and load balancing the node.js application. Nginx is used in production environment for scaleable purpose. Nginx is very popular for load balancing the servers.I’ll show you how to install nginx on centos 6.5 and configure for node.js application.
Note: The recommended way to install ngix is on linux machine, because some of the features is not available in windows setup.
What is Nginx?
Nginx (pronounced “engine x”) is a web server with a strong focus on high concurrency, performance and low memory usage. It can also act as a reverse proxy server for HTTP, HTTPS protocols, as well as a load balancer and an HTTP cache.
Install epel repo because nginx is hosted in epel.
Command to install epel repo is
Now it’s time to install nginx
Nginx will be installed in /etc/nginx directory.
Now all set to go, lets start nginx without changing configuration and test it.
To test if the nginx server is up.
Go to any browser and type your machine ip address or localhost, you should see the welcome page of nginx.
The default port of nginx is 80, no need to mention in browser because the browser default uses default port as 80.
Now lets change some configuration of nginx so that we can mention node.js server url.
The nginx use nginx.confi while starting up, if you see nginx.config under root folder of nginx installation.
In that configuration file you will a line telling
# Load config files from the /etc/nginx/conf.d directory
# The default server is in conf.d/default.conf
Means if you want to add any server go and add in default.conf under conf.d.
Just open default.conf
delete all the entry and add below lines
Note: You can add any number of node.js application servers under upstream project block.
As you can see i have added just one node.js server, you can add multiple server to improve the performance and to handle the traffic.
Time for checking all is working.
Now go to web browser and type machine ip address or localhost you should see your node.js application.
If you see your application then your set to go and enjoy.
You can check the performance by load testing, you can use jMeter by apache. Best way is add one server test the load and then add another then again test the load, then you ll come to know the performance of nginx.
Note: Nginx is also used for caching the static css/img/js for increasing performance, means no need to load the same css/img/js every time end user request it ll serve using the stored cache.