{"id":4018,"date":"2013-12-24T00:37:33","date_gmt":"2013-12-23T16:37:33","guid":{"rendered":"https:\/\/www.icocean.com\/wp\/?p=4018"},"modified":"2013-12-24T00:37:33","modified_gmt":"2013-12-23T16:37:33","slug":"%e8%a7%a3%e5%86%b3php-cgi-%e8%bf%9b%e7%a8%8b-cpu-100","status":"publish","type":"post","link":"https:\/\/www.icocean.com\/blog\/?p=4018","title":{"rendered":"\u89e3\u51b3PHP-CGI \u8fdb\u7a0b CPU 100%"},"content":{"rendered":"<p>\u6709\u65f6\u5019\uff0c\u8fd0\u884c Nginx\u3001PHP-CGI(php-fpm) Web\u670d\u52a1\u7684 Linux \u670d\u52a1\u5668\uff0c\u7a81\u7136\u7cfb\u7edf\u8d1f\u8f7d\u4e0a\u5347\uff0c\u4f7f\u7528 top \u547d\u4ee4\u67e5\u770b\uff0c\u5f88\u591a php-cgi \u8fdb\u7a0b CPU \u4f7f\u7528\u7387\u63a5\u8fd1100%\u3002\u540e\u6765\uff0c\u6211\u901a\u8fc7\u8ddf\u8e2a\u53d1\u73b0\uff0c\u8fd9\u7c7b\u60c5\u51b5\u7684\u51fa\u73b0\uff0c\u8ddf PHP \u7684 file_get_contents() \u51fd\u6570\u6709\u7740\u5bc6\u5207\u7684\u5173\u7cfb\u3002<\/p>\n<p>\u4eec\u77e5\u9053\uff0c\u5728 php.ini \u4e2d\uff0c\u6709\u4e00\u4e2a\u53c2\u6570 max_execution_time \u53ef\u4ee5\u8bbe\u7f6e PHP \u811a\u672c\u7684\u6700\u5927\u6267\u884c\u65f6\u95f4\uff0c\u4f46\u662f\uff0c\u5728 php-cgi(php-fpm) \u4e2d\uff0c\u8be5\u53c2\u6570\u4e0d\u4f1a\u8d77\u6548\u3002\u771f\u6b63\u80fd\u591f\u63a7\u5236 PHP \u811a\u672c\u6700\u5927\u6267\u884c\u65f6\u95f4\u7684\u662f php-fpm.conf \u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u4ee5\u4e0b\u53c2\u6570\uff1a<!--more--><br \/>\nThe timeout (in seconds) for serving a single request after which the worker process will be terminated Should be used when &#8216;max_execution_time&#8217; ini option does not stop script execution for some reason &#8216;0s&#8217; means &#8216;off&#8217; <value name=\"request_terminate_timeout\">0s<\/value><br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<\/p>\n<p>The timeout (in seconds) for serving a single request after which the worker process will be terminated<br \/>\nShould be used when &#8216;max_execution_time&#8217; ini option does not stop script execution for some reason<br \/>\n&#8216;0s&#8217; means &#8216;off&#8217;<br \/>\n<value name=\"request_terminate_timeout\">0s<\/value><\/p>\n<p>\u9ed8\u8ba4\u503c\u4e3a 0 \u79d2\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0cPHP \u811a\u672c\u4f1a\u4e00\u76f4\u6267\u884c\u4e0b\u53bb\u3002\u8fd9\u6837\uff0c\u5f53\u6240\u6709\u7684 php-cgi \u8fdb\u7a0b\u90fd\u5361\u5728 file_get_contents() \u51fd\u6570\u65f6\uff0c\u8fd9\u53f0 Nginx+PHP \u7684 WebServer \u5df2\u7ecf\u65e0\u6cd5\u518d\u5904\u7406\u65b0\u7684 PHP \u8bf7\u6c42\u4e86\uff0cNginx \u5c06\u7ed9\u7528\u6237\u8fd4\u56de\u201c502 Bad Gateway\u201d\u3002\u4fee\u6539\u8be5\u53c2\u6570\uff0c\u8bbe\u7f6e\u4e00\u4e2a PHP \u811a\u672c\u6700\u5927\u6267\u884c\u65f6\u95f4\u662f\u5fc5\u8981\u7684\uff0c\u4f46\u662f\uff0c\u6cbb\u6807\u4e0d\u6cbb\u672c\u3002\u4f8b\u5982\u6539\u6210 <value name=\"request_terminate_timeout\">30s<\/value>\uff0c\u5982\u679c\u53d1\u751f file_get_contents() \u83b7\u53d6\u7f51\u9875\u5185\u5bb9\u8f83\u6162\u7684\u60c5\u51b5\uff0c\u8fd9\u5c31\u610f\u5473\u7740 150 \u4e2a php-cgi \u8fdb\u7a0b\uff0c\u6bcf\u79d2\u949f\u53ea\u80fd\u5904\u7406 5 \u4e2a\u8bf7\u6c42\uff0cWebServer \u540c\u6837\u5f88\u96be\u907f\u514d\u201c502 Bad Gateway\u201d\u3002<\/p>\n<p>\u8981\u505a\u5230\u5f7b\u5e95\u89e3\u51b3\uff0c\u53ea\u80fd\u8ba9 PHP \u7a0b\u5e8f\u5458\u4eec\u6539\u6389\u76f4\u63a5\u4f7f\u7528 file_get_contents(&#8220;http:\/\/example.com\/&#8221;) \u7684\u4e60\u60ef\uff0c\u800c\u662f\u7a0d\u5fae\u4fee\u6539\u4e00\u4e0b\uff0c\u52a0\u4e2a\u8d85\u65f6\u65f6\u95f4\uff0c\u7528\u4ee5\u4e0b\u65b9\u5f0f\u6765\u5b9e\u73b0 HTTP GET \u8bf7\u6c42\u3002\u8981\u662f\u89c9\u5f97\u9ebb\u70e6\uff0c\u53ef\u4ee5\u81ea\u884c\u5c06\u4ee5\u4e0b\u4ee3\u7801\u5c01\u88c5\u6210\u4e00\u4e2a\u51fd\u6570\u3002<br \/>\n<?php $ctx = stream_context_create(array( 'http' => array( &#8216;timeout&#8217; => 1 \/\/\u8bbe\u7f6e\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u79d2 ) ) ); file_get_contents(&#8220;http:\/\/example.com\/&#8221;, 0, $ctx); ?><br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<br \/>\n8<br \/>\n9<\/p>\n<p><?php  \n$ctx = stream_context_create(array(  \n   'http' => array(<br \/>\n       &#8216;timeout&#8217; => 1 \/\/\u8bbe\u7f6e\u4e00\u4e2a\u8d85\u65f6\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u79d2<br \/>\n       )<br \/>\n   )<br \/>\n);<br \/>\nfile_get_contents(&#8220;http:\/\/example.com\/&#8221;, 0, $ctx);<br \/>\n?><\/p>\n<p>\u5f53\u7136\uff0c\u5bfc\u81f4 php-cgi \u8fdb\u7a0b CPU 100% \u7684\u539f\u56e0\u4e0d\u53ea\u6709\u8fd9\u4e00\u79cd\uff0c\u90a3\u4e48\uff0c\u600e\u4e48\u786e\u5b9a\u662f file_get_contents() \u51fd\u6570\u5bfc\u81f4\u7684\u5462\uff1f<\/p>\n<p>\u9996\u5148\uff0c\u4f7f\u7528 top \u547d\u4ee4\u67e5\u770b CPU \u4f7f\u7528\u7387\u8f83\u9ad8\u7684 php-cgi \u8fdb\u7a0b\u3002<\/p>\n<p>top &#8211; 10:34:18 up 724 days, 21:01,  3 users,  load average: 17.86, 11.16, 7.69<br \/>\nTasks: 561 total,  15 running, 546 sleeping,   0 stopped,   0 zombie<br \/>\nCpu(s):  5.9%us,  4.2%sy,  0.0%ni, 89.4%id,  0.2%wa,  0.0%hi,  0.2%si,  0.0%st<br \/>\nMem:   8100996k total,  4320108k used,  3780888k free,   772572k buffers<br \/>\nSwap:  8193108k total,    50776k used,  8142332k free,   412088k cached<\/p>\n<p>PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND<br \/>\n10747 www       18   0  360m  22m  12m R 100.6 0.3    0:02.60 php-cgi<br \/>\n10709 www       16   0  359m  28m  17m R 96.8  0.4    0:11.34 php-cgi<br \/>\n10745 www       18   0  360m  24m  14m R 94.8  0.3    0:39.51 php-cgi<br \/>\n10707 www       18   0  360m  25m  14m S 77.4  0.3    0:33.48 php-cgi<br \/>\n10782 www       20   0  360m  26m  15m R 75.5  0.3    0:10.93 php-cgi<br \/>\n10708 www       25   0  360m  22m  12m R 69.7  0.3    0:45.16 php-cgi<br \/>\n10683 www       25   0  362m  28m  15m R 54.2  0.4    0:32.65 php-cgi<br \/>\n10711 www       25   0  360m  25m  15m R 52.2  0.3    0:44.25 php-cgi<br \/>\n10688 www       25   0  359m  25m  15m R 38.7  0.3    0:10.44 php-cgi<br \/>\n10719 www       25   0  360m  26m  16m R  7.7  0.3    0:40.59 php-cgi<\/p>\n<p>\u627e\u5176\u4e2d\u4e00\u4e2a CPU 100% \u7684 php-cgi \u8fdb\u7a0b\u7684 PID\uff0c\u7528\u4ee5\u4e0b\u547d\u4ee4\u8ddf\u8e2a\u4e00\u4e0b\uff1a<\/p>\n<p>strace -p 10747<\/p>\n<p>\u5982\u679c\u5c4f\u5e55\u663e\u793a\uff1a<\/p>\n<p>select(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})<br \/>\npoll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)<br \/>\nselect(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})<br \/>\npoll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)<br \/>\nselect(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})<br \/>\npoll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)<br \/>\nselect(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})<br \/>\npoll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)<br \/>\nselect(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})<br \/>\npoll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)<br \/>\nselect(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})<br \/>\npoll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)<br \/>\nselect(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})<br \/>\npoll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)<br \/>\nselect(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})<br \/>\npoll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)<br \/>\nselect(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})<br \/>\npoll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)<br \/>\nselect(7, [6], [6], [], {15, 0})        = 1 (out [6], left {15, 0})<br \/>\npoll([{fd=6, events=POLLIN}], 1, 0)     = 0 (Timeout)<\/p>\n<p>\u90a3\u4e48\uff0c\u5c31\u53ef\u4ee5\u786e\u5b9a\u662f file_get_contents() \u5bfc\u81f4\u7684\u95ee\u9898\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u5019\uff0c\u8fd0\u884c Nginx\u3001PHP-CGI(php-fpm) Web\u670d\u52a1\u7684 Linux \u670d\u52a1\u5668\uff0c\u7a81\u7136\u7cfb\u7edf\u8d1f\u8f7d\u4e0a\u5347 <a href='https:\/\/www.icocean.com\/blog\/?p=4018' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"class_list":["post-4018","post","type-post","status-publish","format-standard","hentry","category-lamp","category-24-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4018"}],"version-history":[{"count":1,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4018\/revisions"}],"predecessor-version":[{"id":4019,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4018\/revisions\/4019"}],"wp:attachment":[{"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.icocean.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}