Understanding and Troubleshooting PIE Service Interface Timeout in PHP/FPM
This article explains the three main configuration parameters—fastcgi_read_timeout, max_execution_time, and request_terminate_timeout—that affect PIE service interface timeout, demonstrates how each influences HTTP response codes and PHP execution, and provides step‑by‑step debugging and fixes for Nginx, PHP, and PHP‑FPM settings.
When developing and maintaining a PIE service, developers often encounter ambiguous timeout behavior: requests may return 504 after 10 seconds, 500 after 30 seconds, or continue executing beyond the expected limit. The article identifies three configuration groups that determine the maximum execution time of an interface: Nginx fastcgi timeouts, PHP's max_execution_time , and PHP‑FPM's request_terminate_timeout .
1. Nginx configuration – The fastcgi_read_timeout (along with related fastcgi parameters) controls the timeout between Nginx and FastCGI. If the interface execution exceeds this value, Nginx returns a 504 Gateway Timeout, while the PHP script continues running. A simple test shows a 10‑second timeout producing a 504 response, confirming that Nginx timeouts do not stop PHP execution.
2. PHP configuration – The max_execution_time directive limits the CPU time consumed by PHP code. When exceeded, PHP emits a fatal error and the interface returns a 500 status. However, this limit does not count time spent in system calls such as sleep , file I/O, or network requests, so real‑world interfaces often run longer than the configured value.
3. PHP‑FPM configuration – The request_terminate_timeout setting terminates the entire worker process after the specified duration, causing a 502 response. Initial tests showed the timeout was ineffective because a custom logging library intercepted the SIGTERM signal, logged the event, but failed to exit the process. The library also relied on pcntl_signal without invoking pcntl_signal_dispatch() , preventing the callback from running.
After enabling asynchronous signal handling (available from PHP 7.1) and correcting the signal handler to exit the process, the timeout behaved as expected: the request ended after 10 seconds with a 502 response, and the PHP script stopped execution.
Summary Table
Parameter
Effect on Interface Return Time
Effect on PHP Execution Time
Timeout Response Code
Typical Online Default
fastcgi_read_timeout
Yes
No
504
10 s
max_execution_time
Yes (minor)
Controls pure PHP CPU time
500
30 s
request_terminate_timeout
Yes
Mainly controls PHP execution
502 or other
30 s
In conclusion, the interface return time is the minimum of the three timeout values (typically 10 seconds online), while PHP execution time is primarily limited by request_terminate_timeout after fixing the logging library issue. These findings apply only to FPM‑mode HTTP requests; CLI scripts are not affected by these settings.
Beijing SF i-TECH City Technology Team
Official tech channel of Beijing SF i-TECH City. A publishing platform for technology innovation, practical implementation, and frontier tech exploration.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.