TExcelTAD
Home › Web Servers in Depth: Nginx, HAProxy, Caddy & Traefik › Zero-Downtime Reloads & Upgrades

Zero-Downtime Reloads & Upgrades

A front-end proxy is changed often (a new route, a new certificate, a backend added) and restarted almost never. Every one of the four servers in this course can apply a new configuration without dropping connections, but each does it differently, and each has a way to get it wrong. This lesson shows Nginx's mechanism in detail and how the others compare.

What nginx -s reload does

reload sends SIGHUP to the master process. The master:

  1. reads and checks the new configuration; if it is invalid, it logs the error and keeps running the old one;
  2. opens any new listening sockets, then starts new workers with the new configuration;
  3. tells the old workers to shut down gracefully: they stop accepting connections, finish the requests in progress, and exit.

The listening sockets belong to the master and are never closed, so no connection is refused during the switch. The session proves it: a client sends requests in a tight loop while the configuration is reloaded five times, and a slow request that started before the reloads completes normally:

bash · try it yourself
cat > app.py <<'EOF'
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
class H(BaseHTTPRequestHandler):
    def do_GET(self):
        if self.path == "/slow": time.sleep(3)
        self.send_response(200); self.end_headers(); self.wfile.write(b"ok\n")
    def log_message(self, *a): pass
