File_get_contents () in php causes nginx to display 504

Source: Internet
Author: User
Tags stop script
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 ^

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.