Nginx + Php-fpm fastcgi upstream timed out

Nginx + Php-fpm fastcgi upstream timed out

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Nginx + Php-fpm FastCGI Upstream Timed Out

When a PHP script exceeds the duration allocated for processing, Nginx often returns an HTTP 504 Gateway Time-out. In your specific scenario, a simple script that sleeps for 70 seconds is being terminated after exactly 60 seconds by Nginx. The error log confirms this: upstream timed out (110: Connection timed out) while reading response header from upstream.

This behavior is not necessarily a bug; it is the result of a specific timeout mechanism in the Nginx FastCGI module that interacts differently with your PHP-FPM pool configuration than you might expect.

The Hidden Timeout: fastcgi_read_timeout

You have correctly identified and configured several standard timeouts, including proxy_* directives (which are irrelevant for Unix sockets) and the fastcgi_* set to 600 seconds. You also adjusted the PHP configuration via phpinfo() and the FPM pool file (request_terminate_timeout = 600).

However, there is a specific directive in Nginx's FastCGI module that acts as a hard cap for reading data from the upstream application, independent of the time you spend writing to it. This is fastcgi_read_timeout.

While fastcgi_send_timeout controls how long Nginx waits to send parameters to PHP (like headers or body), and fastcgi_connect_timeout handles the socket connection handshake, fastcgi_read_timeout dictates how long Nginx will wait for a response from PHP after sending the request.

In many default installations or older configurations, this value defaults to 60 seconds. Even if you set fastcgi_read_timeout 600; in your configuration file, Nginx might be inheriting a different value or the change might not have taken effect due to how the directive is scoped within your server block.

To resolve this, ensure that fastcgi_read_timeout is explicitly set to a value higher than your script's execution time. It should be placed inside the location block handling PHP requests:

location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    
    # Standardize all FastCGI timeouts explicitly
    fastcgi_connect_timeout 600;
    fastcgi_send_timeout 600;
    fastcgi_read_timeout 600;
    
    include fastcgi_params;
}

It is crucial to note that fastcgi_read_timeout applies specifically to the time Nginx waits for the response from the upstream application. If your PHP script generates a large output (e.g., echoing a massive dataset), this timeout can trigger prematurely if the network or disk I/O cannot keep up with the transmission rate, even if the total time is under the limit.

The request_terminate_timeout Interaction

You have also configured request_terminate_timeout = 600 in your PHP-FPM pool configuration. This setting instructs FPM to kill a process if it runs longer than 600 seconds. However, this does not prevent Nginx from timing out first.

The architecture of the request flow is:

  1. Nginx sends the request to PHP-FPM.
  2. PHP-FPM executes the script.
  3. PHP-FPM sends the response back to Nginx.
  4. Nginx forwards the response to the client.

If your script sleeps for 70 seconds, it is still running when the initial 60-second window closes. If fastcgi_read_timeout (or a similar limit in your specific Nginx version) defaults to 60 seconds and you haven't successfully overridden it, Nginx will close the connection to PHP-FPM after 60 seconds of silence or data transfer delays. At that exact moment, PHP-FPM might still be running, waiting for further input or simply finishing its work, but it will never receive the request completion signal from Nginx because the pipe was broken by Nginx's timeout logic.

Verification Steps

Since you mentioned restarting both services, ensure your configuration changes are being applied to the correct worker processes. You can verify which timeouts are currently active using nginx -T or by inspecting the specific server block in question.

Look for any default_type or global settings that might be overriding your specific location block, though this is rare with timeout directives.

If you continue to see the upstream timed out error after explicitly setting fastcgi_read_timeout 600;, check if you are using a Load Balancer (like AWS ELB) in front of Nginx. While you noted the idle timeout is increased, ensure that the target group health checks and the listener timeouts on the load balancer itself do not interfere. However, since your error log explicitly states "upstream timed out," the issue originates within the Nginx-to-FPM communication, making the Load Balancer's idle timeout less likely to be the direct cause of the 504 error unless it is truncating the entire response stream before it reaches Nginx.

For high-performance applications running long tasks, consider splitting the logic so that the initial request returns immediately with a job ID, and the actual heavy processing happens asynchronously. This prevents blocking the web server process entirely, which is often more resilient than simply increasing timeouts indefinitely.

Our network