Home / Hosting & Cloud / Reverse Proxy Node.js
Hosting & Server
Reverse Proxy Nginx in front of Node.js: the complete guide
Node.js is not made to be exposed directly on the Internet. Nginx in reverse proxy manages SSL, compression and websockets for you.
Reverse Proxy Basic Configuration
server {
listen 80;
server_name monapp.com;
location / {
proxy_pass http://127.0.0.1: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 SSL with CertBot
sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d monapp.com
Tip: Upgrade and Connection headers are essential for websockets (socket.io) to work properly through the proxy.
frequently asked questions
Do you need PM2 in addition to Nginx?
Yes, PM2 handles the node.js (automatic restart, logs) process while nginx handles incoming HTTP/HTTPS traffic.
Should port 3000 remain open publicly?
No, it must remain accessible only locally (127.0.0.1); Only Nginx must be exposed on ports 80/443.