How do you configure request timeout behavior differently at the Kestrel level versus the reverse proxy level, and why might they need to be coordinated?
Short answer
Both Kestrel (via Limits.RequestHeadersTimeout, KeepAliveTimeout, and the RequestTimeouts middleware for per-endpoint timeouts) and the reverse proxy (nginx's proxy_read_timeout, an ALB's idle timeout) can independently enforce timeout behavior -- if these are misaligned (for…
Advertisement
AModel answer
Both Kestrel (via Limits.RequestHeadersTimeout, KeepAliveTimeout, and the RequestTimeouts middleware for per-endpoint timeouts) and the reverse proxy (nginx's proxy_read_timeout, an ALB's idle timeout) can independently enforce timeout behavior -- if these are misaligned (for example, the proxy's timeout is shorter than a legitimately slow but valid backend operation), the proxy will terminate the connection prematurely with a generic error before Kestrel's own more informative timeout/error handling ever gets a chance to respond, so these values should be deliberately coordinated across the whole chain.
# nginx.conf
proxy_read_timeout 60s;
// Program.cs -- Kestrel's own timeout should be equal to or shorter than the proxy's
builder.Services.AddRequestTimeouts(options => {
options.DefaultPolicy = new RequestTimeoutPolicy { Timeout = TimeSpan.FromSeconds(55) };
});
app.UseRequestTimeouts();Real-world example
A slow report-generation endpoint legitimately taking 50 seconds was being terminated by nginx's default 60-second proxy_read_timeout intermittently under load, traced to nginx's timeout interacting poorly with connection queueing delays, resolved by both raising the proxy timeout and adding explicit endpoint-level timeout handling in the application itself.
Likely follow-ups
- What error does the client see when the reverse proxy times out versus when the application itself times out?
- How would you design a long-running operation to avoid depending on generous synchronous timeout values entirely?
Related topics