A Node.js HTTP server can speak TLS, serve static files, and handle 30 000 concurrent connections directly — but in production it almost never does. Putting Nginx in front gives you a place to terminate TLS, serve static assets, rate-limit, and rotate Node versions without dropping connections. This article wires a single-host Node app behind Nginx on Ubuntu, with the settings that matter for WebSockets, slow clients, and graceful reloads.
How to verify
Inspect the current state before changing anything:
sudo nginx -t
sudo systemctl status nginx --no-pager | head
ss -lntp | grep -E ':80|:443|:3000'
curl -sI http://127.0.0.1:3000/ | head
sudo nginx -T 2>/dev/null | grep -E 'proxy_pass|upstream|listen' | head -20
If Node is listening on 0.0.0.0:3000, that is a leak — once Nginx is fronting it, the upstream should bind to 127.0.0.1:3000 only and the firewall should not expose port 3000.
What’s happening
Nginx in front of Node solves five problems at once. TLS termination so Node does not need to handle certificate rotation. Slow-client buffering so a 56 Kbps mobile uplink does not occupy a Node worker for the full request body. Static asset offload so /assets/* does not waste a Node turn of the event loop. Request shaping (rate limits, body size caps) at the edge. And zero-downtime reload — nginx -s reload lets the OS keep listening sockets open while a new worker pool spins up.
The handoff between the two is plain HTTP/1.1 (or HTTP/2 with proxy_http_version 2.0, but 1.1 is almost always fine over localhost). The proxy sets Host, X-Real-IP, and X-Forwarded-* headers so the Node app can see the real client. WebSocket connections need explicit Upgrade and Connection headers preserved, which is the most common configuration mistake on the boundary.
The procedure
-
Make Node bind to localhost only. Edit the application’s listen call to
app.listen(3000, '127.0.0.1')(or setHOST=127.0.0.1). Restart and confirm:sudo ss -lntp | grep :3000 # 127.0.0.1:3000 users:(("node",pid=...,fd=...)) -
Install Nginx and a Let’s Encrypt client if not already present:
sudo apt update sudo apt install -y nginx python3-certbot-nginx -
Drop the server block at
/etc/nginx/sites-available/app.conf:upstream app_node { server 127.0.0.1:3000 fail_timeout=0; keepalive 32; } server { listen 80; server_name app.example.com; return 301 https://$host$request_uri; } server { listen 443 ssl http2; server_name app.example.com; # certbot fills these in ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem; client_max_body_size 25m; location / { proxy_pass http://app_node; proxy_http_version 1.1; 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; proxy_set_header Connection ""; proxy_read_timeout 60s; proxy_send_timeout 60s; } location /ws/ { proxy_pass http://app_node; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_read_timeout 3600s; } } -
Enable the site and reload Nginx:
sudo ln -s /etc/nginx/sites-available/app.conf /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx -
Provision the certificate. Certbot will rewrite the SSL paths in the file:
sudo certbot --nginx -d app.example.com -m ops@example.com --agree-tos -n sudo certbot renew --dry-run
Operational notes
proxy_set_header Connection ""on the main location enables Nginx upstream keepalive — without it, Nginx opens a new TCP connection to Node for every request and the event loop wastes time on the handshake.- Without
proxy_read_timeouttuning, an SSE endpoint dies at 60 s. Either set the timeout higher per-location or have the client implement reconnection on close — both are valid, but pick one consciously. - If the app reads
req.ipfor rate limiting, configure trust ofX-Forwarded-Forin the framework (app.set('trust proxy', '127.0.0.1')in Express) or every client looks like the proxy. client_max_body_sizedefaults to 1 MB. Uploads beyond that get a 413 from Nginx before Node ever sees the request — match it to the application’s actual upload limit.- Reload, not restart.
systemctl reload nginxre-reads the config without dropping live connections;restartcloses the listening sockets briefly and TLS handshakes in flight fail.
This pairs naturally with PM2 process management for the upstream side. On the hosts Stack Harbor operates for clients under managed operations, the Node-behind-Nginx pattern is the default for single-VM apps — and the certbot renewal hook is monitored, so the cert never quietly expires at 03:00 on a Sunday.