How to Configure Nginx as Reverse Proxy for Nodejs App

Nodejs is a free open source, lightweight, scalable and efficient JavaScript framework built on Chrome’s V8 JavaScript engine, and uses an event-driven, non-blocking I/O model. Nodejs is now everywhere, and has become so popular for developing software from websites, web apps to network apps and more.

Nginx is an open source, high-performance HTTP server, load balancer and reverse proxy software. It has a straightforward configuration language making it easy to configure. In this article, we will show how to configure Nginx as a reverse proxy for Nodejs applications.

Read Also: The Ultimate Guide to Secure, Harden and Improve Performance of Nginx Web Server

Note: If your system already running with Nodejs and NPM, and have your app running on a certain port, go straight to Step 4.

Step 1: Installing Nodejs and NPM in Linux

The latest version of Node.js and NPM is available to install from the official NodeSource Enterprise Linux, Fedora, Debian and Ubuntu binary distributions repository, which is maintained by the Nodejs website and you will need to add it to your system to be able to install the latest Nodejs and NPM packages as shown.

On Debian/Ubuntu

---------- Install Node.js v11.x ---------- 
$ curl -sL | sudo -E bash -
$ sudo apt-get install -y nodejs

---------- Install Node.js v10.x ----------
$ curl -sL | sudo -E bash -
$ sudo apt-get install -y nodejs

On CentOS/RHEL and Fedora

---------- Install Node.js v11.x ---------- 
$ curl -sL | bash -

---------- Install Node.js v10.x ----------
$ curl -sL | bash -

Step 2: Creating a Nodejs Application

For demonstration purpose, we will be creating a sample application called “sysmon”, which will run on port 5000 as shown.

$ sudo mkdir -p /var/www/html/sysmon
$ sudo vim /var/www/html/sysmon/server.js

Copy and paste the following code in the server.js file (replace with your server IP).

const http = require('http');

const hostname = '';
const port = 5000;

const server = http.createServer((req, res) => {
	res.statusCode = 200;
  	res.setHeader('Content-Type', 'text/plain');
  	res.end('Sysmon App is Up and Running!\n');

server.listen(port, hostname, () => {
  	console.log(`Server running at http://${hostname}:${port}/`);

Save the file and exit.

Now start your node application using the following command (press Ctrl+x to terminate it).

$ sudo node /var/www/html/sysmon/server.js
$ sudo node /var/www/html/sysmon/server.js &   #start it in the background to free up your terminal

Now open a browser and access your application at the URL

Access Node App from Browser

Access Node App from Browser

Step 3: Install Nginx Reverse Proxy in Linux

We will install the latest version of Nginx from the official repository, as shown below.

On Debian/Ubuntu

Create a file called /etc/apt/sources.list.d/nginx.list and add the following lines to it.

deb bionic nginx
deb-src  bionic nginx

Next, add the repository signing key, update your system package index and install the nginx package as follows.

$ wget --quiet && sudo apt-key add nginx_signing.key
$ sudo apt update
$ sudo apt install nginx

On CentOS/RHEL and Fedora

Create a file named /etc/yum.repos.d/nginx.repo and paste one of the configurations below.

name=nginx repo
baseurl=$releasever/$basearch/ gpgcheck=0 enabled=1
name=nginx repo
baseurl=$releasever/$basearch/ gpgcheck=0 enabled=1

Note: Due to differences between how CentOS and RHEL, it is necessary to replace $releasever with either 6 (for 6.x) or 7 (for 7.x), depending upon your OS version.

Next, add the repository signing key and install the nginx package as shown.

# wget --quiet && rpm --import nginx_signing.key
# yum install nginx

After successfully installing Nginx, start it, enable it to auto-start at system boot and check if it is up and running.

---------- On Debian/Ubuntu ---------- 
$ sudo systemctl status nginx
$ sudo systemctl enable nginx
$ sudo systemctl status nginx

---------- On CentOS/RHEL ---------- 
# systemctl status nginx
# systemctl enable nginx
# systemctl status nginx

If you are running a system firewall, you need to open port 80 (HTTP), 443 (HTTPS) and 5000 (Node app), which the web server listens on for client connection requests.

---------- On Debian/Ubuntu ---------- 
$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp
$ sudo ufw allow 5000/tcp
$ sudo ufw reload

---------- On CentOS/RHEL ---------- 
# firewall-cmd --permanent --add-port=80/tcp
# firewall-cmd --permanent --add-port=443/tcp
# firewall-cmd --permanent --add-port=5000/tcp
# firewall-cmd --reload 

Step 4: Configure Nginx as Reverse Proxy For Nodejs Application

Now create a server block configuration file for your Node app under /etc/nginx/conf.d/ as shown.

$ sudo vim /etc/nginx/conf.d/sysmon.conf 

Copy and paste the following configuration (change with your server IP and tecmint.lan with your domain name).

server {
    listen 80;
    server_name sysmon.tecmint.lan;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;

Save the changes and exit the file.

Finally, restart the Nginx service to effect the recent changes.

$ sudo systemctl restart nginx
# systemctl restart nginx

Step 5: Access Nodejs Application via Web Browser

Now you should be able to access your Node app without providing the port it is listening on, in the URL: this is a much convenient way for users to access it.


For your test domain name to work, you need to setup local DNS using the /etc/hosts file, open it and add the line below in it (remember to change with your server IP and tecmint.lan with your doamin name as before). sysmon.tecmint.lan
Access Node App via Nginx Reverse Proxy

Access Node App via Nginx Reverse Proxy

That’s all! In this article, we showed how to configure Nginx as a reverse proxy for Nodejs applications. Use the feedback form below to ask any questions or share your thoughts about this article.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Ravi Saive

I am Ravi Saive, creator of TecMint. A Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux. Follow Me: Twitter, Facebook and Google+

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

Got something to say? Join the discussion.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.