What are the common Nginx timeouts and why do they matter?
Understand common Nginx timeouts: client_header_timeout, proxy_read_timeout, keepalive_timeout and more, why they matter, and how tuning prevents 504 errors.
Expected Interview Answer
Nginx exposes several timeout directives that limit how long it waits at each phase of a connection — the main ones are client_header_timeout, client_body_timeout, send_timeout, keepalive_timeout, and the proxy timeouts (proxy_connect_timeout, proxy_send_timeout, proxy_read_timeout). They matter because they protect the server from slow or stuck connections and control how gracefully it handles slow clients and slow backends.
Client-facing timeouts (client_header_timeout, client_body_timeout, send_timeout) bound how long Nginx tolerates a slow client sending headers, a body, or receiving a response, defending against slow-loris style attacks and freeing worker connections. keepalive_timeout controls how long an idle persistent connection stays open. The proxy_* timeouts govern the link to upstream backends: proxy_connect_timeout for establishing the connection, proxy_send_timeout and proxy_read_timeout for sending to and reading from the backend. Setting them too low causes premature 504 Gateway Timeout errors; too high lets stuck connections exhaust worker slots.
- Protects against slow-client and slow-loris attacks
- Frees worker connections held by stuck requests
- Prevents premature 504 errors on slow backends
- Bounds resource usage under load
- Makes failure behavior predictable and tunable
AI Mentor Explanation
Timeouts are like the umpire's limits on how long a bowler can delay before bowling or a batter can take guard. If a player stalls too long, play cannot proceed and the umpire intervenes. Each phase — run-up, delivery, response — has its own reasonable window, just as Nginx bounds header, body, and response phases so one slow participant cannot freeze the whole match.
Step-by-Step Explanation
Step 1
Bound header reading
Set client_header_timeout so Nginx drops clients that send request headers too slowly.
Step 2
Bound body reading
Use client_body_timeout to limit how long Nginx waits between reads of the request body.
Step 3
Bound the response
Set send_timeout to cap how long Nginx waits for the client to accept response data.
Step 4
Tune keepalive
Adjust keepalive_timeout to control how long idle persistent connections are kept open.
Step 5
Tune proxy timeouts
Set proxy_connect_timeout, proxy_send_timeout, and proxy_read_timeout to match realistic backend latency and avoid premature 504s.
What Interviewer Expects
- Naming the main client and proxy timeout directives
- Explaining what phase each timeout governs
- Linking low timeouts to 504 Gateway Timeout errors
- Awareness of slow-loris protection
- Reasoning about worker connection exhaustion
Common Mistakes
- Confusing client timeouts with proxy timeouts
- Setting proxy_read_timeout too low and causing 504 errors on slow endpoints
- Assuming a single timeout directive covers all phases
- Setting timeouts extremely high and exhausting worker connections
- Ignoring keepalive_timeout impact on connection reuse
Best Answer (HR Friendly)
“Nginx timeouts are time limits for how long it waits at each step of handling a request. They matter because they stop slow or stuck connections from tying up the server, while making sure real but slow requests still get a fair chance before Nginx gives up.”
Code Example
http {
# Client-facing timeouts
client_header_timeout 10s;
client_body_timeout 10s;
send_timeout 10s;
keepalive_timeout 65s;
server {
location /api/ {
proxy_pass http://backend;
# Backend-facing timeouts
proxy_connect_timeout 5s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
}
}
}Follow-up Questions
- Which timeout causes a 504 Gateway Timeout and why?
- How do Nginx timeouts help mitigate slow-loris attacks?
- What is the difference between proxy_read_timeout and proxy_send_timeout?
- How does keepalive_timeout affect connection reuse and performance?
- How would you tune timeouts for a long-polling or streaming endpoint?
MCQ Practice
1. Which timeout most commonly causes a 504 Gateway Timeout?
proxy_read_timeout bounds waiting for the backend's response; exceeding it returns a 504 Gateway Timeout.
2. Which timeout primarily helps defend against slow-loris attacks?
client_header_timeout drops clients that trickle request headers slowly, the core slow-loris technique.
3. What does keepalive_timeout control?
keepalive_timeout sets how long an idle keep-alive connection is kept open for reuse.
Flash Cards
What does proxy_read_timeout do? — Bounds how long Nginx waits for the backend to send response data; exceeding it yields a 504.
What does client_header_timeout protect against? — Slow clients that trickle request headers, such as slow-loris attacks, by dropping them after the window.
What does keepalive_timeout govern? — How long an idle persistent (keep-alive) connection is held open for reuse before closing.
Why avoid overly high timeouts? — Stuck connections hold worker slots longer, risking worker connection exhaustion under load.