Skip to content

HTTP 504 Gateway Timeout Error: What It Means and How to Fix It

What the HTTP 504 Gateway Timeout error means and how to fix it: find slow requests, PHP-FPM and MySQL slow logs, and Nginx timeout settings.

Category
Web Servers
Covers
Apache, MySQL, Nginx
On this page

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 Timeout or 504 Gateway Time-out nginx
  • HTTP Error 504
  • Gateway 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

CodeWhat happened
502 Bad GatewayThe backend refused the connection, crashed or sent an invalid response. See our 502 guide.
503 Service UnavailableThe server is overloaded, in maintenance or rate-limiting. See our 503 guide.
504 Gateway TimeoutThe backend accepted the request but didn’t answer before the proxy’s timeout.

Common Causes of a 504 Error

  1. Slow database queries: missing indexes, locked tables or a large report.
  2. Long-running requests: imports, exports, backups, image processing or bulk emails run inside a web request.
  3. Slow or unreachable external APIs: a payment gateway, mail service or third-party API that doesn’t answer.
  4. An overloaded backend: all PHP-FPM workers or app threads are busy, so new requests wait in a queue.
  5. Timeouts that don’t match between the CDN, load balancer, Nginx, PHP-FPM and PHP.
  6. 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

  1. Reload the page after a minute. The server may have been briefly overloaded.
  2. 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.
  3. 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 -h and top. If PHP-FPM logs server 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.

LayerSettingDefault
Nginx → PHP-FPMfastcgi_read_timeout60s
Nginx → app (proxy_pass)proxy_read_timeout60s
Apache mod_proxyProxyTimeout (falls back to Timeout)60s
PHPmax_execution_time30s
PHP-FPM poolrequest_terminate_timeout0 (off)
AWS Application Load BalancerIdle timeout60s

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 nginx
sudo 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/health
sudo 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.

For the full list of codes and what they mean, see our HTTP status codes list. Guides for other common errors:

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.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top