Sub topic 11
Tuning Proxy Timeouts and Buffering
Assume you know basic HTTP, TCP ports, and how to run an application on a local port. This chapter uses an application listening on 127.0.0.1:3000.
Understand the proxy timing model
Nginx communicates with two different peers:
- The client sends a request to Nginx.
- Nginx sends a request to the upstream application.
- The upstream application sends a response to Nginx.
- Nginx sends that response to the client.
Each stage can become slow. A timeout usually measures inactivity between operations, not the complete lifetime of a request.
| Directive | Measures | Typical problem |
|---|---|---|
| `proxy_connect_timeout` | Time to connect to the upstream | The application is down or unreachable |
| `proxy_send_timeout` | Time between writes to the upstream | The upstream reads the request slowly |
| `proxy_read_timeout` | Time between reads from the upstream | The application takes too long or stops responding |
| `send_timeout` | Time between writes to the client | The client reads very slowly |
A connection timeout usually produces a 502 Bad Gateway. A response-read timeout usually produces a 504 Gateway Timeout. Check your access and error logs to confirm which stage failed.
Configure practical timeout values
Start with values based on the application’s behavior rather than making every timeout very large.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_connect_timeout 3s;
proxy_send_timeout 30s;
proxy_read_timeout 60s;
send_timeout 30s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 16k;
proxy_busy_buffers_size 24k;
proxy_temp_file_write_size 64k;
}
}A short proxy_connect_timeout detects unavailable backends quickly. The proxy_read_timeout must be longer than the slowest legitimate period between response chunks. For example, an endpoint that performs a report query for 90 seconds needs a larger value or a better application design, such as sending progress updates.
Do not set proxy_read_timeout to several hours merely to hide slow or stuck requests. Long timeouts consume worker connections and can make an outage worse.
Understand response buffering
With proxy_buffering on, Nginx reads response data from the upstream into memory and sends it to the client separately. This lets a fast application finish its upstream connection while a slower client continues receiving data.
If the response is larger than the available memory buffers, Nginx can write temporary data to disk. This reduces memory pressure but increases disk I/O. Monitor the temporary-file location when serving large downloads.
Disable response buffering for data that must reach the client immediately, such as server-sent events or some streaming endpoints:
location /events/ {
proxy_pass http://127.0.0.1:3000;
proxy_buffering off;
proxy_read_timeout 1h;
}With buffering disabled, the upstream connection remains active while the client receives the stream. The application should send periodic heartbeat data, because proxy_read_timeout still applies when no response bytes arrive.
proxy_request_buffering controls the request body rather than the response. Leave it enabled for ordinary requests. Disable it only when an application needs to receive a large upload as it arrives and can safely handle a client disconnect.
Follow the data path
Validate and troubleshoot
After changing a site configuration, test it before reloading:
sudo nginx -t && sudo systemctl reload nginxUse the error log to distinguish connection failures, upstream timeouts, and client-side delays. Compare the application’s own logs with Nginx timestamps. If an endpoint regularly reaches proxy_read_timeout, measure its work and decide whether to increase the timeout, stream progress, or move the long task to a background job.
Exercise
An endpoint sends its first response byte quickly, then sends another byte every 45 seconds. Which setting must be at least this long, and why?
Show answer
Set proxy_read_timeout above 45 seconds because it measures the maximum idle period between reads from the upstream.