Introduction
When deploying Node.js applications to production, one of the most important steps is setting up a reverse proxy server. Nginx is one of the most popular and powerful web servers used for this purpose.
In simple words, Nginx acts as a middle layer between users and your Node.js application. It receives requests from users and forwards them to your application, then sends the response back to the user.
Using Nginx as a reverse proxy improves performance, security, and scalability of your Node.js applications.
In this article, we will learn step-by-step how to configure Nginx as a reverse proxy for Node.js applications with practical examples and best practices.
What is a Reverse Proxy?
A reverse proxy is a server that sits between client (browser) and backend server (Node.js app).
How it Works
User sends request to Nginx
Nginx forwards request to Node.js app
Node.js processes request
Nginx sends response back to user
This helps in handling traffic, improving security, and hiding backend implementation.
Why Use Nginx with Node.js?
Prerequisites
Before starting, make sure you have:
Step 1: Create a Simple Node.js Application
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from Node.js App');
});
server.listen(3000, () => {
console.log('Server running on port 3000');
});
Run the app:
node app.js
Step 2: Install Nginx
On Ubuntu:
sudo apt update
sudo apt install nginx
Start Nginx:
sudo systemctl start nginx
Step 3: Configure Nginx Reverse Proxy
Open Nginx config file:
sudo nano /etc/nginx/sites-available/default
Update configuration:
server {
listen 80;
server_name your_domain.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;
}
}
Save and exit.
Step 4: Test and Restart Nginx
Check config:
sudo nginx -t
Restart Nginx:
sudo systemctl restart nginx
Now open browser and visit your domain or IP.
Step 5: Run Node.js App in Background
Use PM2 (recommended):
npm install -g pm2
pm2 start app.js
pm2 save
pm2 startup
This keeps your app running even after server restart.
Serving Static Files with Nginx
You can let Nginx serve static files for better performance.
location /static/ {
root /var/www/myapp;
}
Enable HTTPS using SSL (Important for SEO)
Install Certbot:
sudo apt install certbot python3-certbot-nginx
Run command:
sudo certbot --nginx -d your_domain.com
This will automatically configure SSL.
Load Balancing with Multiple Node.js Instances
upstream nodeapp {
server localhost:3000;
server localhost:3001;
}
server {
listen 80;
location / {
proxy_pass http://nodeapp;
}
}
Common Mistakes Developers Make
Forgetting to restart Nginx after changes
Wrong proxy_pass URL
Not handling headers properly
Not using PM2 for production
Ignoring SSL setup
Best Practices for Nginx Reverse Proxy
Why This Setup is Important
Improves performance of Node.js apps
Handles large traffic efficiently
Provides production-ready setup
Enhances security
Summary
Configuring Nginx as a reverse proxy for Node.js applications is an essential step for deploying scalable and secure applications. It acts as a bridge between users and your backend, improving performance and reliability.
By following this step-by-step guide, you can easily set up Nginx with Node.js and build production-ready applications with better speed, security, and scalability.