Muhammad Tabarak - Home
DevOps

Zero-Downtime Deployments: Rolling PM2 Clusters with Nginx on VPS

Deploying production Node.js applications with zero downtime using rolling PM2 cluster reloads and Nginx upstream reverse proxying.

Muhammad Tabarak
Muhammad TabarakFounder & Software Engineer
November 7, 2026
3 min read
Zero-Downtime Deployments: Rolling PM2 Clusters with Nginx on VPS

Deploying code updates to production should never require a maintenance window or risk throwing 502 Bad Gateway errors to active users. When running production Node.js applications on Virtual Private Servers (VPS), a standard pm2 restart all abruptly terminates every running worker process, dropping all active WebSocket connections and in-flight HTTP requests.

For CloudMaintain, we engineered an automated zero-downtime deployment pipeline combining Nginx upstream reverse proxying with PM2 rolling cluster reloads. Here is how we structured our deployment scripts to guarantee zero dropped packets during production releases.

The Anatomy of a Rolling Cluster Reload

Unlike a restart—which stops all workers before booting new ones—a rolling reload updates worker processes sequentially. PM2 signals Worker 0 with a SIGINT, waits for it to finish serving active requests, spawns a new instance running the updated code, verifies that the new worker is ready to receive traffic, and only then proceeds to reload Worker 1.

Deployment Lifecycle
Rendering interactive flow...
Figure: PM2 Rolling Cluster Reload Sequence with Readiness Handshake

Configuring the Graceful Shutdown Handshake

Rolling reloads only function correctly if your Node.js application actively cooperates with the process manager. Without an explicit readiness signal and shutdown listener, PM2 will assume a new worker is healthy as soon as the Node process spawns, before it has actually connected to the database or bound to its network socket.

Our backend entry point establishes an explicit lifecycle contract:

Infrastructure Flow
Rendering interactive flow...
Figure: Production VPS Reverse Proxy & Cluster Architecture

Automated Deployment Script Safeguards

  • Pre-Flight Typecheck & Test Verification: The deployment shell script executes npm run test and npx tsc --noEmit on an isolated staging directory prior to triggering the reload. If compilation fails, the script aborts immediately without touching running production processes.
  • Nginx Proxy Buffering & Timeouts: Nginx is tuned with proxy_next_upstream error timeout http_502;. If an individual worker fails during a reload, Nginx automatically retries the request against a neighboring healthy worker before returning any response to the user.
  • Instant One-Command Rollback: A symlinked directory structure (/current pointing to timestamped releases in /releases) allows instant rollback to the previous working build in under 300ms if post-deploy health checks fail.
Enjoyed this article?Add this site as a preferred source so Google surfaces my posts first — in Search & AI Mode.
Add as preferred source→