php教程 php手册 php中file_get_contents()导致nginx出现504

php中file_get_contents()导致nginx出现504

May 24, 2016 pm 01:00 PM

Nginx+PHP-CGI(php-fpm) 的Web环境,突然发现系统负载上升,top 查看后发现很多 php-cgi 进程 CPU 使用率接近100%.

找其中一个 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()导致的问题.

原因是:file_get_contents的目标网站如果反应过慢,file_get_contents就会一直卡在那里不会超时,我们知道php.ini 里面max_execution_time 可以设置 PHP 脚本的最大执行时间,但是,在 php-cgi(php-fpm) 中,该参数不会起效,真正能够控制 PHP 脚本最大执行时间的是 php-fpm.conf 配置文件中的以下参数:

The timeout (in seconds) for serving a single request after which the worker process will be terminated   

Should be used when 'max_execution_time' ini option does not stop script execution for some reason   

'0s' means 'off'   

0s 

默认值为 0 秒,也就是说,PHP 脚本会一直执行下去,这样,当所有的 php-cgi 进程都卡在 file_get_contents() 函数时,这台 Nginx+PHP 的 WebServer 已经无法再处理新的 PHP 请求了,Nginx 将给用户返回"502 Bad Gateway",修改该参数,设置一个 PHP 脚本最大执行时间是必要的,但是,治标不治本,例如改成 30s,如果发生 file_get_contents() 获取网页内容较慢的情况,这就意味着 150 个 php-cgi 进程,每秒钟只能处理 5 个请求,WebServer 同样很难避免"502 Bad Gateway".

要做到彻底解决,不妨重新封装一下file_get_contents函数,代码如下:

1

2

3

4

5

6

7

8

9

function _file_get_content($str) { 

$ctx = stream_context_create(array(   

   'http' => array(   

   'timeout' => 10 //设置一个超时时间,单位为秒   

   )   //开源代码phprm.com

   )   

);   

return file_get_contents($str, 0, $ctx);   

}

로그인 후 복사

如此用_file_get_content代替直接使用file_get_contents 问题解决.


教程地址:

欢迎转载!但请带上文章地址^^

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)