[文章作者:张宴 本文版本:v1.0 最后修改:2011.08.05 转载请注明原文链接:http://blog.zyan.cc/file_get_contents/]

  有时候,运行 Nginx、PHP-CGI(php-fpm) Web服务的 Linux 服务器,突然系统负载上升,使用 top 命令查看,很多 php-cgi 进程 CPU 使用率接近100%。后来,我通过跟踪发现,这类情况的出现,跟 PHP 的 file_get_contents() 函数有着密切的关系。

  大、中型网站中,基于 HTTP 协议的 API 接口调用,是家常便饭。PHP 程序员们喜欢使用简单便捷的 file_get_contents("http://example.com/") 函数,来获取一个 URL 的返回内容,但是,如果 http://example.com/ 这个网站响应缓慢,file_get_contents() 就会一直卡在那儿,不会超时。

  我们知道,在 php.ini 中,有一个参数 max_execution_time 可以设置 PHP 脚本的最大执行时间,但是,在 php-cgi(php-fpm) 中,该参数不会起效。真正能够控制 PHP 脚本最大执行时间的是 php-fpm.conf 配置文件中的以下参数:
  默认值为 0 秒,也就是说,PHP 脚本会一直执行下去。这样,当所有的 php-cgi 进程都卡在 file_get_contents() 函数时,这台 Nginx+PHP 的 WebServer 已经无法再处理新的 PHP 请求了,Nginx 将给用户返回“502 Bad Gateway”。修改该参数,设置一个 PHP 脚本最大执行时间是必要的,但是,治标不治本。例如改成 <value name="request_terminate_timeout">30s</value>,如果发生 file_get_contents() 获取网页内容较慢的情况,这就意味着 150 个 php-cgi 进程,每秒钟只能处理 5 个请求,WebServer 同样很难避免“502 Bad Gateway”。

  要做到彻底解决,只能让 PHP 程序员们改掉直接使用 file_get_contents("http://example.com/") 的习惯,而是稍微修改一下,加个超时时间,用以下方式来实现 HTTP GET 请求。要是觉得麻烦,可以自行将以下代码封装成一个函数。
  当然,导致 php-cgi 进程 CPU 100% 的原因不只有这一种,那么,怎么确定是 file_get_contents() 函数导致的呢?

  首先,使用 top 命令查看 CPU 使用率较高的 php-cgi 进程。

top - 10:34:18 up 724 days, 21:01,  3 users,  load average: 17.86, 11.16, 7.69
Tasks: 561 total,  15 running, 546 sleeping,   0 stopped,   0 zombie
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:   8100996k total,  4320108k used,  3780888k free,   772572k buffers
Swap:  8193108k total,    50776k used,  8142332k free,   412088k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                                              
10747 www       18   0  360m  22m  12m R 100.6 0.3    0:02.60 php-cgi                                                                                                              
10709 www       16   0  359m  28m  17m R 96.8  0.4    0:11.34 php-cgi                                                                                                              
10745 www       18   0  360m  24m  14m R 94.8  0.3    0:39.51 php-cgi                                                                                                              
10707 www       18   0  360m  25m  14m S 77.4  0.3    0:33.48 php-cgi                                                                                                              
10782 www       20   0  360m  26m  15m R 75.5  0.3    0:10.93 php-cgi                                                                                                              
10708 www       25   0  360m  22m  12m R 69.7  0.3    0:45.16 php-cgi                                                                                                              
10683 www       25   0  362m  28m  15m R 54.2  0.4    0:32.65 php-cgi                                                                                                              
10711 www       25   0  360m  25m  15m R 52.2  0.3    0:44.25 php-cgi                                                                                                              
10688 www       25   0  359m  25m  15m R 38.7  0.3    0:10.44 php-cgi                                                                                                              
10719 www       25   0  360m  26m  16m R  7.7  0.3    0:40.59 php-cgi

  找其中一个 CPU 100% 的 php-cgi 进程的 PID,用以下命令跟踪一下:
strace -p 10747

  如果屏幕显示:
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() 导致的问题了。




技术大类 » Web服务器 | 评论(203) | 引用(0) | 阅读(194072)
cheaperoakleysunglasses Email Homepage
2012-6-30 09:33
If you are not a movement, but would like a pair of sunglasses that will capture others attention, discount oakley sunglasses is still option is ideal for you. You like sports style was low-key and vintage look, if I decide to buy oakley sunglasses will give you every option in the Sun. Once you know that Oakley Active Sunglasses protect you from the Sun's glare, you can style and fashion--you decide, it is at the top of Oakley Asian Fit Sunglasses in this market.
醉逍遥 Email Homepage
2012-6-30 11:33
高手学习了,这么好的博客早就应该收藏起来了,哈哈。www.kanmw.com
Celine handbags Email Homepage
2012-7-2 16:09
Celine handbags,
Women handbag is state right into a lot of women who wear them.
Celine bag,
With regards to cheap Celine women's handbags, you will find endless solutions.
Celine luggage tote,
These women handbag varieties may be the dream of work in general and then any type of company meetings.
Oakley vault Email Homepage
2012-7-2 16:14
When the term sunglasses come up in any conversation, it is generally very quickly followed by the brand name of Oakley Vault. They have been one of the leading manufacturers of Oakley Sunglasses for years and they are by far the most popular brand that is sought after on Oakleyvault20l2.com today.
weew
2012-7-5 11:53
小虫
2012-7-11 21:01
您好,

在生产环境用了 nginx0.8 + php 5.26 + php-fpm 的方案,发现使用设置stream_context_create的方式并不能解决问题,php-fpm走的似乎是自己的配置,不大懂具体的原理

最后用curl库才解决了超时的问题~
wenlin Email
2012-7-25 14:51
file_get_contents 被冤枉了…,经过蛋疼地排查,真正的原因是 启用了 --with-curlwrappers 这个选项…当此选项被启用时,curl 模块会接管以下 PHP Stream Wrappers 支持的协议:curl 7.21.6 (x86_64-unknown-linux-gnu) libcurl/7.21.6 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp 其中,仅 file 被排除不接管…但这个模块的 stream wrapper 实现有 Bug,会在等待服务端返回数据包时一直 select, poll,不会阻塞…,详见“php-5.3.6/ext/curl/stream.c::173” 位置,php_curl_stream_read 函数中处理…包括 select 的 15s 超时,也只在此函数中存在…(其他 php 源代码中都没有)请平反 file_get_contents 吧~~~
virginiag Email Homepage
2012-7-26 10:00
Popular The North Face Jackets with the low price sale on online. If you want to buy the jackets that The North Face Outlet has discount for you. And we have other series for customers. http://www.northfaceoutletchicago.com/
龙岩无痛人流 Email Homepage
2012-8-5 09:20
嘿嘿 挺不错的   周末过来溜达下
lirourou77 Email Homepage
2012-9-5 13:01
in a new mechanism of system to replace the old in medicine have medical mechanism, which is the cheap designer handbags      
cattle nets", and click "online voting" found that, the original vote is paid, 1 yuan 1 ticket.

"If you have to vote online, by pay treasure, net silver or cell phone pays fee." HeXianSheng cheap designer clothes        
said, let people will choose the doctors like this is a good thing, but if will pay it is stale, "so, mean who's money, who can election?"

【 operation 】 can buy tickets into batch quantity
discount designer handbags        

According to HeXianSheng provide web site, reporter found "purple cattle nets", "popular activities" column in the article 1 is: ningxiang county "open the door the medical" advanced unit and "top ten doctors, top ten nurses, top ten technicians" selection activities to officiall
分组 Email Homepage
2012-9-6 21:48
应该是配置php吧。 nginx又是躺着也中枪
ted
2012-9-20 17:24
Process 17724 attached - interrupt to quit
poll([{fd=5, events=POLLIN|POLLERR|POLLHUP}], 1, 1000) = 1 ([{fd=5, revents=POLLIN}])
recvfrom(5, "NOT_STOREDrn", 32768, MSG_DONTWAIT, NULL, NULL) = 12
sendto(5, "add /tmp/prod-cache_menu_lock 0 "..., 40, MSG_DONTWAIT, NULL, 0) = 40
poll([{fd=5, events=POLLIN|POLLERR|POLLHUP}], 1, 1000) = 1 ([{fd=5, revents=POLLIN}])
recvfrom(5, "NOT_STOREDrn", 32768, MSG_DONTWAIT, NULL, NULL) = 12
sendto(5, "add /tmp/prod-cache_menu_lock 0 "..., 40, MSG_DONTWAIT, NULL, 0) = 40
poll([{fd=5, events=POLLIN|POLLERR|POLLHUP}], 1, 1000) = 1 ([{fd=5, revents=POLLIN}])
recvfrom(5, "NOT_STOREDrn", 32768, MSG_DONTWAIT, NULL, NULL) = 12
sendto(5, "add /tmp/prod-cache_menu_lock 0 "..., 40, MSG_DONTWAIT, NULL, 0) = 40
....
张兄, 我的这个又是啥情况? 有遇到过没?
当时系统整体负载很低:
top - 16:24:44 up 59 days,  1:03,  1 user,  load average: 0.39, 0.26, 0.18
Tasks: 366 total,   2 running, 361 sleeping,   3 stopped,   0 zombie
Cpu(s):  6.1%us,  4.2%sy,  0.0%ni, 87.5%id,  0.0%wa,  0.0%hi,  2.2%si,  0.0%st
Mem:   8193220k total,  3352868k used,  4840352k free,    50756k buffers
Swap:  4192956k total,      696k used,  4192260k free,  2720452k cached

只有一个php-cgi进程就出现:
PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND              
9976 nobody    20   0  398m  39m  13m R 59.4  0.5   0:03.70 php-cgi              
3954 root      20   0  187m  61m  488 S 39.5  0.8  66:19.16 memcached
...
Cheap Snapback Hats Email Homepage
2012-9-25 16:03
First off, what's CFD exactly? Cheap Snapback Hats You cannot discuss CFD propagate gambling unless you know very well what a CFD is correct? Snapback Hats Wholesale Well, CFD appears for Agreements for Distinction which is gdiuhop[=[ljoujo a kind of mixture item that gets traded.
我要学习网
2012-10-13 01:08
太厉害了!我要学习网
Coach Email Homepage
2012-11-3 14:30
"The Coach Outletengines that will likely power the J-31 we do know a bit mor.Coach Outlet  Those engines were actually revealed at the Zhuhai showCoach Outlet in 2008," Fisher said referring to an annual China air show. He beCoach Outletlieves the new J-31 engine is undergoing preliminary
Coach Email Homepage
2012-11-3 14:33
"The Coach Outletengines that will likely power the J-31 we do know a bit mor.Coach Outlet  Those engines were actually revealed at the Zhuhai showCoach Outlet in 2008," Fisher said referring to an annual China air show. He beCoach Outletlieves the new J-31 engine is undergoing preliminary
North Face UK Email Homepage
2012-11-13 22:26
<a href="http://www.buynorthfaceuk.co.uk/">North Face UK</a> store has selling North Face jackets with over 10 years of time. All our customers are all very great about our <a href="http://www.buynorthfaceuk.co.uk/">North Face Outlet</a> jacket. As the winter coming, <a href="http://www.buynorthfaceuk.co.uk/">Buy North Face</a> jackets here can enjoy lowest price.
Christian Louboutin Shoes Email Homepage
2012-11-13 22:26
Ladies who want to be sexy and charming need a brace of fashion <a href="http://www.onlinechristianlouboutinuk.co.uk/">Christian Louboutin Shoes</a>. For all women, put on <a href="http://www.onlinechristianlouboutinuk.co.uk/">Christian Louboutin Heels</a> will be very attractive and outstanding in any place. Shoes from <a href="http://www.onlinechristianlouboutinuk.co.uk/">Christian Louboutin UK</a> store are all very cheap and excellent. Take a look!
North Face Outlet Email Homepage
2012-11-13 22:26
<a href="http://www.us2012northfacejackets.com/">North Face Jackets</a> is becoming more and more important to everyone as the winter is around the corner. Buy north face winter jacket from <a href="http://www.us2012northfacejackets.com/">North Face Outlet</a> online store can save your much money. <a href="http://www.us2012northfacejackets.com/">Discount North Face</a> jacket from our store is your best choice for winter.
Christian Louboutin Outlet Email Homepage
2012-11-13 22:27
Thousand kinds of fashion red bottom shoes are on great discount in <a href="http://www.womenlouboutinshoes.com/">Christian Louboutin Outlet</a> store. No matter who can find the right style CL shoes she like from our store. You will totally fall in love with our <a href="http://www.womenlouboutinshoes.com/">Cheap Christian Louboutin</a> shoes.
分页: 9/11 第一页 上页 4 5 6 7 8 9 10 11 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]