Contact Us

How to Configure Nginx as Reverse Proxy for Nodejs App

Mobile App | January 21, 2021

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.

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

On CentOS/RHEL and Fedora

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.

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

Save the file and exit.

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

Now open a browser and access your application at the URL http://198.168.43.31:5000.

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.

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

On CentOS/RHEL and Fedora

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

CentOS
RHEL

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.

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

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.

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.

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

Save the changes and exit the file.

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

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 192.168.43.31 with your server IP and tecmint.lan with your doamin name as before).

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.

This content was originally published here.