PHP function problems

Sometimes, running nginx and PHP CGI (PHP FPM) Linux server web services, a sudden increase in system load, view with top command, CPU utilization, many phpcgi process approaches 100 percent by tracking later found that the situation with PHP's file_get_contents () function are closely related.

 

 

In medium to large sites, based on the HTTP protocol API call PHP programmers prefer to use the more common recreational deer simple and convenient file-get-contents ( "http://example.com/ ") function to get the URL of the returned content, however, If http://example.com/ site responds slowly, the file -get-contents () will always stay there, and do not exceed the time limit.

 

 

We know that Max has a parameter that can specify the time, it can set the maximum execution time of PHP scripts. However, in the PHP CGI (PHP FPM), this parameter has no effect PHP FPM.CONF configuration file the following parameters can really control the maximum execution time of PHP scripts:

 

View sketch?

 

Service a single request timeout (in seconds), after which the worker process will be terminated

 

Should be used when the "max_execution_time" ini options script execution for some reason did not stop

 

"0s" represent "off"

 

<value name=“request\u terminate\u timeout”>0s</value>

 

The default value is 0 seconds, which means that PHP script will continue to execute this, when all the PHP CGI process are trapped in file-get-contents () function, nginx + PHP web server will no longer deal with the new PHP request , nginx returned to the user "502 bad gateway" it is necessary to change this parameter to set the maximum execution time of PHP scripts, but there is no need to cure these symptoms. For example, change <value name = "request-terminate-timeout> 30s </ value> If the file-get-contents () Gets web content is very slow, which means that 150 PHP CGI process can only 5 per second requests, but it is difficult to avoid the webserver "502 bad gateway."

 

 

To solve this problem, PHP programmers can only get rid of the file directly using the "get" content ( "http://example.com/") habit, just slightly modify, add a timeout, and then implement the following manner httpget If problems are encountered requests, the following code can be packaged into the function.

Guess you like

Origin www.cnblogs.com/blogst/p/11804362.html