Opscraft
Nginx & reverse proxies · Lesson 6 of 6 · 4 min read

Interview questions and model answers

"What is the difference between a forward proxy and a reverse proxy?"

A forward proxy sits in front of clients and makes requests on their behalf, for example a corporate proxy controlling outbound access. A reverse proxy sits in front of servers; clients think it is the site, and it routes each request to a backend, terminating TLS, balancing load, caching and enforcing limits along the way. Nginx in front of an app is a reverse proxy.

"Why does Nginx handle many connections with little memory?"

It uses a small number of worker processes, each running an event loop over non-blocking sockets with epoll, instead of a thread per connection. A worker handles whichever connections are ready and never waits on one, so thousands of idle or slow clients cost only a little memory each. Capacity is roughly workers times worker_connections, halved when proxying, and limited by file descriptors.

"Users get 502s after a deploy. How do you debug it?"

A 502 means Nginx couldn't get a valid response from the upstream. I'd check the error log for the exact message: connection refused means nothing is listening yet, so the app is still starting or the port is wrong, and readiness checks or a slower rollout fix it; "upstream prematurely closed" suggests the app's keep-alive timeout is shorter than Nginx's or the app is crashing; and if the upstream is a DNS name, Nginx may still be using the old IPs because it resolved them once at startup, which a resolver directive fixes.

"502 vs 504?"

502 Bad Gateway means the proxy got an invalid response or none at all, because the connection was refused or closed. 504 Gateway Timeout means the upstream accepted the connection but didn't respond within the proxy's timeout, in Nginx proxy_read_timeout. 502 points at availability, 504 at slowness.

"How does Nginx choose a location block?"

Exact matches with = win immediately. Otherwise it finds the longest matching prefix; if that prefix uses ^~, it stops there. If not, it checks regex locations in file order and uses the first match, falling back to the longest prefix if no regex matches. So file order matters only among regexes.

"Explain the proxy_pass trailing slash."

If proxy_pass has no URI, the original request path is passed unchanged. If it includes a URI, even just a slash, the part of the path that matched the location is replaced with that URI. So location /api/ with proxy_pass http://app/ sends /api/users as /users, while proxy_pass http://app sends it as /api/users.

"How would you rate-limit a login endpoint?"

Define a limit_req_zone keyed on the client IP, using the real client address via the realip module if there's a load balancer in front, at a low rate such as a few requests per minute, with a small burst and nodelay, and return 429. I'd add account-level limits in the app as well, because attackers rotate IPs, and put a WAF or bot protection at the edge for larger attacks.

"ingress-nginx is retired. What would you do?"

First inventory which controller we actually run, since F5's NGINX Ingress Controller is a different project that's still maintained. If it's community ingress-nginx, I'd plan a migration to a Gateway API implementation such as Envoy Gateway, or the AWS Load Balancer Controller on EKS: map every annotation to a Gateway API feature, run both controllers side by side, move host names gradually with DNS or weighted routing, watch error rates and latency, then remove the old controller. Until then, I'd restrict who can create Ingress objects and keep the controller off the internet where possible, since it no longer gets security fixes.