File_get_contents () in PHP causes Nginx to appear 504

Source: Internet
Author: User
Tags fpm php script stop script

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.