Nginx + PHP-CGI (php-fpm) Web environment, suddenly found that the system load rise, top view found many php-cgi process CPU usage is close to 100%. find a CPU 100% ph... nginx + PHP-CGI (php-fpm) Web environment, suddenly found that the system load rise, top view found many php-cgi process CPU usage is close to 100%.
Find the PID of a php-cgi process with a CPU of 100% and trace it with strace-p 10747. the following results are displayed:
Select (7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0 })
Poll ([{fd = 6, events = POLLIN}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0 })
Poll ([{fd = 6, events = POLLIN}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0 })
Poll ([{fd = 6, events = POLLIN}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0 })
Poll ([{fd = 6, events = POLLIN}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0 })
Poll ([{fd = 6, events = POLLIN}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0 })
Poll ([{fd = 6, events = POLLIN}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0 })
Poll ([{fd = 6, events = POLLIN}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0 })
Poll ([{fd = 6, events = POLLIN}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0 })
Poll ([{fd = 6, events = POLLIN}], 1, 0) = 0 (Timeout)
Select (7, [6], [6], [], {15, 0}) = 1 (out [6], left {15, 0 })
Poll ([{fd = 6, events = POLLIN}], 1, 0) = 0 (Timeout)
It is almost certainly a problem caused by file_get_contents.
The reason is: if the response of the target website of file_get_contents is too slow, file_get_contents will remain stuck there and will not time out. we know php. max_execution_time in ini can be used to set the maximum execution time of PHP scripts. However, this parameter does not take effect in php-cgi (php-fpm, which of the following parameters in the php-fpm.conf configuration file can really control the maximum execution time of PHP scripts:
The timeout (in seconds) for serving a single request after which the worker process will be terminated
Shocould be used when 'max _ execution_time 'ini option does not stop script execution for some reason
'0s' means 'off'
0 s
The default value is 0 seconds. that is to say, the PHP script will be executed continuously. in this way, when all php-cgi processes are stuck in the file_get_contents () function, this Nginx + PHP WebServer can no longer process new PHP requests. Nginx will return "502 Bad Gateway" to the user and modify this parameter, it is necessary to set the maximum execution time of a PHP script. However, it is not a permanent cure. for example, if file_get_contents () is changed to 30 s, this means that 150 php-cgi processes can only process 5 requests per second. it is also difficult for WebServer to avoid "502 Bad Gateway ".
To completely solve the problem, re-encapsulate the file_get_contents function. the code is as follows:
Function _ file_get_content ($ str) {$ ctx = stream_context_create (array ('http' => array ('timeout' => 10 // set a timeout time, in seconds) // open source code phprm.com); return file_get_contents ($ str, 0, $ ctx );}
In this case, use _ file_get_content instead of file_get_contents to solve the problem.
Tutorial address:
Reprinted! But please include the article address ^