Two Next.js instances run behind nginx under PM2 in cluster mode. A release restarts them one at a time, so there is always an instance answering. The detail that makes it work is not obvious.
Do not start it with npm
{
// Not "npm" with args "start".
script: "node_modules/next/dist/bin/next",
args: "start",
exec_mode: "cluster",
instances: 2,
}Running npm start makes npm the supervised process and next start a child of it. PM2 can only share a listening socket across processes it forked itself, so with npm in between, cluster mode stops load-balancing and a rolling reload stops being rolling. Invoking the Next.js binary directly puts the actual server under PM2's control.
The reload
pm2 reload restarts instances sequentially and waits for each to come up before moving to the next. With two instances, one is always serving. A restart, by contrast, stops everything and then starts it — the difference is a few seconds of refused connections on every deploy.