Process Model & Reloads
Master and workers, event loops, and zero-downtime reloads measured under load
Master and workers
- The master process runs as root. It reads and validates the config, opens the listening sockets (ports below 1024 need root) and the log files, and starts the workers. It never handles a request.
- Workers run as the unprivileged
nginxuser and do all the work.worker_processes autostarts one per CPU core; this lab's container sees 12 cores, so 12 workers. - Each worker is single-threaded and runs an event loop: it registers
all its sockets with the kernel and handles whichever are ready. Nothing
in a worker may block. Reading a file that's not in the page cache can,
which is why
sendfileandaioexist.
Capacity is roughly workers × worker_connections, halved when
proxying, since every client connection has a matching upstream
connection. 12 × 4096 ÷ 2 is about 24,000 concurrent proxied clients here,
if the file descriptor limit allows it.
Workers don't share memory by default. Anything that must be consistent
across workers (rate-limit counters, cache keys, upstream health and
connection counts) needs a shared memory zone: limit_req_zone,
keys_zone=, and zone in an upstream block. Leaving out the upstream
zone made least_conn blind to busy backends in this lab (see
Balancing Methods).
Changing configuration safely
A reload: the master re-reads the config. If it's valid, the master starts new workers with it and tells the old workers to stop accepting connections, finish their in-flight requests, and exit. The listening sockets never close, so no connection is refused.
If the new config is invalid, the master logs an error and keeps the old workers running. Tested by appending a server block pointing at a host that doesn't exist:
docker exec nginx nginx -s reload
nginx: [emerg] host not found in upstream "does-not-exist" in /etc/nginx/conf.d/site.conf:160
curl -k -o /dev/null -w '%{http_code}' https://localhost:8443/api/whoami
200 # still serving with the old configIs a reload really zero-downtime?
Five clients sent requests continuously for 8 seconds while Nginx was reloaded five times:
{200: 1365, 'RemoteProtocolError': 2}Two of 1,367 requests failed. Old workers close their idle keepalive connections when they shut down. A client that sends its next request on one of those connections at the same moment sees the connection close instead of a response. No request that reached a worker was dropped; the failures are the race on idle connections.
Every HTTP client talking to anything that reloads, restarts or
scales needs to retry idempotent requests on a closed connection
(RemoteProtocolError, ECONNRESET, "server disconnected"). Most
browsers do this automatically. Many HTTP libraries don't, or do it only
for some methods. That's the remaining 0.15%.
Other signals
| Command | Does |
|---|---|
nginx -s reload | Graceful config reload (SIGHUP) |
nginx -s quit | Graceful shutdown: finish requests (SIGQUIT) |
nginx -s stop | Immediate shutdown (SIGTERM) |
nginx -s reopen | Reopen log files after rotation (SIGUSR1) |
kill -USR2 <master> | Start a new master with a new binary: upgrade without downtime |
Docker's docker stop sends SIGTERM by default, an immediate stop. The
official image sets STOPSIGNAL SIGQUIT, so containers stop gracefully.
Lab
- Run a loop of requests and reload ten times. How many fail? Change the client to retry once on a closed connection and run again.
- Make a typo in
site.confand runnginx -t. Then reload anyway, and confirm the site still works. - Check
docker inspect nginx:1.30.5 --format '{{.Config.StopSignal}}'.