You want zero-downtime deploys without committing to Kubernetes. Blue/green is the simplest pattern that works: two complete copies of the app (blue and green) running side-by-side, Nginx routing 100% of traffic to whichever is live, and the cutover is a one-line config change plus an Nginx reload. This article walks the layout, the cutover script, the health gate that prevents promoting a broken green, and the rollback in seconds.
How to verify
For a host already running blue/green, the layout is observable from outside:
ss -tlnp | grep -E ':(8081|8082)\s'
sudo nginx -T 2>/dev/null | grep -A3 'upstream app'
curl -s http://127.0.0.1:8081/healthz
curl -s http://127.0.0.1:8082/healthz
readlink /etc/nginx/upstreams/active.conf
journalctl -u nginx --since='1 hour ago' | grep reload
The two app instances should be on different ports, both responding to a health endpoint, and the upstream config should be a symlink pointing to either blue.conf or green.conf.
What’s happening
The blue/green pattern: two identical app environments, named blue and green, listening on different local ports (or different hosts). At any moment, one is live (receiving traffic) and the other is idle. To deploy: build the new version into the idle environment, run its health checks, then flip Nginx to point at it. Rollback is the inverse flip.
Nginx is the cutover mechanism. The site config references an upstream block via an include of a symlink; the symlink points to either blue.conf or green.conf. Flipping the symlink and reloading Nginx switches traffic — atomic for new connections, drain for existing keepalive connections. The reload is a SIGHUP that forks new worker processes with the new config; old workers finish their connections and exit.
The procedure
-
Run two app instances. Same binary, different systemd unit names with different
EnvironmentFiles pointing at different ports.# /etc/systemd/system/myapp-blue.service [Unit] Description=My app — blue After=network-online.target [Service] EnvironmentFile=/etc/myapp/blue.env ExecStart=/usr/local/bin/myapp Restart=on-failure User=myapp# /etc/myapp/blue.env PORT=8081 # /etc/myapp/green.env PORT=8082 -
Set up the Nginx upstream layout. Two upstream files and a symlink chosen by an include.
# /etc/nginx/upstreams/blue.conf upstream app { server 127.0.0.1:8081 max_fails=2 fail_timeout=5s; keepalive 32; }# /etc/nginx/upstreams/green.conf upstream app { server 127.0.0.1:8082 max_fails=2 fail_timeout=5s; keepalive 32; }# /etc/nginx/sites-enabled/myapp.conf server { listen 443 ssl http2; server_name app.example.com; # TLS config ... location / { include /etc/nginx/upstreams/active.conf; proxy_pass http://app; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } location = /__which { internal; return 200 "active: $upstream_addr\n"; } }sudo ln -sf /etc/nginx/upstreams/blue.conf /etc/nginx/upstreams/active.conf -
The cutover script. Builds health check, then flip + reload.
#!/usr/bin/env bash set -Eeuo pipefail CURRENT="$(basename "$(readlink -f /etc/nginx/upstreams/active.conf)" .conf)" case "$CURRENT" in blue) NEW=green; NEW_PORT=8082 ;; green) NEW=blue; NEW_PORT=8081 ;; *) echo "Unexpected current upstream: $CURRENT" >&2; exit 1 ;; esac echo "Deploying to ${NEW} (port ${NEW_PORT})" sudo systemctl restart "myapp-${NEW}.service" # Health gate — fail if not ready in 30s for i in {1..30}; do if curl -fsS "http://127.0.0.1:${NEW_PORT}/healthz" >/dev/null; then break fi sleep 1 if [[ "$i" -eq 30 ]]; then echo "Health check failed on ${NEW}; aborting cutover" >&2 exit 1 fi done echo "Cutting over to ${NEW}" sudo ln -sfn "/etc/nginx/upstreams/${NEW}.conf" /etc/nginx/upstreams/active.conf sudo nginx -t sudo systemctl reload nginx echo "Cutover complete: ${CURRENT} -> ${NEW}" -
Verify the cutover. Externally, a request to the public URL should now hit the new upstream; internally, the
__whichendpoint confirms.curl -s -k https://app.example.com/__which -
Rollback. Re-run the cutover script. It reads the active symlink, picks the other side, and flips back — symmetric by construction.
-
Drain time for long-lived connections. Nginx finishes existing in-flight requests under the old worker; long-lived (WebSocket, SSE) connections may linger. Configure
worker_shutdown_timeout(default 4h is fine) or actively kick clients via app-side close logic if needed.
Common pitfalls
- The two environments must share zero stateful resources except the database — a session store local to one side breaks during cutover.
- Database migrations that are backwards-incompatible (drop a column, rename a table) break the side still serving the old version; use expand/contract migrations.
- A health endpoint that returns 200 before the app is actually ready (no DB pool warmed, no caches primed) leads to a cutover that breaks under traffic — make
/healthzdeep enough. - The Nginx reload is graceful for HTTP but instant for
worker_processes 1configurations — usually fine, but worth measuring on slow-keep-alive workloads. - A long TTL on a CDN edge cache pointing at the same hostname cannot blue/green at the origin without surrogate-key purging — the cutover is real only when the edge sees the new origin response.
In the engagements we run, blue/green via Nginx is the default deployment pattern for single-host services and small fleets where Kubernetes is more than the workload needs. The cutover script lives under Git, the rollback is rehearsed, and the health gate is what prevents promoting a broken build. See /en/services/managed-operations/ and /en/knowledge-base/immutable-infrastructure/.