NGINX+PHP-CGI (PHP-FPM) Web environment
A sudden discovery of system load rising, top view found many php-cgi process CPU utilization close to 100%
Looking for one of the CPU 100% php-cgi process PID, followed by Strace-p 10747, the result found the following results:
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {, 0}) = 1 (out [6], left {15, 0})
Poll ([{fd=6, Events=pollin}], 1, 0) = 0 (Timeout)
Is almost certainly the problem caused by file_get_contents (),
The reason is: File_get_contents's target Web site if the response is too slow, file_get_contents will always be stuck there will not timeout,
We know php.ini inside Max_execution_time can set the maximum execution time for a PHP script, but in php-cgi (PHP-FPM), the parameter does not work. The real ability to control the maximum execution time of a PHP script is the following parameters in the php-fpm.conf configuration file:
The timeout (in seconds) to serving a single request after the which the worker process would be terminated
Should be used as ' max_execution_time ' ini option does not stop script execution for some reason
' 0s ' means ' off '
<value name= "Request_terminate_timeout" >0s</value> default is 0 seconds, which means the PHP script will continue to execute. In this way, when all the php-cgi processes are stuck in the file_get_contents () function, the WebServer of this nginx+php is no longer able to process the new PHP request, and Nginx will return "502 bad Gateway" to the user. Modify this parameter to set a PHP script maximum execution time is necessary, but the symptom does not cure the root causes. For example, to 30s, if file_get_contents () to get the content of the Web page is slow, which means that 150 php-cgi process, only 5 requests per second, WebServer also difficult to avoid "502 bad Gateway."
To do a thorough solution, you might want to encapsulate the file_get_contents function:
The code is as follows |
Copy Code |
function _file_get_content ($STR) { $ctx = stream_context_create (Array ( ' http ' => array ( ' Timeout ' => 10//Set a timeout in seconds ) ) ); Return file_get_contents ($str, 0, $ctx); } |
So use _file_get_content instead of directly using file_get_contents problem solving.