Nginx and API gateways · mid
Rate limits and reviewing an Nginx config
limit_req uses a leaky bucket per key. Choose the key carefully behind a load balancer, add a burst, return 429, and know how proxy_pass changes the path.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- limit_req_zone defines the key, the memory zone and the rate, for example $binary_remote_addr zone=otp_ip:10m rate=20r/m. limit_req in a location applies it.
- Behind a load balancer, $binary_remote_addr is the balancer address, so all users share one bucket. Use the real IP module (set_real_ip_from <balancer range>; real_ip_header X-Forwarded-For; real_ip_recursive on) and trust it only from the balancer range, or anyone can fake the header.
- Without burst, two requests in the same short moment exceed the rate. burst=20 nodelay lets a short burst through at once and then rejects. Without nodelay, extra requests are delayed to the rate.
- Rejected requests get 503 by default. Set limit_req_status 429, add a Retry-After header, and return a JSON body from a named location with error_page.
- limit_req_dry_run on logs what would be rejected, so you can measure before you enforce.
- proxy_pass with a URI part (http://app/) replaces the matched prefix, so /api/orders reaches the app as /orders. Without a URI part (http://app) the full path is passed.
- Rate limits per IP are weak for login and OTP abuse behind mobile carrier networks: many users share one IP. Add limits per phone number or account in the app.
The red pen
Common traps: mistake → fix
Mistake: limit_req counts requests per user
Fix: It counts per key. With the wrong key it counts per load balancer.
Mistake: proxy_pass http://app and http://app/ are the same
Fix: A trailing slash changes the path sent to the app.
Mistake: Rejected requests return 429 automatically
Fix: The default is 503.
Easy to hard
Questions interviewers ask, easy to hard
- How does limit_req work? What do rate, burst and nodelay do?
- Why would all users share one rate limit bucket?
- What path does the app see for /api/orders with proxy_pass http://app/?
- How would you rate limit an OTP endpoint at Nginx?
- Review this config and list every problem.
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.