Notes
Why a proxy
Why put a proxy in front at all? nginx terminates TLS, so the app speaks plain HTTP on localhost. One port can serve many apps by host name or path. Its event loop absorbs slow clients so your app's workers are not tied up. It caches, compresses, rate limits, serves static files and writes consistent access logs. The application focuses on business logic.
- TLS termination
- one port for many apps (by host or path)
- slow clients handled by an event loop, not app threads
- caching, compression
- rate limits, IP rules
- static files, fast
- access logs in one place
The core config
The core config. An upstream block names the backend and keeps up to sixteen idle connections open to it. A server block listens on a port and matches a server name. A location block matches paths and proxy_pass sends requests upstream. Two easy to miss lines: proxy http version one point one and an empty Connection header, without which upstream keep alive does not work. And set Host and X-Forwarded-For, so the app sees the real host and client address.
upstream shoprung {
server 127.0.0.1:8080;
keepalive 16; }
server { listen 8443 ssl
http2; server_name
shoprung.test;
location / {
proxy_pass http://shoprung;
proxy_http_version 1.1;
proxy_set_header Connection
""; ...Host, X-Forwarded TLS and redirect
TLS termination uses the certificate and key from the last episodes, with only TLS one point two and 1.3 allowed. A small second server block catches plain HTTP and returns a permanent redirect. Our request to the HTTP port got 301 pointing at the same path on HTTPS. Keeping the path in the redirect, with the request URI variable, matters for both users and search engines.
server { listen 8081;
return 301 https://
shoprung.test:8443
$request_uri; }
$ curl :8081/api/health
301 → https://shoprung.test
:8443/api/health
ssl_protocols TLSv1.2 TLSv1.3 Caching
Caching. We cached the product list for thirty seconds and added a header showing the cache status. The first request was a MISS, went to ShopRung, and took one point eight milliseconds. The next two were HITs served from nginx's cache in about a quarter of a millisecond, without touching the app at all. Only cache responses that are the same for everyone: product lists yes, a user's cart never.
X-Cache: MISS 1.843 ms X-Cache: HIT 0.315 ms X-Cache: HIT 0.251 ms proxy_cache_path ... keys_ zone=shop:10m; proxy_cache shop; proxy_cache_valid 200 30s; never cache per-user pages
Compression
Compression. The product list was one thousand six hundred and sixty eight bytes. With a client that accepts gzip, nginx sent five hundred and sixty eight, about a third. nginx only compresses the types you list, so add application slash json for APIs, and skip tiny responses and images, which are already compressed.
without Accept-Encoding:
1,668 bytes
Accept-Encoding: gzip:
568 bytes
gzip on;
gzip_types application/json;
gzip_min_length 256;
skip images, already small Rate limiting
Rate limiting. We allowed five requests a second per client address, with a burst of five. Twenty requests fired as fast as possible: six succeeded, the first one plus the burst, and fourteen got four two nine, too many requests. nginx tracks clients in a small shared memory zone. Behind another load balancer, limit by the real client address, or every user shares one bucket.
limit_req_zone $binary_remote_addr zone=perip:1m rate=5r/s; limit_req zone=perip burst=5 nodelay; limit_req_status 429; 6 × 200 14 × 429 Too Many Requests
Change it safely
Change nginx safely. nginx minus t checks the configuration before you apply it; ours once failed with unknown directive http two, because the directive came in a newer version. A reload starts new workers with the new config while old workers finish their requests, so nothing drops. nginx minus T prints the full merged config. And the access log shows every request, while the error log explains every five oh two.
$ nginx -t syntax is ok / test is successful $ nginx -s reload (or systemctl reload nginx) old workers finish, new ones start: no downtime $ nginx -T full config access.log, error.log
Timeouts and limits
Some defaults bite. proxy read timeout is sixty seconds, so a slow report endpoint returns five oh four. client max body size is one megabyte, so uploads fail with four thirteen. Response buffering is on, which breaks server sent events and streaming until you turn it off for that location. And websockets need the Upgrade and Connection headers passed through. Set each deliberately per location.
- proxy_read_timeout 60s → 504 for slow endpoints
- proxy_connect_timeout
- client_max_body_size 1m → 413 on big uploads
- proxy_buffering (on): off for streaming/SSE
- websockets: Upgrade and Connection headers
Common mistakes
Mistakes. Reloading without nginx minus t. No keep alive to the upstream. Losing the client's address because X-Forwarded-For is not set. Caching responses that differ per user. A trailing slash in proxy_pass, which silently rewrites paths. Forgetting the one megabyte upload limit. And no rate limit on login endpoints.
- reload without nginx -t
- no upstream keep-alive
- missing X-Forwarded-For
- caching per-user responses
- proxy_pass trailing slash surprises (path rewrite)
- default 1 MB upload limit
- no rate limit on login
Say it like this
In the interview: a reverse proxy accepts client connections and forwards requests to backends, so it can terminate TLS, route by host and path, balance load, cache, compress, rate limit and log in one place. With nginx I keep upstream connections alive, pass the forwarded headers, test with nginx minus t, reload without downtime, and set timeouts and body limits per location.
- "It accepts client connections
- and forwards requests to backends: TLS termination, routing by host and path, load
- balancing, caching, gzip, rate
- limits and logging. With nginx I
- test with nginx -t, reload with no downtime, tune timeouts."