An HTTP 504 Gateway Timeout error means a server acting as a gateway or proxy didn’t get a response from the server behind it in time. The backend is usually running, but it’s too slow: a heavy database query, a long report, a stuck external API call or an overloaded server. The proxy waited as long as its timeout allowed, then gave up.
This guide explains what the 504 status code means, the most common causes, and how to fix it on Nginx, Apache, PHP-FPM, load balancers and CDNs, including which timeout settings to check.
What Does 504 Gateway Timeout Mean?
The HTTP standard (RFC 9110) says a 504 means the server, “while acting as a gateway or proxy, did not receive a timely response from an upstream server it needed to access in order to complete the request.”
A request often passes through several servers, and each one has its own timeout:
Browser → CDN / load balancer → Nginx (reverse proxy) → PHP-FPM / app → database / external API
The 504 comes from the first layer whose timeout ran out. The slow part is somewhere after it.
You may see it as:
504 Gateway Timeoutor504 Gateway Time-out nginxHTTP Error 504Gateway Timeout: The gateway did not receive a timely response from the upstream server or application.(Apache)504 Gateway Time-out ... The server didn't respond in time.
504 vs 502 and 503
| Code | What happened |
|---|---|
| 502 Bad Gateway | The backend refused the connection, crashed or sent an invalid response. See our 502 guide. |
| 503 Service Unavailable | The server is overloaded, in maintenance or rate-limiting. See our 503 guide. |
| 504 Gateway Timeout | The backend accepted the request but didn’t answer before the proxy’s timeout. |
Common Causes of a 504 Error
- Slow database queries: missing indexes, locked tables or a large report.
- Long-running requests: imports, exports, backups, image processing or bulk emails run inside a web request.
- Slow or unreachable external APIs: a payment gateway, mail service or third-party API that doesn’t answer.
- An overloaded backend: all PHP-FPM workers or app threads are busy, so new requests wait in a queue.
- Timeouts that don’t match between the CDN, load balancer, Nginx, PHP-FPM and PHP.
- Network or DNS problems between the proxy and the backend, for example after a backend IP changed.
Quick Fixes If You’re Visiting the Site
- Reload the page after a minute. The server may have been briefly overloaded.
- Don’t resubmit forms or payments straight away. The action may have finished on the server even though the page timed out. Check your email or account first.
- Try again later, or contact the site owner with the URL and the time of the error.
How to Fix a 504 Error on Your Own Server
Step 1: Confirm the timeout in the logs
Nginx logs every upstream timeout:
sudo grep "timed out" /var/log/nginx/error.log | tail -n 10
upstream timed out (110: Connection timed out) while reading response header from upstream, client: 203.0.113.10, server: example.com, request: "GET /report HTTP/1.1", upstream: "fastcgi://unix:/run/php/php8.3-fpm.sock:"
The request field shows which URL is slow. “While reading response header” means the backend accepted the request but took too long. “While connecting to upstream” means it couldn’t even open a connection in time, which points to a network problem or a backend that’s completely busy.
Measure how long the URL takes with curl:
curl -s -o /dev/null -w "status=%{http_code} time=%{time_total}s\n" https://example.com/report
Step 2: Find out why the backend is slow
Increasing timeouts only hides the problem. First find what the request is waiting for:
- PHP-FPM slow log: it records a stack trace for any request that runs longer than a threshold. Add this to your pool file (for example
/etc/php/8.3/fpm/pool.d/www.conf) and restart PHP-FPM:request_slowlog_timeout = 10s slowlog = /var/log/php8.3-fpm.slow.log - MySQL slow query log: turn it on to catch queries that take more than a couple of seconds:
sudo mysql -e "SET GLOBAL slow_query_log = 1; SET GLOBAL long_query_time = 2;"Then check the active queries with
SHOW FULL PROCESSLIST;. - Server load: run
uptime,free -handtop. If PHP-FPM logsserver reached pm.max_children setting, requests are queueing for a free worker. See how to fix pm.max_children. - External calls: give HTTP calls to other APIs their own short timeout in your code, so one slow service can’t hold the whole request.
Step 3: Move long jobs out of the web request
Imports, exports, reports, video processing and bulk email shouldn’t run while the browser waits. Put them in a background queue (such as Laravel queues, Celery or a cron job), return a response straight away, and let the user download the result or get an email when it’s ready. This fixes the 504 for good, instead of making the timeout longer.
Step 4: Adjust the timeouts (if the request really needs more time)
Some admin tasks legitimately take a few minutes. In that case, raise the timeouts for that path only, and make sure every layer allows at least as much time as the one in front of it.
| Layer | Setting | Default |
|---|---|---|
| Nginx → PHP-FPM | fastcgi_read_timeout | 60s |
| Nginx → app (proxy_pass) | proxy_read_timeout | 60s |
| Apache mod_proxy | ProxyTimeout (falls back to Timeout) | 60s |
| PHP | max_execution_time | 30s |
| PHP-FPM pool | request_terminate_timeout | 0 (off) |
| AWS Application Load Balancer | Idle timeout | 60s |
Example: allow five minutes for an export URL in Nginx:
location /admin/export {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_read_timeout 300s;
}
Then raise max_execution_time for that script (with set_time_limit(300) in the code, or in php.ini), test and reload:
sudo nginx -t && sudo systemctl reload nginxsudo systemctl restart php8.3-fpm
Step 5: Check the CDN and load balancer
If you use a CDN or load balancer, it has its own timeout, and that’s often the shortest one. Cloudflare, for example, waits 100 seconds for the origin by default and shows its own 524 error (not 504) when the origin is too slow. Raise the timeout on the load balancer, or bypass the CDN for long admin tasks.
Step 6: Check connectivity to the backend
If the log says upstream timed out ... while connecting to upstream, test the connection from the proxy server:
curl -sv --max-time 10 http://10.0.0.5:8080/healthsudo ss -ltnp | grep 8080
Check firewalls between the servers, and that the backend’s address in the config is still correct. Nginx resolves hostnames in proxy_pass once, when it starts, so restart or reload Nginx after a backend’s IP changes.
Related HTTP Error Guides
For the full list of codes and what they mean, see our HTTP status codes list. Guides for other common errors:
- HTTP 400 Bad Request
- HTTP 401 Unauthorized
- HTTP 403 Forbidden
- HTTP 404 Not Found
- HTTP 429 Too Many Requests
- HTTP 500 Internal Server Error
- HTTP 502 Bad Gateway
- HTTP 503 Service Unavailable
Frequently Asked Questions
What does 504 Gateway Timeout mean?
It means a proxy or gateway server waited too long for a response from the server behind it and gave up. The backend is usually running, but too slow.
Is a 504 error my fault?
No. It’s a server-side problem. Reload after a minute, and don’t resubmit forms or payments until you’ve checked whether the first attempt went through.
How do I fix 504 Gateway Timeout in Nginx?
Find the slow URL in the Nginx error log, then find why it’s slow (slow queries, PHP-FPM slow log, server load). If the request really needs more time, raise fastcgi_read_timeout or proxy_read_timeout for that location.
What is the difference between 502 and 504?
502 means the backend failed: it was down, refused the connection or crashed. 504 means the backend was there but didn’t answer in time.
Should I just increase the timeout?
Only for requests that legitimately take long, and only for those paths. For normal pages, fix the slow query or code, or move the work to a background job. Long timeouts tie up workers and can make overload worse.
Conclusion
An HTTP 504 Gateway Timeout error means the backend took longer than the proxy was willing to wait. Find the slow URL in the proxy’s error log, find out what it’s waiting for with the PHP-FPM and MySQL slow logs, and move long jobs to the background. Raise timeouts only for the requests that really need it, and keep every layer’s timeout in line with the one in front of it.
How did it make you feel?
