Before you go, check out these stories!

Hackernoon logoConfigure Nginx as a Reverse Proxy for your Nodejs Application [A Step by Step Guide] by@programming

Configure Nginx as a Reverse Proxy for your Nodejs Application [A Step by Step Guide]

Author profile picture


Senior Full Stack

NGINX Open Source is an open source web server and reverse proxy that has grown in popularity in recent years because of its scalability, outstanding performance, and small footprint. NGINX Open Source was first created to solve the C10K problem (serving 10,000 simultaneous connections on a single web server).

NGINX Open Source’s features and performance have made it a staple of high‑performance sites – it’s the #1 web server at the 100,000 busiest websites in the world

Consider the following node js application

const http = require('http');

const hostname = 'localhost';
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}/`);

Now Install Nginx Reverse Proxy in Linux

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

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.

<strong>---------- On Debian/Ubuntu ----------</strong> 
$ sudo systemctl status nginx
$ sudo systemctl enable nginx
$ sudo 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.

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

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 localhost with your server IP and yourdomainname with your domain name).

server {
    listen 80;
    server_name sysmon.yourdomainname;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://localhost: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

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 localhost with your server IP and yourdomainname with your domain name as before).

localhost sysmon.yourdomainname

Congratulations! You had successfully configured Nginx as a
reverse proxy for your Nodejs application.


Join Hacker Noon

Create your free account to unlock your custom reading experience.