Prevent nginx 504 Gateway timeout using PHP set_time_limit()

Prevent nginx 504 Gateway timeout using PHP set_time_limit()

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

Stefan Bogdanescu

Founder & Senior Architect

Prevent nginx 504 Gateway timeout using PHP set_time_limit()

When a PHP script runs longer than expected behind Nginx, you'll see 504 Gateway Time-out. This error isn't caused by your application logic failing; it's triggered by the web server terminating the request before the backend responds. Many developers assume that increasing the execution time limit inside the code will fix the issue, but set_time_limit(0) often feels like it does nothing when running PHP-FPM with Nginx.

This happens because set_time_limit() controls the script execution timeout managed by the PHP interpreter itself, not the Nginx connection timeout. If your script takes longer than the time allowed in the Nginx configuration, the server closes the connection before the PHP process even finishes, regardless of how you've set the internal limit.

The difference between PHP limits and Nginx limits

To resolve a 504 Gateway Time-out, you must adjust the timeout values in both layers:

  1. PHP-FPM: Controls how long the script can run before the PHP engine kills the process.
  2. Nginx: Controls how long the server waits for a response from PHP-FPM before closing the connection.

If either of these timeouts is too low, you'll get an error. The common mistake is adjusting only one side. For example, setting set_time_limit(0) in your PHP code allows the script to run indefinitely from the PHP engine's perspective, but if Nginx is configured to close idle connections after 60 seconds, the request will still fail with a 504 once that window expires.

Configuring Nginx timeouts

The primary fix involves editing the Nginx configuration file (usually in /etc/nginx/nginx.conf or a specific site config). You need to increase the proxy_read_timeout directive if you are using FastCGI, or ensure the worker process isn't timing out on long-running requests.

Add or modify the following settings within your server block:

location ~ \.php$ {
    fastcgi_pass   unix:/run/php/php-fpm.sock; # Adjust path as needed
    fastcgi_index  index.php;
    
    # Increase the time Nginx waits for PHP to respond
    fastcgi_read_timeout 600s; 
    
    # Optional: Increase buffer size if large payloads are involved
    fastcgi_buffer_size 128k;
    fastcgi_buffers     4 256k;
}

The fastcgi_read_timeout directive tells Nginx to wait up to 600 seconds (10 minutes) for the FastCGI server to send a complete response. Without this, the default might be as low as 60 seconds, causing the cut-off you're experiencing.

Remember that Nginx also has a proxy_connect_timeout and fastcgi_send_timeout. While these are less likely to cause issues for long-running scripts (since the script is already running), it's good practice to align them with your read_timeout to prevent half-finished responses from being dropped mid-stream.

Adjusting PHP-FPM settings

Even with Nginx configured correctly, the PHP-FPM pool itself can terminate a request if it exceeds its own limits. You should check the php.ini file or the specific pool configuration (e.g., /etc/php/8.x/fpm/pool.d/www.conf).

Look for these directives:

; Maximum execution time in seconds
max_execution_time = 600
; Maximum amount of memory each script is allowed to allocate (optional but relevant)
memory_limit = 256M

If max_execution_time is set lower than your Nginx timeout, PHP will send a 504 or 500 Internal Server Error response before the script completes. Setting it to -1 effectively disables the limit for that specific pool, matching the behavior of set_time_limit(0).

Why set_time_limit(0) might not feel like it works

You mentioned using set_time_limit(0) in your code. This function sets the maximum execution time to unlimited. However, if you are seeing a 504 Gateway Time-out, it means the connection was severed by Nginx before PHP could finish executing or even send headers.

The sequence of events is:

  1. User requests URL.
  2. Nginx starts request and waits for FastCGI response.
  3. Nginx hits fastcgi_read_timeout (e.g., default 60s).
  4. Nginx sends RST or closes connection.
  5. PHP script might still be running, but the client never receives the output.

In this scenario, changing set_time_limit(0) inside the script is insufficient because the network pipe has already been broken by the web server. The solution requires configuration changes in both Nginx and PHP-FPM to ensure they agree on a sufficient timeout window for your specific use case.

For projects requiring complex build steps or long-lived tasks within standard HTTP requests, consider restructuring the architecture to decouple heavy processing from the response cycle, or utilize job queues if available.

Our network