How to install Nginx on CentOS 6.5 with Node.js

[custom-facebook-feed]

Introduction

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.

Step 1:

Install epel repo because nginx is hosted in epel.

Command to install epel repo is

yum install epel-release

Step 2:

Now it’s time to install nginx

yum install nginx

Nginx will be installed in /etc/nginx directory.

Step 3:

Now all set to go, lets start nginx without changing configuration and test it.

service nginx start

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.

Step 4:

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.

cd /etc/nginx

nano nginx.confi

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
include /etc/nginx/conf.d/*.conf;

Means if you want to add any server go and add in default.conf under conf.d.

Just open default.conf

cd conf.d/

nano default.conf

delete all the entry and add below lines

upstream project {
server <your node.js application host name1>:<port number1>;

server <your node.js application host name2>:<port number2>;

}
server {
listen 80;
location /{
proxy_pass http://project;
}

}

Note: You can add any number of node.js application servers under upstream project block.

confi

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.

Step 5:

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.

Step 6(Optional):

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.

Leave a Reply

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