|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的好朋友们!偶然候,运转Nginx、PHP-CGI(php-fpm)Web办事的Linux办事器,俄然体系负载上升,利用top下令检察,良多php-cgi历程CPU利用率靠近100%。厥后,我经由过程跟踪发明,这类情形的呈现,跟PHP的file_get_contents()函数有着亲切的干系。
们晓得,在php.ini中,有一个参数max_execution_time能够设置PHP剧本的最年夜实行工夫,可是,在php-cgi(php-fpm)中,该参数不会起效。真正可以把持PHP剧本最年夜实行工夫的是php-fpm.conf设置文件中的以下参数:
[color=#333333!important]
1
[color=#317cc5!important]2
3
[color=#317cc5!important]4
[color=#004ed0!important]The[color=#004ed0!important]timeout[color=#006fe0!important][color=#333333!important]([color=#800080!important]in[color=#006fe0!important]seconds[color=#333333!important])[color=#006fe0!important][color=#800080!important]for[color=#006fe0!important]serving[color=#006fe0!important]a[color=#006fe0!important][color=#004ed0!important]single[color=#004ed0!important]request[color=#004ed0!important]after[color=#004ed0!important]which[color=#004ed0!important]the[color=#004ed0!important]worker[color=#004ed0!important]process[color=#004ed0!important]will[color=#004ed0!important]be[color=#004ed0!important]terminated
[color=#004ed0!important]Should[color=#004ed0!important]be[color=#004ed0!important]usedwhen[color=#006fe0!important][color=#008000!important]max_execution_time[color=#006fe0!important][color=#004ed0!important]ini[color=#004ed0!important]option[color=#004ed0!important]does[color=#800080!important]not[color=#006fe0!important][color=#004ed0!important]stop[color=#004ed0!important]script[color=#004ed0!important]execution[color=#800080!important]for[color=#006fe0!important][color=#004ed0!important]somereason[color=#006fe0!important]
[color=#008000!important]0s[color=#006fe0!important]means[color=#006fe0!important][color=#008000!important]off[color=#006fe0!important]
[color=#006fe0!important]<[color=#004ed0!important]value[color=#002d7a!important]name[color=#006fe0!important]=[color=#008000!important]"request_terminate_timeout"[color=#006fe0!important]>[color=#ce0000!important]0s[color=#006fe0!important]<[color=#006fe0!important]/value[color=#006fe0!important]>
默许值为0秒,也就是说,PHP剧本会一向实行下往。如许,当一切的php-cgi历程都卡在file_get_contents()函数时,这台Nginx+PHP的WebServer已没法再处置新的PHP哀求了,Nginx将给用户前往“502BadGateway”。修正该参数,设置一个PHP剧本最年夜实行工夫是需要的,可是,治本不治标。比方改成<valuename="request_terminate_timeout">30s</value>,假如产生file_get_contents()猎取网页内容较慢的情形,这就意味着150个php-cgi历程,每秒钟只能处置5个哀求,WebServer一样很难制止“502BadGateway”。
要做到完全办理,只能让PHP步伐员们改失落间接利用file_get_contents("http://example.com/")的习气,而是略微修正一下,加个超不时间,用以下体例来完成HTTPGET哀求。如果以为贫苦,能够自即将以下代码封装成一个函数。
[color=#333333!important]
1
[color=#317cc5!important]2
3
[color=#317cc5!important]4
5
[color=#317cc5!important]6
7
[color=#317cc5!important]8
9
[color=#ff0000!important]<?php[color=#006fe0!important]
[color=#002d7a!important]$ctx[color=#006fe0!important][color=#006fe0!important]=[color=#006fe0!important][color=#004ed0!important]stream_context_create[color=#333333!important]([color=#800080!important]array[color=#333333!important]([color=#006fe0!important]
[color=#006fe0!important][color=#008000!important]http[color=#006fe0!important][color=#006fe0!important]=[color=#006fe0!important]>[color=#006fe0!important][color=#800080!important]array[color=#333333!important]([color=#006fe0!important]
[color=#006fe0!important][color=#008000!important]timeout[color=#006fe0!important][color=#006fe0!important]=[color=#006fe0!important]>[color=#006fe0!important][color=#ce0000!important]1[color=#006fe0!important][color=#ff8000!important]//设置一个超不时间,单元为秒
[color=#006fe0!important][color=#333333!important])[color=#006fe0!important]
[color=#006fe0!important][color=#333333!important])[color=#006fe0!important]
[color=#333333!important])[color=#333333!important];[color=#006fe0!important]
[color=#004ed0!important]file_get_contents[color=#333333!important]([color=#008000!important]"http://example.com/"[color=#333333!important],[color=#006fe0!important][color=#ce0000!important]0[color=#333333!important],[color=#006fe0!important][color=#002d7a!important]$ctx[color=#333333!important])[color=#333333!important];[color=#006fe0!important]
[color=#ff0000!important]?>
固然,招致php-cgi历程CPU100%的缘故原由不但有这一种,那末,怎样断定是file_get_contents()函数招致的呢?
起首,利用top下令检察CPU利用率较高的php-cgi历程。
top-10:34:18up724days,21:01,3users,loadaverage:17.86,11.16,7.69
Tasks:561total,15running,546sleeping,0stopped,0zombie
Cpu(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
Mem:8100996ktotal,4320108kused,3780888kfree,772572kbuffers
Swap:8193108ktotal,50776kused,8142332kfree,412088kcached
PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND
10747www180360m22m12mR100.60.30:02.60php-cgi
10709www160359m28m17mR96.80.40:11.34php-cgi
10745www180360m24m14mR94.80.30:39.51php-cgi
10707www180360m25m14mS77.40.30:33.48php-cgi
10782www200360m26m15mR75.50.30:10.93php-cgi
10708www250360m22m12mR69.70.30:45.16php-cgi
10683www250362m28m15mR54.20.40:32.65php-cgi
10711www250360m25m15mR52.20.30:44.25php-cgi
10688www250359m25m15mR38.70.30:10.44php-cgi
10719www250360m26m16mR7.70.30:40.59php-cgi
找个中一个CPU100%的php-cgi历程的PID,用以下下令跟踪一下:
strace-p10747
假如屏幕显现:
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)
那末,就能够断定是file_get_contents()招致的成绩了。
欢迎大家来到仓酷云论坛! |
|