Fix a 504 Gateway Timeout error
A 504 Gateway Timeout means nginx reached your app, but the app didn’t answer in time. Unlike a 502 (app not responding at all), a 504 is about slowness.
What’s taking so long?
1. A slow database query. The most common cause. A missing index or a query scanning a huge table can take seconds. Find slow queries, add indexes, and avoid N+1 query patterns.
2. A slow external call. If your request calls a third-party API that’s slow or down, your response waits on it. Add timeouts to outbound calls so one slow dependency doesn’t hang the whole request.
3. Genuinely heavy work in the request. Generating a big report, processing an upload, sending lots of email? Don’t do it inside the web request — move it to a background job or a cron task and return quickly.
4. The timeout is just too short for a legitimate long task. Some endpoints (large imports) genuinely need longer. The proxy timeout can be raised for a specific route — but treat that as a last resort, after you’ve made the work faster or moved it to the background.
How to diagnose
- Reproduce it — is it one specific page/endpoint, or everything?
- Check the app log with timing — where is the time going?
- Watch resources while it runs:
topfor CPU, and check the database. → Disk & memory - Isolate: temporarily stub the slow query/API call — does the 504 vanish?
The right fixes
- Add database indexes; cache expensive results with Redis.
- Set timeouts on outbound HTTP calls.
- Move slow work to background jobs.
- Only then consider a longer proxy timeout.
Running OrbixPanel? Resource graphs help you spot the slow path fast. OrbixPanel.com → — the free, self-hosted, AI-powered hosting control panel.