How to Use Nginx with Express for Node.js Apps
To use
nginx with an express app, configure nginx as a reverse proxy that forwards HTTP requests to your Express server running on a specific port. This setup improves performance, handles static files, and adds security by isolating your app from direct internet access.Syntax
Here is the basic syntax to configure nginx as a reverse proxy for an express app:
server: Defines the server block for your domain or IP.listen: The portnginxlistens on (usually 80 for HTTP).location /: Matches all requests to forward.proxy_pass: The URL wherenginxforwards requests (your Express app address).proxy_set_header: Passes headers likeHostandX-Real-IPto Express for correct request info.
nginx
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;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}Example
This example shows a simple Express app running on port 3000 and an nginx config forwarding requests from port 80 to the app. It demonstrates how nginx acts as a middleman between users and your Express server.
javascript and nginx
// app.js - Express server import express from 'express'; const app = express(); const PORT = 3000; app.get('/', (req, res) => { res.send('Hello from Express behind Nginx!'); }); app.listen(PORT, () => { console.log(`Express server running on http://localhost:${PORT}`); }); # nginx.conf snippet server { listen 80; server_name localhost; 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; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
Output
Express server running on http://localhost:3000
When accessing http://localhost in a browser, you see:
Hello from Express behind Nginx!
Common Pitfalls
Common mistakes when using nginx with express include:
- Not forwarding headers like
HostandX-Real-IP, causing Express to get wrong client info. - Forgetting to set
proxy_http_version 1.1and connection upgrade headers, which breaks WebSocket support. - Running Express on the same port as
nginx(usually 80), causing port conflicts. - Not restarting
nginxafter config changes.
Example of a wrong config missing headers:
nginx
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:3000;
# Missing proxy_set_header lines
}
}
# Corrected config includes:
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;Quick Reference
Tips for using nginx with express:
- Run Express on a port like 3000, not 80.
- Use
nginxto handle static files and SSL termination. - Always forward important headers for correct client info.
- Reload
nginxafter config changes withsudo nginx -s reload. - Test your setup locally before deploying.
Key Takeaways
Use nginx as a reverse proxy to forward requests to your Express app running on a separate port.
Always set proxy headers in nginx to pass client info correctly to Express.
Run Express on a non-privileged port like 3000 and let nginx listen on port 80 or 443.
Reload nginx after any configuration changes to apply them.
Use nginx to improve security, performance, and enable features like SSL termination.