Large downloads through nginx are slow to start, and the error log warns `an upstream response is buffered to a temporary file`.
IntermediateTroubleshootingLoad balancers and reverse proxies
Answer
Likely causes → With proxy_buffering on (default), nginx reads the upstream response into memory buffers and then spills to disk (proxy_temp_path) when the response exceeds them. Large files cause disk I/O and can fill the disk; slow disks delay delivery.
Diagnose →
grep 'buffered to a temporary file' /var/log/nginx/error.log | wc -l
du -sh /var/lib/nginx/proxy 2>/dev/null || du -sh /var/cache/nginx/proxy_temp
df -h /var
Fix → For large downloads, stream instead of buffering to disk:
location /downloads/ {
proxy_max_temp_file_size 0; # never spill to disk
proxy_pass http://files;
}
Better: serve big static objects from S3/CloudFront or let the app return X-Accel-Redirect so nginx serves the file directly.
Prevent → Monitor the temp path's disk usage and keep large-object delivery off the app path.
More on Load balancers and reverse proxies
nginx returns `502 Bad Gateway` and the error log says `connect() failed (111: Connection refused) while connecting to upstream`.Beginnernginx returns `504 Gateway Time-out` with `upstream timed out (110: Connection timed out) while reading response header from upstream`.BeginnerAn ALB target group shows all targets `unhealthy` with `Health checks failed with these codes: [404]`.BeginnerAn ALB returns sporadic `502`s, and access logs show `elb_status_code=502` with `target_status_code=-`. Load is low.Intermediatenginx logs `upstream prematurely closed connection while reading response header from upstream`.Intermediatenginx error log shows `no live upstreams while connecting to upstream` and every request gets 502.Intermediate