Skip to content

Lesson 1 of 9 · 20m

How nginx Actually Handles Traffic

The master/worker event model, request routing order, and why location matching mistakes are responsible for so many production surprises.

nginx feels simple because a small config can proxy a small app in ten minutes. Production nginx is different: one wrong location, one missing header, or one misunderstood timeout can turn a healthy backend into an incident. The first thing to internalize is that nginx is not a threaded application server. It is an event-driven proxy with a small number of worker processes, each capable of handling thousands of mostly-idle connections.

The mental model

A master process reads configuration, owns privileged work such as binding low ports, and supervises worker processes. Workers do the real traffic handling. They use non-blocking I/O and an event loop, so one worker can juggle many connections without one thread per socket.

client ──keepalive──▶ nginx worker ──upstream connection──▶ application
                       │ epoll/kqueue event loop
                       └─ accepts, reads, writes, buffers, retries

This is why nginx is so good at TLS termination, buffering slow clients, and absorbing connection churn. It is also why a CPU-blocking operation in nginx itself is so damaging: block one worker and you block every connection assigned to that worker's event loop.

Location matching is a decision tree

nginx does not simply scan locations top to bottom. For most static and proxy routes, exact match wins first, then the longest prefix match, then regex matches in file order unless ^~ disables regex evaluation.

location = /healthz { proxy_pass http://127.0.0.1:8081; }
location ^~ /api/   { proxy_pass http://app_upstream; }
location ~* [.]php$  { return 404; }
location /          { proxy_pass http://web_upstream; }

A request for /api/users uses ^~ /api/. A request for /api/login.php still uses /api/ because ^~ tells nginx not to evaluate regexes for that prefix. Remove that marker and the regex may win. This distinction has caused real authentication bypasses and wrong-backend incidents.

Read the effective config, not the file you remember

Use nginx -T to dump the fully merged configuration after includes. It shows what nginx will actually load, not what one file in isolation appears to say.

nginx -t
nginx -T | sed -n '/server_name learn.sudosecurity.org/,/}/p'

nginx -t validates syntax and socket binding logic, but it does not prove upstreams are healthy or certificates are trusted. Treat it as a compile check, not a deploy verification.

Field notes

  • location order is not general execution order; exact, prefix, regex, and ^~ follow specific precedence.
  • One server block can shadow another when duplicate names or wildcard listeners are involved; inspect the merged output.
  • Keep worker count at auto unless you have measured a reason otherwise.
  • nginx workers shine at I/O concurrency, not CPU-heavy request processing; compressing huge responses has real CPU cost.

Was this lesson helpful?

Sign in to rate