How it works
Nginx uses a master process and multiple worker processes. The master reads configuration, manages workers, and coordinates reloads. Workers handle client traffic. The master does not handle each request in the data path.
Each worker runs an event loop with operating-system features such as epoll on Linux or kqueue on BSD systems. Nonblocking network I/O lets a worker manage many connections without a thread for each connection. CPU-heavy work and blocking operations can still stop progress. An event-driven design does not provide unlimited concurrency.
For HTTP traffic, Nginx selects a virtual server from the listen address and requested hostname. It then matches a location to decide how to handle the request. Configured modules check access, limit request rates, serve content, and proxy requests. For a proxied request, Nginx opens or reuses an upstream connection and sends the response to the client.
Workers have separate process memory. Selected state, such as rate-limit counters and cache metadata, can use shared-memory zones that the configuration explicitly defines. Cached response bodies usually use disk storage. During a graceful configuration reload, new workers start with the new configuration. Old workers finish their current work.
What it gives you in a design
- Reverse proxying and TLS termination
- Nginx can terminate client TLS and route requests by hostname or path. Configure backend TLS separately, including whether Nginx verifies upstream certificates.
- Upstream load balancing
- HTTP upstream groups support weighted round robin, least connections, IP hashing, and generic hashing, including consistent hashing. Choose a method based on request duration, backend capacity, and whether you need affinity.
- Response buffering and caching
- Response buffering can shorten the time a backend must serve a slow client. It uses memory and can also use temporary files. Proxy caching can prevent repeated backend reads. It requires explicit cache keys, freshness rules, and controls for private content.
- Traffic admission controls
- Request-rate and connection limits help reject or delay excess traffic before it reaches applications. Shared-memory zones coordinate these limits across workers on one instance. They do not automatically coordinate limits across a fleet.
- Static file delivery
- Nginx can serve files directly without an application server. It supports efficient file transfer and HTTP cache headers. A CDN usually provides a separate layer when geographic distribution and less work at the origin matter.
- Long-lived connections and stream proxying
- Nginx can proxy WebSocket connections when the configuration sets upgrade headers correctly. The optional stream module can proxy TCP and UDP traffic. HTTP path routing and HTTP caching do not apply to that layer.
Scaling, replication and failure
To scale within a host, adjust worker count, connection limits, operating-system file-descriptor limits, and resource budgets. Client sockets and upstream sockets both use capacity. Estimate active requests, idle keepalive connections, long-lived connections, TLS CPU cost, and buffer needs. Do not treat worker_connections as a throughput promise.
To scale across hosts, place multiple Nginx instances behind a load balancer or another layer that distributes traffic. Configuration, certificates, and upstream discovery must reach every instance. Nginx does not automatically replicate local caches and rate-limit zones. Cache hit rates and decisions to accept traffic change when you add or remove instances.
Open-source Nginx detects upstream failures passively from observed request failures. NGINX Plus also offers active health checks. Failover applies to eligible later requests or retry attempts. It does not transparently move an established connection. Limit the number or duration of retries. A lost response does not prove that a backend failed to perform a write.
Nginx does not provide a distributed consistency protocol for application state. Keep authoritative state outside the proxy. Decide explicitly whether cached responses can be stale. Graceful reloads reduce disruption, but host failures still close connections. Long-lived sessions need a way to reconnect.
Limits and defaults to know
Many of these values are configurable. Check the current documentation before you use an exact value.
| Worker connections | The documented worker_connections default is 512 per worker. This includes both upstream and client connections. File-descriptor limits also restrict effective capacity. |
|---|---|
| Request body size | client_max_body_size defaults to 1m. Nginx returns HTTP 413 for larger bodies unless you increase the limit. A value of 0 disables this size check. |
| Upstream connection timeout | proxy_connect_timeout defaults to 60 seconds. You can change it, but operating-system behavior can impose additional limits. |
| Upstream read timeout | proxy_read_timeout defaults to 60 seconds. It applies between successive read operations, not to the total response duration. |
| Rate-limit rejection status | limit_req_status defaults to HTTP 503. You can change it. HTTP 429 is often clearer for an API rate-limit response. |
Use it when
- You need an HTTP entry layer to terminate TLS and route requests to application services by hostname or path.
- You want simple load balancing across stateless backends without route logic in every client.
- You can cache public responses that clients request repeatedly near the application to reduce backend work.
- You need local controls to accept traffic, request-size limits, or response buffers to protect backend resources.
- You need to proxy WebSockets or serve static assets alongside application traffic.
Avoid it when
- You need globally exact quotas, inventory allocation, or another transactional decision. Local proxy state cannot provide that guarantee.
- You need durable message delivery, replay, or persistent work queues. Nginx is not a message broker.
- You need worldwide edge caching and protection from large-scale network-layer attacks. A single Nginx deployment does not replace a CDN or managed protection service.
- You need extensive dynamic service policy, identity-aware routing, and configuration management across a fleet. You do not want to build the control plane that supports them.
What interviewers ask about it
When Nginx is on your whiteboard, expect these follow-up questions. Each answer gives the core of a strong answer.
- Why can Nginx handle many connections with relatively few workers?
- Workers use event-driven, nonblocking network I/O, not one thread per connection. Capacity still depends on file descriptors, memory, CPU, upstream connections, and the work each request needs.
- What happens when an application server becomes unhealthy?
- Passive checks can temporarily exclude it after qualifying failures, as the upstream configuration permits. Retry only eligible requests within a fixed budget. Use application-level idempotency for writes because an uncertain response can hide a completed operation.
- Does a global rate limit stay the same when you add Nginx instances?
- No. A shared-memory rate-limit zone shares counters across workers in one instance, not across independent instances. A strict global limit needs coordinated state or a separate quota service. Local limits still help protect each proxy and its backends.
- How can you safely cache API responses?
- Cache only responses with a clear policy for shared use and freshness. Include every input that changes the representation in the cache key. Explicitly address authorization, cookies, tenant boundaries, and invalidation. A high hit rate does not justify private data leaks.
- How do WebSockets change the capacity plan?
- A proxied WebSocket usually keeps a client connection and an upstream connection open for the session. Plan capacity for file descriptors and connection memory. Configure idle timeouts and heartbeats. Explain how clients reconnect after a proxy or backend failure.
- Can Nginx reload configuration without traffic loss?
- A graceful reload starts new workers and lets old workers finish existing connections. Long-lived connections can keep old workers alive. An explicit shutdown timeout can limit the time old workers wait, but it closes remaining connections.
Common mistakes
- You multiply worker_processes by worker_connections and claim guaranteed client capacity. You ignore upstream sockets or operating-system limits.
- You assume every Nginx deployment includes active health checks, advanced gateway features, or automatic service discovery. You do not check the edition, modules, and configuration.
- You treat local caches and rate-limit counters as a replicated, globally consistent service.
- You enable broad retries or caching without considering duplicate writes, private responses, and stale data.
Alternatives
- HAProxy
- A strong choice when you mainly need dedicated TCP or HTTP load balancing with detailed traffic controls. Static file service and proxy caching matter less.
- Envoy
- Often a better choice when dynamic configuration, service-to-service traffic policy, and integration with a service-mesh control plane are central requirements.
- Managed load balancer or API gateway
- A better choice when less operational responsibility matters more than low-level proxy control. Check the service's limits for timeouts, connections, routing, authentication, and prices.
- CDN
- A better choice for geographically distributed delivery of cacheable content and less traffic to the origin. It often complements Nginx rather than replacing every origin-proxy function.
Where it is used
Nginx is a good fit for these interview questions. Each line tells you why.
- Design a URL shortener like BitlyNginx can route redirect traffic and cache eligible redirects when you explicitly define freshness, analytics, and link-update rules.
- Design a distributed rate limiterIts local request limits provide a clear example to explain why protection for each instance differs from a distributed quota.
- Design a chat app like WhatsAppWebSocket proxy support shows trade-offs for connection capacity, idle timeouts, backend routing, and how clients reconnect.
- Design a flash giveaway: 6 million burgers in an hourNginx can reject excess incoming traffic. The application still needs atomic claims and retry-safe allocation.
Common questions
What is Nginx architecture?
Nginx uses a master process to manage configuration and worker processes. Workers run event loops that handle many network connections with nonblocking I/O. Modules route requests, proxy requests, cache responses, control access, and provide other behavior.
Is Nginx a web server or a load balancer?
It can be both. Nginx can serve files directly, reverse proxy requests to applications, and distribute requests across upstream servers. State its role in your design instead of including it as a box without an explanation.
What is the difference between Nginx and an API gateway?
Nginx provides many gateway components, including routing, TLS termination, and traffic limits. A broader API gateway may also provide managed authentication integrations, API lifecycle tools, developer access management, and centralized policy distribution. The difference depends on the product and modules, not just the proxy engine.