When a browser shows "504 Gateway Timeout", many people assume the site is completely down. The error is actually more specific: the front server (usually Nginx) passed the request to the application behind it and didn't get a reply in time. The app may well be running, just too slowly. That distinction matters, because fixing a 504 usually means fixing code or the database rather than the server.
504 versus 502
A 502 Bad Gateway appears when the backend returns an invalid response or refuses the connection, for example when PHP-FPM or a Node.js process has crashed. A 504 means the connection was made and the request sent, but the wait ran out before the answer arrived. Put simply, 502 is "nobody answered" and 504 is "the answer took too long".
Typical causes
- Slow SQL queries on unindexed tables, huge joins or locked tables.
- Waiting on external services such as a payment gateway, SMS provider or delivery API that doesn't respond.
- Heavy work inside the request: big imports, image processing, PDF reports or bulk email.
- Busy PHP-FPM workers, often during bot traffic or sudden spikes, so new requests queue until they time out.
- Exhausted resources: low memory pushing the system into swap, or an overloaded disk.
Finding the cause in logs
Start with the Nginx error log, usually /var/log/nginx/error.log on a VPS:
upstream timed out (110: Connection timed out) while reading response header from upstream,
client: 203.0.113.25, server: example.uz, request: "POST /admin/import HTTP/1.1",
upstream: "fastcgi://unix:/run/php/php8.2-fpm.sock"
It tells you which URL is slow and which backend failed to respond. One page failing points to that page's code; the whole site failing points to a shared resource such as the database or PHP-FPM. Next, look at running queries and enable the slow query log:
mysql -u root -p -e "SHOW FULL PROCESSLIST;"
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 2;
Where timeouts live
| Layer | Setting | Purpose |
|---|---|---|
| Nginx (proxy) | proxy_read_timeout | Waiting for an HTTP backend |
| Nginx (PHP) | fastcgi_read_timeout | Waiting for PHP-FPM |
| PHP | max_execution_time | Script execution limit |
| PHP-FPM | request_terminate_timeout | Pool-level hard stop |
The shortest one fires first. Both Nginx values default to 60 seconds. Raise them only for the specific endpoint that genuinely needs it:
location = /admin/import.php {
fastcgi_read_timeout 300s;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}
Raising timeouts site-wide is risky: slow requests hold workers longer and the site collapses faster under load.
Example: a school in Andijan exporting grades
Teachers at a private school in Andijan export every class's grades to one Excel file at the end of each term. As enrolment grew, the export started returning 504. The log showed /teacher/export timing out, and the slow query log revealed an unindexed search on the grades table. Adding an index on student_id and quarter brought it down to seconds, and later the export moved to a background job that shows a download link when ready.
The right order of fixes
- Find the slow spot in the logs.
- Speed up the query: indexes, less data, caching.
- Give external API calls short timeouts, e.g.
CURLOPT_TIMEOUTin cURL. - Move heavy jobs to a queue or cron.
- Only then raise timeouts, and only where needed.
On shared hosting
Server settings are managed by the host, but you can still check a lot yourself. The ISPmanager panel, opened from the sayt.uz cabinet, shows site error logs and lets you change the PHP version and some parameters. On WordPress, a 504 is often caused by one heavy plugin, a backup extension or a misconfigured cron task, so try disabling recent plugins. If you can't find the cause, open a support ticket with the time and URL of the error.