A timeout rarely surfaces where it's caused. Nginx returns the 504, but Nginx didn't do anything wrong: it stopped waiting for PHP, and PHP was usually waiting on a query that was itself stuck behind a lock. Debugging a timeout means walking that chain backwards to the layer that actually stalled, and each layer signals in its own way that it was the one that gave up first.
The timeout layers
A Laravel HTTP request touches multiple layers. Each has its own timeout:
| Layer | Default | Config |
|---|---|---|
| Client (browser, curl) | Varies (curl has none) | Client-specific |
| Load balancer (ALB, CF) | 60s AWS ALB default | idle_ |
| Nginx upstream | 60s | fastcgi_ |
| PHP-FPM request | 0 (unlimited) | request_ |
| PHP script | 30s | max_ |
| Database statement | Unlimited (dangerous) | statement_ |
Diagnose the layer that failed
Nginx access log
# Enable timing fields in log_format
log_format upstream_timing '$remote_addr $request $status '
'request=$request_time upstream=$upstream_response_time';
# Look for 504s with timing
grep ' 504 ' /var/log/nginx/access.log | tail
If upstream_response_time is roughly equal to fastcgi_read_timeout, Nginx gave up waiting. That timer runs between two successive reads from PHP-FPM, not over the whole response, so a request that keeps sending output can run longer than it. If upstream_response_time is -, Nginx never contacted an upstream for that request, so the problem sits in front of PHP-FPM.
PHP-FPM slowlog
/etc/php/8.2/fpm/pool.d/www.conf
slowlog = /var/log/php-fpm/slow.log
request_slowlog_timeout = 10s
request_terminate_timeout = 60s
PHP-FPM dumps the stack trace of any request taking longer than request_slowlog_timeout. Useful for catching where the hang actually happens, often inside a database driver or file operation.
Database-level timeouts
app/Providers/AppServiceProvider.php (PostgreSQL)
use Illuminate\Database\Events\ConnectionEstablished;
use Illuminate\Support\Facades\Event;
public function boot(): void
{
// Per-session statement timeout on every new pgsql connection
Event::listen(function (ConnectionEstablished $event) {
if ($event->connection->getDriverName() === 'pgsql') {
$event->connection->statement("SET statement_timeout = '15s'");
}
});
}
Laravel's config/database.php has no statement_timeout key, so set it with a listener like this or on the role itself: ALTER ROLE app_user SET statement_timeout = '15s';. Without statement_timeout, a query can hold a PHP-FPM worker indefinitely. Set a value lower than your request timeout so DB hangs fail recoverably instead of taking out the worker.
Common 504 causes
- Synchronous external HTTP call with a long or missing timeout. Guzzle on its own has no timeout, and Laravel's Http client defaults to 30 seconds. Set
Http::timeout(5)on every external call. - Slow DB query on unindexed column.
EXPLAIN ANALYZEreveals full-table scans. - Exhausted PHP-FPM worker pool.
pm.max_childrentoo low, legitimate requests queue, queue exceeds timeout. - Lock contention on a hot row.
SELECT ... FOR UPDATEblocking other requests. Visible in Postgres'spg_locks. - Blocking cron job on the same server. A scheduled task hogging CPU or disk starves web workers.
Don't just raise every timeout
The instinct to bump fastcgi_read_timeout to 300s papers over the real issue. A request taking 300s shouldn't run in a request at all. Queue it. Have the request dispatch a job and return a 202 with a polling URL, or notify the client by webhook when the job finishes.