Setting up Node.js application with nginx as a reverse proxy

This tutorial is on how to set up NGINX as a reverse proxy in front of a Node.js application.

This is not mandatory but there are some benefits of using nginx in place of Node.js default server.

As discussed on this Stackoverflow Post

In short why we should use nginx in front of Node.js,

  • Not having to worry about privileges/setuid for the Node.js process as only root can bind to port 80. Nginx will take care of that.

  • Nginx is more efficient in serving static files like images, css, js, and html.

  • Better handling of node service crash.

  • Running another web server in front of Node may help to mitigate security flaws and DoS attacks against Node. For a real-world example, CVE-2013-4450 is prevented by running something like Nginx in front of Node.

So, Following are the steps on how to install and configure it.

  • Running Your Node.js Application with Forever
  • Map a Domain to nginx and add proxy for Node Server
  • Restarting Your Node.js App at Reboot

Running Your Node.js Application with Forever

Forever is a simple CLI tool for ensuring that a given script runs continuously (i.e. forever).

First, let's install forever globally:

npm install forever -g

We can use forever to run scripts continuously (whether it is written in node.js or not).

To start a script with forever you need to follow these steps:

Let's move to Node.js application folder,

cd /path/to/our/node/application/

and run the main script file with forever:

forever start main.js Where main.js is the file which define node.js server and its setup.

Now open http://[our-server-ip]:[port] in your browser to see your app running.
If you are running application from local machine, http://[localhost]:[port] will work.

Map a Domain to nginx and add proxy for Node Server

I am assuming that nginx is already installed and running, if not we can install it using
apt-get install nginx (Ubuntu)

Now lets setup the domain for nginx. Let's create a file for our domain in /etc/nginx/conf.d/ or we can use default server block configuration file.

Just add following code in that file,

server { listen 80; server_name meabhishek.com; }

This configures the web server to respond to requests at its root. Assuming our server is available at http://meabhishek.com, accessing it via a web browser would send the request to nginx server.

At this point its not going anywhere and not linked to our Node setup already running.

We have to set up a reverse proxy with nginx and to do that we will again edit the same file inside /etc/nginx/conf.d/.

Just add,

location / { proxy_pass http://localhost:{YOUR_PORT}; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } after server name, final file will look like this,

server { listen 80; server_name meabhishek.com; location / { proxy_pass http://localhost:[YOUR_PORT]; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }

Here, YOUR_PORT is the port Node.js app is listening to.

Now when http://meabhishek.com will be accessed via a web browser, web browser would send the request to the application server’s private IP address on given port, which would be received and replied to by the Node.js application.

Once setup is done, restart nginx by running following command,
service nginx restart

Restarting Your Node.js App at Reboot

So forever will take care of node.js application in case of any crash but when system reboots, Forever itself is killed. Who will restart our app now?

There are many option for doing this but I would suggest using crontab.

To open crontab and start editing:
crontab -e

Append the following code to crontab:

@reboot /usr/local/bin/forever start /path/to/main.js

This will restart our node.js application everytime after reboot.

We now have our Node.js application running behind an Nginx reverse proxy