How to Set Up Nginx Proxy Pass with Node.js: A Quick Guide

When you’re building a Node.js application, using a reverse proxy server like Nginx can help improve performance, security, and scalability. In this guide, we’ll go through how to set up Nginx to proxy requests to your Node.js application.

Prerequisites

  • Node.js app running on a local port (e.g., 3000).

  • Nginx installed on your server.

Step 1: Install Nginx

If you don’t already have Nginx installed, you can install it with the following commands:

On Ubuntu/Debian:

sudo apt update sudo apt install nginx

On CentOS/RHEL:

sudo yum install nginx

Step 2: Run Your Node.js Application

Ensure your Node.js application is running. For example, if your app is using express, you would typically run it on port 3000:

node app.js

Step 3: Configure Nginx

Now, let’s set up Nginx to forward requests to your Node.js app.

  1. Create a new Nginx configuration file for your app:

sudo nano /etc/nginx/sites-available/myapp

  1. Add the following configuration in the file:
nginx

server {
listen 80;
server_name your-domain.com; # Replace with your actual domain or IP address

location / {
    proxy_pass http://127.0.0.1:3000;  # Point to your Node.js app running on port 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;
}

}

  • proxy_pass forwards the traffic from Nginx to your Node.js app running on port 3000.

  • proxy_set_header ensures that necessary headers (such as the Host header) are passed to the Node.js app correctly.

  1. Enable the new configuration by creating a symbolic link in the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/

  1. Test the Nginx configuration to make sure everything is correct:

sudo nginx -t

  1. Reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 4: Verify the Setup

Now, go to your browser and visit http://your-domain.com or http://<your-server-ip>. If everything is set up correctly, Nginx will forward the requests to your Node.js app running on port 3000.

Optional: Set Up SSL with Let’s Encrypt

To enable HTTPS, you can set up SSL using Let’s Encrypt.

  1. Install Certbot for automatic SSL configuration:

sudo apt install certbot python3-certbot-nginx

  1. Run Certbot to automatically configure SSL for your domain:

sudo certbot --nginx -d your-domain.com

  1. Certbot will automatically update the Nginx configuration to support HTTPS. Once done, you can verify the setup by visiting https://your-domain.com.

Conclusion

By configuring Nginx as a reverse proxy for your Node.js app, you can easily manage traffic, improve performance, and secure your application with SSL. Nginx will handle incoming requests, forwarding them to your Node.js application, while Node.js can focus on the core business logic of your app. This setup is a robust and scalable solution for production environments.

3 Likes