ThreadingHTTPServer(("127.0.0.1", 9000), H).serve_forever()
EOF
python3 app.py &
for i in $(seq 50); do curl -s 127.0.0.1:9000/ > /dev/null && break; sleep 0.1; done
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log error.log notice;
worker_processes 2;
events {}
http {
  access_log off;
  server {
    listen 127.0.0.1:8080;
    location / { proxy_pass http://127.0.0.1:9000; add_header X-Version v1; }
  }
}
EOF
nginx -p "$PWD" -c nginx.conf -e stderr
workers() { pgrep -P "$(cat nginx.pid)" | sort | tr '\n' ' '; }
before=$(workers)
curl -s -o slow.txt -w '%{http_code}' 127.0.0.1:8080/slow > slow.code &          # a long request in flight
(for i in $(seq 300); do curl -s -o /dev/null -w '%{http_code}\n' 127.0.0.1:8080/; done) > codes.txt &
for v in 2 3 4 5 6; do
  sleep 0.3; sed -i "s/X-Version v[0-9]*/X-Version v$v/" nginx.conf
  nginx -p "$PWD" -c nginx.conf -e stderr -s reload 2>/dev/null
done
wait %2 %3
echo "300 requests during 5 reloads: $(sort codes.txt | uniq -c | xargs)"
echo "slow request started before the reloads: $(cat slow.code) $(cat slow.txt)"
echo "now serving: $(curl -s -D - -o /dev/null 127.0.0.1:8080/ | grep -i x-version | tr -d '\r')"
after=$(workers)
echo "worker pids changed: $([ "$before" != "$after" ] && echo yes || echo no); workers now: $(echo $after | wc -w)"
grep -c 'SIGHUP' error.log
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/null; kill %1
300 requests during 5 reloads: 300 200
slow request started before the reloads: 200 ok
now serving: X-Version: v6
worker pids changed: yes; workers now: 2
5

All 300 requests succeeded, the slow request finished on its old worker, and the latest version answered afterwards. The error log (at level notice) has one signal 1 (SIGHUP) received …, reconfiguring line per reload: 5.

Graceful has a cost: old workers live as long as their slowest connection. WebSockets and long downloads can keep "shutting down" workers (and their memory) around for hours after frequent reloads. worker_shutdown_timeout 30s; (main context) caps that wait, closing what is left.

A broken reload: loud or silent

bash · try it yourself
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log error.log;
events {}
http {
  access_log off;
  server { listen 127.0.0.1:8080; return 200 "v1\n"; }
}
EOF
nginx -p "$PWD" -c nginx.conf -e stderr
sed -i 's/return 200 "v1\\n";/return 200 "v2\\n"/' nginx.conf          # forgot the semicolon
echo "--- nginx -s reload:"
nginx -p "$PWD" -c nginx.conf -e stderr -s reload; echo "exit code: $?"
echo "--- kill -HUP (what a bare signal does):"
kill -HUP "$(cat nginx.pid)"; echo "exit code: $?"
sleep 0.5
echo "still serving: $(curl -s 127.0.0.1:8080/)"
grep -o 'emerg.*' error.log | sed 's/^emerg\] [0-9]*#[0-9]*: //'
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/null
--- nginx -s reload:
2026/10/28 03:40:44 [emerg] 14#14: unexpected "}" in /home/learner/nginx.conf:6
exit code: 1
--- kill -HUP (what a bare signal does):
exit code: 0
still serving: v1
unexpected "}" in /home/learner/nginx.conf:6

nginx -s reload starts a short-lived nginx process that parses the configuration before signalling the master, so it printed the error and exited with 1: a deploy script can catch that. (Ubuntu's systemctl reload nginx runs the same command.) A bare kill -HUP, as some process supervisors and older scripts send, "succeeded" with exit code 0: the master rejected the file, kept serving v1, and the only trace is a line in the error log. Either way, run nginx -t first and fail the deploy on its exit code, then verify that the change is live with a request, as the Field Notes wrapper does.

Upgrading the binary without downtime

A new Nginx binary (a security update) needs new processes, not just new workers. Nginx can hand its listening sockets to a new master:

bash
# Sample session: replacing the binary by hand, after the package has put the new one in place
$ sudo kill -USR2 $(cat /run/nginx.pid)         # start a new master with the new binary; old pid file -> nginx.pid.oldbin
$ ps -o pid,args -C nginx
    PID COMMAND
    812 nginx: master process /usr/sbin/nginx
    813 nginx: worker process
   2210 nginx: master process /usr/sbin/nginx   # the new master, child of the old one
   2211 nginx: worker process
$ sudo kill -WINCH $(cat /run/nginx.pid.oldbin)  # old master: stop its workers gracefully
$ curl -sI https://shop.example.com | head -1   # test the new binary
HTTP/2 200
$ sudo kill -QUIT $(cat /run/nginx.pid.oldbin)   # happy: retire the old master
# unhappy instead: kill -HUP the old master (restarts its workers) and -QUIT the new one

On Debian and Ubuntu, apt upgrade of the nginx package restarts the service by default, which briefly closes the listening sockets; for most sites that is a few milliseconds of refused connections. Where even that matters, run several proxies behind a load balancer and upgrade them one at a time (drain, upgrade, re-add), which is also the only way to upgrade the kernel or the machine.

How the others reload

Server Reload Notes
Nginx nginx -s reload / systemctl reload nginx (SIGHUP) new workers, old ones drain
HAProxy systemctl reload haproxy (master-worker mode, SIGUSR2) new process takes over the sockets (-x / expose-fd listeners); old one finishes its connections; hard-stop-after caps the wait. Server state can be carried over with server-state-file. Many changes need no reload at all: the Runtime API
Caddy caddy reload (POSTs the config to the admin API) config swapped in-process; a failed load leaves the old config running and caddy reload returns an error, unlike nginx -s reload
Traefik nothing: providers push changes the file provider watches the file; Docker/Kubernetes providers react to events. A broken dynamic file is logged and the last good configuration stays

Test yourself

During nginx -s reload, why are no connections refused?

  1. Nginx queues them in the kernel while it restarts
  2. The master keeps the listening sockets open; only workers are replaced, and old workers finish their requests
  3. Clients retry automatically
  4. Reloads are instant

A supervisor sends SIGHUP to the Nginx master after a config change; the change isn't live. What happened most likely?

  1. The reload is still running
  2. The new config was invalid; the master logged an error in the error log and kept the old configuration
  3. The workers crashed
  4. Nginx needs a restart for every change

What does worker_shutdown_timeout limit?

  1. How long a request may take
  2. How long old workers may keep serving open connections after a reload before they are closed
  3. The reload frequency
  4. The upstream timeout