[文章作者:张宴 本文版本: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) | 阅读(194752)
小狼
2012-12-27 14:44
真的很厉害。看完几篇文章后 感觉到了高手是什么样的
Dlad Email
2013-3-25 10:30
学到一手,博主厉害
15026982229 上门按摩 Homepage
2013-5-7 19:09
海阔天空俏佳人时尚娱乐会所【15026982229】在全国主要城市均设有连锁店,就近提供北京,上海,广州,西安,重庆,杭州,温州,南京....本地兼职MM上门保健按摩。网址:www.lvcai.org
django Email Homepage
2013-7-8 14:03
Asking an oldtime gal to Coach Outlet Online  choose from is a lot like demanding any female for your day. A state name in the company is "The House of Gucci" as it Coach Outlet  is among the famous Italian fashion brands. Some wash rag wallet was shown within 1935, that may soon after get Coach Factory Outlet  to be the favorite Kelly felix Carrier. Cleansing the motor and it is shaft is additionally necessary as hair Coach Outlet Online  could geSt stuck to its shaft,
磨延城 Email Homepage
2013-10-21 18:01
磨途歌学习了
django Email Homepage
2013-11-30 18:59
Uggs Sale the ideal idea Coach Outlet Online  enough time to search for many of reduction Ugg boot. Like big portions? If you began ready your apartment, Coach Outlet  it's always best to consider the most effective way direct to the stage it could be to utilise your inventory throughout Coach Factory Outlet  concern, or maybe you basically won't destination stuff not enough.we're anti- whitening tray because Coach Outlet Online  i will be enjoy just get germ dog breeders
福利工口姬 Email Homepage
2014-4-16 16:48
可以说我不懂吗?
Steven Email Homepage
2015-3-19 02:21
Thank you for this post Weight Loss Tea
steev Email Homepage
2015-3-20 23:02
真的很厉害。看完几篇文章后 感觉到了高手是什么样的
steev Email Homepage
2015-3-20 23:02
真的很厉害。看完几篇文章后 感觉到了高手是什么样的 asphalt 8 hack
Aceslup
2015-6-11 15:39
怎么就肯定是 file_get_contents() 函数的问题呢?对开发不是很明白,望点拨。
assignment help Email Homepage
2019-10-24 19:55
gotoassignmenthelp.com.au is the leading assignment help provider worldwide. We have a group of online assignment help experts from top universities to help these students in the best possible manner. gotoassignmenthelp.com.au began its online assignment help Perth services with the desire to serve students struggling in almost any subject with the support of our professional assignment experts. Each of these academic writers possesses extensive knowledge and expertise.
leahmelda Email Homepage
2019-11-8 20:07
Online Biology Paper Writing Services produces different assignments, including Buy Essay Online Services, to help students deal with the Buy Business Reports challenges they face every day.  https://researchpapers247.com/biology-papers-3/
leahmelda Email Homepage
2019-11-8 20:08
Online Biology Paper Writing Services produces different assignments, including Buy Essay Online Services, to help students deal with the Buy Business Reports challenges they face every day.
Online Research Paper Writing Services Email Homepage
2019-12-13 19:10
Are you in need of Custom Research Paper Writing Services? Hires our Custom Research Paper Writers for all your Legitimate Custom Research Paper Writing Services.https://researchpapers247.com/research-paper-services/
American History Writing Services Email Homepage
2020-8-12 15:59
American history paper writing services are essential for american history assignment writing service students and American History Writing Services seekers. https://researchpapers247.com/american-history-writing-services/
Chapters mod apk Email Homepage
2022-2-22 20:25
But there is nothing in the Chapters mod apk that is not possible and that you cannot do because it is a simulation and interactive game mod in which everything is put under the control of a gamer who can be anything in the game at any time at any place or situation he wants to be in the game.
marito Email
2022-6-14 20:39
토토사이트 Homepage
2023-10-19 09:12
I must say, I was surprised. I rarely come across blogs that are both educational and engaging, and there is no doubt that you hit the nail on the head. The problem is that few people talk about it sensibly토토사이트
Lucky cola
2024-2-7 13:31
Prepare for non-stop gaming action like never before.
Lucky cola
分页: 10/11 第一页 上页 5 6 7 8 9 10 11 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]