Introduction
When deploying Node.js applications to production, performance, security, and scalability become very important. Running a Node.js app directly on the internet is not recommended because it lacks advanced features like load balancing, SSL handling, and request management.
This is where Nginx comes in. Nginx is a powerful web server that can act as a reverse proxy, sitting between users and your Node.js application. It improves performance, handles traffic efficiently, and adds an extra layer of security.
In this article, you will learn how to use Nginx as a reverse proxy for Node.js applications in production with step-by-step guidance and practical examples.
What is a Reverse Proxy?
A reverse proxy is a server that receives requests from clients and forwards them to backend servers.
Flow:
User → Nginx → Node.js App → Response → Nginx → User
Nginx hides your backend server and manages all incoming traffic.
Why Use Nginx with Node.js?
Improves performance with caching
Handles multiple requests efficiently
Provides SSL/TLS support (HTTPS)
Enables load balancing
Protects backend servers
Serves static files faster
Prerequisites
Before starting, ensure you have:
Node.js application ready
Linux server (Ubuntu recommended)
Basic knowledge of terminal commands
Domain name (optional but recommended)
Step 1: Install Nginx
Update your system and install Nginx:
sudo apt update
sudo apt install nginx -y
Check status:
sudo systemctl status nginx
Start and enable Nginx:
sudo systemctl start nginx
sudo systemctl enable nginx
Step 2: Run Your Node.js Application
Start your Node.js app on a specific port (e.g., 3000):
node app.js
Example Express server:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from Node.js app');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Step 3: Configure Nginx as Reverse Proxy
Create a new configuration file:
sudo nano /etc/nginx/sites-available/myapp
Add configuration:
server {
listen 80;
server_name yourdomain.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;
}
}
Enable the configuration:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
Test configuration:
sudo nginx -t
Restart Nginx:
Join the conversation! Your thoughts help the community grow.