How To Setup Nginx Reverse Proxy With Node

How To Setup Nginx Reverse Proxy With Node
Photo by Михаил Секацкий / Unsplash

Nginx is a web server and reverse proxy server. It is known for its high performance, stability, and low resource consumption. Nginx can be used to handle large amounts of concurrent connections, and is often used to improve the performance of web applications by serving as a load balancer or caching server. It is open-source software and can run on various operating systems including Windows, Linux, and macOS.

What Is A Reverse Proxy?

A reverse proxy is a type of proxy server that sits in front of one or more web servers and directs client requests to the appropriate server. The client connects to the reverse proxy, which then forwards the request to the appropriate web server and returns the server's response to the client. Reverse proxies can be used for a variety of purposes, including load balancing, SSL termination, and caching. They can also be used to provide additional security, by hiding the IP addresses of the web servers and filtering incoming requests.

Benefits Of Using A Reverse Proxy?

  • Load balancing: A reverse proxy can distribute incoming client requests evenly across multiple web servers, improving the overall performance and availability of the web application.

  • SSL termination: A reverse proxy can handle SSL encryption and decryption, relieving the web servers from this processing overhead.

  • Caching: A reverse proxy can cache frequently requested content, reducing the load on the web servers and improving the response time for clients.

  • Security: A reverse proxy can filter incoming requests and hide the IP addresses of the web servers, making it more difficult for attackers to target the web servers directly.

  • Extensibility: A reverse proxy can be used to add additional functionality to a web application, such as compression or authentication.

  • Flexibility: A reverse proxy can be used to route requests to different servers based on the request's URL or other information, which makes it easier to split a web application into multiple components or add new components without modifying the existing code.

How To Deploy A Reverse Proxy In Nginx?

Optional - Removing Default Config File

Locate the default site configuration file: The default site configuration file is typically located at /etc/nginx/sites-enabled/default.

This file is a symlink to another file /etc/nginx/sites-available/default.

You can safely remove the default file by sudo rm /etc/nginx/sites-enabled/default

Making A New Config File

sudo vi /etc/nginx/sites-available/node-server

Let's assume your nodeJs is running on Port:3000

Paste the following code in that file

server {
   listen 80;
   server_name <server_name>;

  location / {
       proxy_pass http://localhost:3000;
       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;


Change <server_name> with

  • localhost - To Run on Available Interfaces
  • - To run only on the domain //This is better

Enabling This Site

  • cd /etc/nginx/sites-available
  • ln -s node-server /etc/nginx/sites-enabled/node-server

That's It 😊. Restart Nginx and you should be good to go.

sudo service nginx restart