Nginx Reverse Proxy

Node js 9 min min read Updated: Mar 30, 2026 Advanced
Nginx Reverse Proxy
Advanced Topic 4 of 8

Nginx Reverse Proxy for Node.js in Production

When a Node.js application is deployed to production, it is usually not exposed directly to the internet. Instead, a web server like Nginx is placed in front of it as a reverse proxy. This setup improves security, performance, and production reliability.

Nginx is one of the most widely used web servers and reverse proxies. It can forward client requests to your Node.js application, handle SSL termination, serve static files efficiently, and help with load balancing.

Key Concept: Nginx acts as a reverse proxy in front of a Node.js app, forwarding requests to the app while improving security, stability, and performance.

What is a Reverse Proxy?

A reverse proxy is a server that sits between the client and your backend application. Instead of the client connecting directly to Node.js, the client sends requests to Nginx, and Nginx forwards those requests to the Node.js server.

In simple words:

  • Client sends request to Nginx
  • Nginx receives the request
  • Nginx forwards it to the Node.js app
  • Node.js sends a response back through Nginx

Why Use Nginx with Node.js?

  • Better security: Node.js app is not directly exposed to the public
  • SSL termination: Nginx can manage HTTPS certificates
  • Performance: Nginx handles static files efficiently
  • Load balancing: Requests can be distributed across multiple Node.js instances
  • Production stability: Nginx works well with PM2 and clustered apps

How Nginx Works with Node.js

In a typical production setup:

  • Nginx listens on port 80 for HTTP or 443 for HTTPS
  • Node.js application runs on an internal port like 3000
  • Nginx forwards traffic from public ports to the Node.js app

This allows the Node.js app to stay internal while Nginx handles public web traffic.

Basic Node.js Example

Suppose your Node.js application is running on port 3000:

javascript const express = require("express"); const app = express(); app.get("/", (req, res) => { res.send("Hello from Node.js app"); }); app.listen(3000, () => { console.log("App running on port 3000"); });

Basic Nginx Reverse Proxy Configuration

A basic Nginx server block for reverse proxy looks like this:

nginx server { listen 80; server_name example.com; 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; } }

Understanding the Nginx Configuration

1. listen 80

Tells Nginx to listen on port 80 for HTTP traffic.

2. server_name

Defines the domain name that should use this configuration.

3. location /

Matches all requests coming to the root path.

4. proxy_pass

Forwards incoming requests to the Node.js app running on port 3000.

5. proxy_set_header

Passes important headers from the client request to the Node.js application.

Why Headers Matter in Reverse Proxy

When using a reverse proxy, your Node.js application may need the original host, protocol, or upgrade headers. These headers help the application handle client details correctly, especially for WebSockets and secure routing.

Serving Static Files with Nginx

Nginx is much faster than Node.js at serving static files like images, CSS, JavaScript bundles, or downloadable assets.

nginx server { listen 80; server_name example.com; location /static/ { alias /var/www/app/static/; } location / { proxy_pass http://localhost:3000; } }

This setup allows Nginx to serve static files directly and forward dynamic requests to Node.js.

Using Nginx with PM2

In many production setups, Node.js applications are managed by PM2 and then proxied through Nginx.

Example flow:

  • PM2 keeps the Node.js app alive on port 3000
  • Nginx receives incoming public traffic
  • Nginx forwards the request to the PM2-managed Node.js process

This combination is very common in production environments.

Using Nginx with HTTPS

One of the biggest advantages of Nginx is that it can handle SSL certificates and HTTPS traffic.

nginx server { listen 443 ssl; server_name example.com; ssl_certificate /etc/ssl/certs/fullchain.pem; ssl_certificate_key /etc/ssl/private/privkey.pem; location / { proxy_pass http://localhost:3000; } }

In this setup, Nginx handles HTTPS, and the Node.js application can continue running internally over plain HTTP.

Redirecting HTTP to HTTPS

It is common to redirect all HTTP traffic to HTTPS:

nginx server { listen 80; server_name example.com; return 301 https://$host$request_uri; }

Nginx with Load Balancing

Nginx can also distribute requests across multiple Node.js instances. This is useful when your app is running in cluster mode or across multiple servers.

nginx upstream node_backend { server localhost:3000; server localhost:3001; server localhost:3002; } server { listen 80; server_name example.com; location / { proxy_pass http://node_backend; } }

This improves scalability and availability.

Nginx and WebSocket Support

If your application uses WebSockets, such as Socket.io, Nginx needs special headers to support connection upgrades.

nginx location /socket.io/ { 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; }

Without these headers, WebSocket connections may not work correctly.

Best Practices for Using Nginx with Node.js

  • Keep Node.js running on an internal port
  • Use Nginx for SSL termination
  • Serve static files through Nginx when possible
  • Use PM2 or another process manager with Node.js
  • Enable HTTPS in production
  • Configure proper headers for WebSockets and real client IPs

Common Mistakes

  • Exposing Node.js directly to the public internet
  • Not forwarding required headers
  • Misconfiguring HTTPS certificates
  • Forgetting WebSocket upgrade headers
  • Not testing proxy configuration after changes

Real-World Use Cases

  • Production APIs built with Express.js
  • Node.js apps running behind PM2
  • Socket.io apps using WebSockets
  • Load-balanced Node.js services
  • Static + dynamic site setups

Nginx vs Direct Node.js Exposure

Feature Direct Node.js Nginx Reverse Proxy
Public traffic handling Basic Better
HTTPS support Possible but less convenient Excellent
Static file serving Slower Fast and efficient
Load balancing Manual or external Built-in support
Production readiness Lower Higher

Conclusion

Nginx reverse proxy is a standard production setup for Node.js applications. It improves security, performance, and reliability by managing public traffic before forwarding requests to the Node.js server.

If you are deploying Node.js applications to production, learning how to use Nginx as a reverse proxy is an important skill. It works especially well with PM2, HTTPS, load balancing, and scalable server architecture.

Quick Summary: Nginx acts as a reverse proxy in front of Node.js apps, helping with HTTPS, security, static file serving, and better production performance.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators