Opscraft
Nginx & reverse proxies · Lesson 1 of 6 · 5 min read

How Nginx works

Almost every web request you will ever debug passes through a reverse proxy, and the most common one is Nginx. Interviewers use it to test whether you understand HTTP in practice: how a request is matched to a server and location, how it is passed to backends, what the timeouts and buffers do, and how to read a 502 or 504 from the error log. This topic covers the process model, the configuration language, proxying and load balancing, TLS, rate limiting and caching, Nginx inside Kubernetes (including the retirement of the community ingress-nginx controller), and the alternatives you should be able to compare.

What a reverse proxy does

A forward proxy acts for clients (an office proxy that browsers use to reach the internet). A reverse proxy acts for servers: clients connect to it as if it were the website, and it passes each request to one of the real backends behind it. That single front door lets you:

Enlarge diagramclienthttps://shop…NginxTLS · routing · limits ·cacheapp 110.0.1.11:8080app 210.0.1.12:8080app 310.0.1.13:8080HTTPSBackends speak plain HTTP on the private network and never face the internet.
Figure 1. A reverse proxy is the single front door: it terminates TLS, picks a backend, and adds headers so the backend knows who really asked.
Swipe sideways to see all of it, or tap to open it full screen.
  • Terminate TLS in one place, so backends can speak plain HTTP on a private network.
  • Load balance across several backend instances and take unhealthy ones out of rotation.
  • Route by host name or path: /api to one service, / to static files.
  • Protect backends with rate limits, request size limits, timeouts and IP allow lists.
  • Cache and compress responses so backends do less work.

Nginx does all of this, and it can also serve static files directly. HAProxy, Envoy, Traefik and Caddy cover the same ground with different strengths (see the last lesson before the interview questions).

Master and worker processes

Nginx runs one master process (as root, so it can bind ports 80 and 443 and read certificates) and several worker processes (as an unprivileged user such as nginx or www-data). The master reads the configuration and manages workers; the workers handle all the traffic.

Enlarge diagrammaster (root)reads config, binds:443worker 1event loop (epoll)worker 2event loop (epoll)worker 3event loop (epoll)worker 4event loop (epoll)Capacity ≈ workers × worker_connections, halved when proxying (client + upstream).
Figure 2. One master process manages several workers. Each worker runs an event loop over thousands of connections instead of one thread per connection.
Swipe sideways to see all of it, or tap to open it full screen.

Each worker runs an event loop: instead of one thread per connection, a single worker watches thousands of sockets with epoll and handles whichever is ready. That is why Nginx serves many slow clients with little memory, where a thread-per-connection server would run out of threads.

worker_processes auto;          # one worker per CPU core
events {
    worker_connections 4096;    # per worker; total ≈ workers × connections
}
worker_rlimit_nofile 65535;     # each connection needs file descriptors

A proxied request uses two connections (client side and upstream side), so the real client capacity is roughly half of worker_processes × worker_connections. The error worker_connections are not enough means you hit that limit; too many open files means the file-descriptor limit is lower than the connections you asked for.

The configuration file

Configuration lives in /etc/nginx/nginx.conf, usually with include /etc/nginx/conf.d/*.conf; for site files. It is made of directives inside nested contexts:

Enlarge diagramMAIN: USER, WORKER_PROCESSES, ERROR_LOGHTTP: LOG_FORMAT, GZIP, UPSTREAMSERVER: LISTEN, SERVER_NAME, SSLeventsworker_connectionslocation /api/proxy_passlocation /try_files
Figure 3. Configuration is nested contexts. Settings flow down and can be overridden lower down.
Swipe sideways to see all of it, or tap to open it full screen.
Context Holds Example directives
main Process settings user, worker_processes, error_log
events Connection handling worker_connections
http Settings for all HTTP traffic log_format, gzip, upstream, limit_req_zone
server One virtual host listen, server_name, ssl_certificate
location One URL pattern inside a server proxy_pass, root, try_files

Most directives are inherited downwards and can be overridden. The big exception: array-like directives such as proxy_set_header and add_header are inherited only if the inner context defines none of its own. Add one add_header inside a location and every header set at server level disappears there, which is a classic cause of missing security headers.

Change configuration safely:

sudo nginx -t                 # test syntax and referenced files
sudo nginx -T | less          # print the full merged configuration
sudo systemctl reload nginx   # graceful: new workers start, old ones finish their requests

A reload (nginx -s reload, signal HUP) never drops connections; a restart does. If nginx -t fails, the reload is refused and the old configuration keeps running.