Choorai
Runtime/proxy timeout

Fixing 502/504 Gateway Timeouts

Repeated 502 and 504 usually mean proxy timeout settings and backend processing time are misaligned.

TL;DR

1) Measure failing endpoint latency 2) Compare proxy timeout vs app execution time 3) Move long jobs to async processing

심각

504 Gateway Timeout

원인

The upstream response did not return before the gateway timeout threshold.

해결책
  1. Measure average and peak latency on failing endpoints
  2. Align timeout values across proxy and runtime
  3. Optimize slow database/external API calls
  4. Move long-running work to queue/background workers

Diagnostic examples

terminal
curl -w "status=%{http_code} total=%{time_total}
" -o /dev/null -s https://api.example.com/reports

curl -i https://api.example.com/health
nginx.conf
location /api/ {
  proxy_connect_timeout 10s;
  proxy_send_timeout 60s;
  proxy_read_timeout 60s;
}

Important

Increasing timeout values alone can delay failure detection. Fix slow queries and blocking calls first.

Prerequisites

  • You can identify exact endpoints returning 502/504.
  • You can inspect proxy logs and application logs.
  • You can review timeout settings across proxy and app runtime.

Validation

  1. Affected requests return stable 2xx/4xx instead of 502/504.
  2. Timeout values are aligned across proxy and upstream services.
  3. Long-running tasks are moved to async jobs or background workers.

Troubleshooting

  • Check slow queries or blocking upstream handlers in logs/APM.
  • Review timeout configs such as proxy_read_timeout and service timeout.
  • Consider queue + callback/webhook patterns for long operations.

References

Related Articles

Last updated: February 22, 2026 · Version: v0.0.1

Send Feedback

Opens a new issue page with your message.

Open GitHub Issue