Backend Development 8 min read

Nginx Interview Questions: Concurrency Model, Threadless Design, Optimization Techniques, and 502 Error Causes

This article explains how Nginx achieves high concurrency with an asynchronous, non‑blocking, epoll‑based architecture, why it avoids multithreading, lists common optimization settings, and enumerates typical reasons for 502 errors, providing practical commands and configuration examples.

Top Architect
Top Architect
Top Architect
Nginx Interview Questions: Concurrency Model, Threadless Design, Optimization Techniques, and 502 Error Causes

Interview Questions Brought by a Reader

Nginx is how it implements concurrency? Why does Nginx not use multithreading? What are common optimization methods? What are possible causes of 502 errors?

Interviewer’s Psychological Analysis

The interviewer wants to see whether the candidate truly understands Nginx’s core principles; superficial knowledge is common, but deep understanding is rare and essential for effective optimization.

Question Breakdown

Nginx How Does It Achieve High Concurrency?

It uses asynchronous, non‑blocking I/O with epoll and extensive low‑level code optimizations.

Unlike a model where each request spawns a process, Nginx runs a master process and multiple worker processes.

The master process collects and distributes incoming requests to workers.

The master also monitors worker status to ensure reliability.

Workers are usually set equal to the number of CPU cores; each worker can handle many requests limited mainly by memory.

The asynchronous non‑blocking model lets idle time be used efficiently, so a few processes can serve massive concurrent traffic.

When a request arrives, a worker handles it only up to the point where it would block (e.g., waiting for an upstream response). The worker registers an event and goes idle; when the upstream replies, the event triggers and the worker resumes processing.

Why Nginx Does Not Use Multithreading

Apache creates multiple processes or threads, each consuming CPU and memory; high concurrency can exhaust resources.

Nginx uses a single‑threaded, asynchronous, non‑blocking model (epoll) with configurable worker processes, avoiding per‑request CPU/memory allocation and reducing context switches, thus supporting higher concurrency.

Common Nginx Optimization Configurations

1) Adjust worker_processes

Set the number of workers to match CPU cores. Example to get CPU core count:

$ grep processor /proc/cpuinfo | wc -l

2) Maximize worker_connections

The total concurrent clients = worker_processes * worker_connections . Set worker_connections to the maximum allowed per core (e.g., 1024).

3) Enable Gzip Compression

Compress responses to reduce bandwidth and improve load speed. Example configuration (inside the http block):

4) Enable Caching for Static Files

Cache static assets to reduce bandwidth and improve performance:

location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 365d;
}

5) Tune Timeouts and Keep‑Alive

Adjust keepalive and timeout settings to reduce CPU/network overhead. (See image for typical values.)

6) Disable Access Logs

Access logs consume CPU; disabling them improves performance:

access_log off;

If logs are needed, enable buffering:

access_log /var/log/nginx/access.log main buffer=16k;

Possible Causes of 502 Errors

FastCGI process not started.

Insufficient FastCGI worker processes.

FastCGI execution timeout.

FastCGI buffer too small.

Proxy buffer too small (adjust proxy_buffer_size and proxy_buffers ).

PHP script execution time too long (adjust request_terminate_timeout in php‑fpm).

Example buffer adjustments:

fastcgi_buffer_size 32k;
fastcgi_buffers 8 32k;
proxy_buffer_size 16k;
proxy_buffers 4 16k;

These settings help prevent 502 errors caused by upstream resource limits.

backendoptimizationoperationsConcurrencynginx502 error
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.