Cloud Infrastructure Community's Space

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.

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 NodeJS 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 Ubuntu/Debian
---------- 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 = '<your_server_ip_address>';
const port = 5000;

const server = http.createServer((req, res) => {
	res.statusCode = 200;
  	res.setHeader('Content-Type', 'text/plain');
  	res.end('NodeJS App is up and running!');

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.

Now open a browser and access your application at the URL

Step 3: Install Nginx Reverse Proxy in Linux

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=<code>$releasever</code>/$basearch/ gpgcheck=0 enabled=1


name=nginx repo
baseurl=<code>$releasever</code>/$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 your_domain_here;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://<your_server_ip_address>:5000;

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.


Press ESC to close