Learn Labs
Foundations

Process Model & Reloads

Master and workers, event loops, and zero-downtime reloads measured under load

Master and workers

masterroot: reads config, binds portsworker 1event loopworker 2event loopworker 12one per core1000s of sockets1000s of sockets1000s of socketsworkers never block: each waits on all its sockets at once (epoll) and handles whichever is ready
  • 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 nginx user and do all the work. worker_processes auto starts 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 sendfile and aio exist.
worker_processes auto;one per coreworker_rlimit_nofile 65535;file descriptors per workerevents {worker_connections 4096;connections per worker, clients AND upstreams}

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.

Changing configuration safely

docker exec nginx nginx -tparse and check the whole config; nothing changesdocker exec nginx nginx -Tthe same, then print the effective config (all includes)docker exec nginx nginx -s reloadSIGHUP to the master: graceful reload

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 config

Is a reload really zero-downtime?

Five clients sent requests continuously for 8 seconds while Nginx was reloaded five times:

{200: 1365, 'RemoteProtocolError': 2}
HUPraceold workersservingfinish in-flight, close idlenew workersnew config, acceptinglisten socketnever closedkeepalive connidle on old workertime → (5 reloads, 1,367 requests, 2 errors)

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.

Other signals

CommandDoes
nginx -s reloadGraceful config reload (SIGHUP)
nginx -s quitGraceful shutdown: finish requests (SIGQUIT)
nginx -s stopImmediate shutdown (SIGTERM)
nginx -s reopenReopen 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

  1. 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.
  2. Make a typo in site.conf and run nginx -t. Then reload anyway, and confirm the site still works.
  3. Check docker inspect nginx:1.30.5 --format '{{.Config.StopSignal}}'.

On this page