Home php教程 php手册 PHP超时处理全面总结(1)

PHP超时处理全面总结(1)

Jun 13, 2016 am 11:15 AM
php comprehensive exist deal with Work develop Summarize Overview of time out

【 概述 】

在PHP开发中工作里非常多使用到超时处理到超时的场合,我说几个场景:

1. 异步获取数据如果某个后端数据源获取不成功则跳过,不影响整个页面展现

2. 为了保证Web服务器不会因为当个页面处理性能差而导致无法访问其他页面,则会对某些页面操作设置

3. 对于某些上传或者不确定处理时间的场合,则需要对整个流程中所有超时设置为无限,否则任何一个环节设置不当,都会导致莫名执行中断

4. 多个后端模块(MySQL、Memcached、HTTP接口),为了防止单个接口性能太差,导致整个前面获取数据太缓慢,影响页面打开速度,引起雪崩

5. 。。。很多需要超时的场合

这些地方都需要考虑超时的设定,但是PHP中的超时都是分门别类,各个处理方式和策略都不同,为了系统的描述,我总结了PHP中常用的超时处理的总结。

【Web服务器超时处理】

[ Apache ]

一般在性能很高的情况下,缺省所有超时配置都是30秒,但是在上传文件,或者网络速度很慢的情况下,那么可能触发超时操作。

目前 apache fastcgi php-fpm 模式 下有三个超时设置:

fastcgi 超时设置:

修改 httpd.conf 的fastcgi连接配置,类似如下:

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">IfModule</span><span> mod_fastcgi.c</span><span class="tag">></span><span>   </span></span></span></li>
<li><span>    FastCgiExternalServer /home/forum/apache/apache_php/cgi-bin/php-cgi -socket /home/forum/php5/etc/php-fpm.sock  </span></li>
<li class="alt"><span> </span></li>
<li><span>    ScriptAlias /fcgi-bin/ "/home/forum/apache/apache_php/cgi-bin/"  </span></li>
<li class="alt"><span> </span></li>
<li><span>    AddHandler php-fastcgi .php  </span></li>
<li class="alt"><span> </span></li>
<li><span>    Action php-fastcgi /fcgi-bin/php-cgi  </span></li>
<li class="alt"><span> </span></li>
<li><span>    AddType application/x-httpd-php .php  </span></li>
<li class="alt"><span> </span></li>
<li>
<span class="tag"></span><span class="tag-name">IfModule</span><span class="tag">></span><span> </span>
</li>
</ol>
Copy after login

缺省配置是 30s,如果需要定制自己的配置,需要修改配置,比如修改为100秒:(修改后重启 apache):

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">IfModule</span><span> mod_fastcgi.c</span><span class="tag">></span><span> </span></span></span></li>
<li><span> </span></li>
<li class="alt"><span>    FastCgiExternalServer /home/forum/apache/apache_php/cgi-bin/php-cgi -socket /home/forum/php5/etc/php-fpm.sock  -idle-timeout 100  </span></li>
<li><span> </span></li>
<li class="alt"><span>    ScriptAlias /fcgi-bin/ "/home/forum/apache/apache_php/cgi-bin/"  </span></li>
<li><span> </span></li>
<li class="alt"><span>    AddHandler php-fastcgi .php  </span></li>
<li><span> </span></li>
<li class="alt"><span>    Action php-fastcgi /fcgi-bin/php-cgi  </span></li>
<li><span> </span></li>
<li class="alt"><span>    AddType application/x-httpd-php .php  </span></li>
<li><span> </span></li>
<li class="alt">
<span class="tag"></span><span class="tag-name">IfModule</span><span class="tag">></span><span> </span>
</li>
</ol>
Copy after login

如果超时会返回500错误,断开跟后端php服务的连接,同时记录一条apache错误日志:

<ol class="dp-xml">
<li class="alt"><span><span>[Thu Jan 27 18:30:15 2011] [error] [client 10.81.41.110] FastCGI: comm with server "/home/forum/apache/apache_php/cgi-bin/php-cgi" aborted: idle timeout (30 sec)  </span></span></li>
<li><span> </span></li>
<li class="alt"><span>[Thu Jan 27 18:30:15 2011] [error] [client 10.81.41.110] FastCGI: incomplete headers (0 bytes) received from server "/home/forum/apache/apache_php/cgi-bin/php-cgi" </span></li>
</ol>
Copy after login

其他 fastcgi 配置参数说明:

<ol class="dp-xml">
<li class="alt"><span><span>IdleTimeout 发呆时限   </span></span></li>
<li><span>ProcessLifeTime 一个进程的最长生命周期,过期之后无条件kill  </span></li>
<li class="alt"><span> </span></li>
<li><span>MaxProcessCount 最大进程个数  </span></li>
<li class="alt"><span> </span></li>
<li><span>DefaultMinClassProcessCount 每个程序启动的最小进程个数  </span></li>
<li class="alt"><span> </span></li>
<li><span>DefaultMaxClassProcessCount 每个程序启动的最大进程个数  </span></li>
<li class="alt"><span> </span></li>
<li><span>IPCConnectTimeout 程序响应超时时间  </span></li>
<li class="alt"><span> </span></li>
<li><span>IPCCommTimeout 与程序通讯的最长时间,上面的错误有可能就是这个值设置过小造成的  </span></li>
<li class="alt"><span> </span></li>
<li><span>MaxRequestsPerProcess 每个进程最多完成处理个数,达成后自杀 </span></li>
</ol>
Copy after login

[ Lighttpd ]

配置:lighttpd.conf

Lighttpd配置中,关于超时的参数有如下几个(篇幅考虑,只写读超时,写超时参数同理):

主要涉及选项:

<ol class="dp-xml">
<li class="alt"><span><span class="attribute">server.max-keep-alive-idle</span><span> = </span><span class="attribute-value">5</span><span> </span></span></li>
<li><span> </span></li>
<li class="alt">
<span class="attribute">server.max-read-idle</span><span> = </span><span class="attribute-value">60</span><span> </span>
</li>
<li><span> </span></li>
<li class="alt">
<span class="attribute">server.read-timeout</span><span> = </span><span class="attribute-value">0</span><span> </span>
</li>
<li><span> </span></li>
<li class="alt">
<span class="attribute">server.max-connection-idle</span><span> = </span><span class="attribute-value">360</span><span> </span>
</li>
</ol>
Copy after login
<ol class="dp-xml">
<li class="alt"><span><span>--------------------------------------------------  </span></span></li>
<li><span> </span></li>
<li class="alt"><span># 每次keep-alive 的最大请求数, 默认值是16  </span></li>
<li><span> </span></li>
<li class="alt">
<span class="attribute">server.max-keep-alive-requests</span><span> = </span><span class="attribute-value">100</span><span> </span>
</li>
<li><span> </span></li>
<li class="alt"><span># keep-alive的最长等待时间, 单位是秒,默认值是5  </span></li>
<li><span> </span></li>
<li class="alt">
<span class="attribute">server.max-keep-alive-idle</span><span> = </span><span class="attribute-value">1200</span><span> </span>
</li>
<li><span> </span></li>
<li class="alt"><span># lighttpd的work子进程数,默认值是0,单进程运行  </span></li>
<li><span> </span></li>
<li class="alt">
<span class="attribute">server.max-worker</span><span> = </span><span class="attribute-value">2</span><span> </span>
</li>
<li><span> </span></li>
<li class="alt"><span># 限制用户在发送请求的过程中,最大的中间停顿时间(单位是秒),  </span></li>
<li><span> </span></li>
<li class="alt"><span># 如果用户在发送请求的过程中(没发完请求),中间停顿的时间太长,lighttpd会主动断开连接  </span></li>
<li><span> </span></li>
<li class="alt"><span># 默认值是60(秒)  </span></li>
<li><span> </span></li>
<li class="alt">
<span class="attribute">server.max-read-idle</span><span> = </span><span class="attribute-value">1200</span><span> </span>
</li>
<li><span> </span></li>
<li class="alt"><span># 限制用户在接收应答的过程中,最大的中间停顿时间(单位是秒),  </span></li>
<li><span> </span></li>
<li class="alt"><span># 如果用户在接收应答的过程中(没接完),中间停顿的时间太长,lighttpd会主动断开连接  </span></li>
<li><span> </span></li>
<li class="alt"><span># 默认值是360(秒)  </span></li>
<li><span> </span></li>
<li class="alt">
<span class="attribute">server.max-write-idle</span><span> = </span><span class="attribute-value">12000</span><span> </span>
</li>
<li><span> </span></li>
<li class="alt"><span># 读客户端请求的超时限制,单位是秒, 配为0表示不作限制  </span></li>
<li><span> </span></li>
<li class="alt"><span># 设置小于max-read-idle时,read-timeout生效  </span></li>
<li><span> </span></li>
<li class="alt">
<span class="attribute">server.read-timeout</span><span> = </span><span class="attribute-value">0</span><span> </span>
</li>
<li><span> </span></li>
<li class="alt"><span># 写应答页面给客户端的超时限制,单位是秒,配为0表示不作限制  </span></li>
<li><span> </span></li>
<li class="alt"><span># 设置小于max-write-idle时,write-timeout生效  </span></li>
<li><span> </span></li>
<li class="alt">
<span class="attribute">server.write-timeout</span><span> = </span><span class="attribute-value">0</span><span> </span>
</li>
<li><span> </span></li>
<li class="alt"><span># 请求的处理时间上限,如果用了mod_proxy_core,那就是和后端的交互时间限制, 单位是秒  </span></li>
<li><span> </span></li>
<li class="alt">
<span class="attribute">server.max-connection-idle</span><span> = </span><span class="attribute-value">1200</span><span> </span>
</li>
<li><span> </span></li>
<li class="alt"><span>-------------------------------------------------- </span></li>
</ol>
Copy after login

说明:

对于一个keep-alive连接上的连续请求,发送第一个请求内容的最大间隔由参数max-read-idle决定,从第二个请求起,发送请求内容的最大间隔由参数max-keep-alive-idle决定。请求间的间隔超时也由max-keep-alive-idle决定。发送请求内容的总时间超时由参数read-timeout决定。Lighttpd与后端交互数据的超时由max-connection-idle决定。

延伸阅读:

http://www.snooda.com/read/244

[ Nginx ]

配置:nginx.conf

<ol class="dp-xml">
<li class="alt"><span><span>http {   </span></span></li>
<li><span> </span></li>
<li class="alt"><span>    #Fastcgi: (针对后端的fastcgi 生效, fastcgi 不属于proxy模式)  </span></li>
<li><span> </span></li>
<li class="alt"><span>    fastcgi_connect_timeout 5;    #连接超时  </span></li>
<li><span> </span></li>
<li class="alt"><span>    fastcgi_send_timeout 10;       #写超时  </span></li>
<li><span> </span></li>
<li class="alt"><span>    fastcgi_read_timeout 10;        #读取超时  </span></li>
<li><span> </span></li>
<li class="alt"><span>   </span></li>
<li><span> </span></li>
<li class="alt"><span>    #Proxy: (针对proxy/upstreams的生效)  </span></li>
<li><span> </span></li>
<li class="alt"><span>    proxy_connect_timeout 15s;    #连接超时  </span></li>
<li><span> </span></li>
<li class="alt"><span>    proxy_read_timeout 24s;          #读超时  </span></li>
<li><span> </span></li>
<li class="alt"><span>    proxy_send_timeout 10s;         #写超时  </span></li>
<li><span> </span></li>
<li class="alt"><span>} </span></li>
</ol>
Copy after login

说明:

Nginx 的超时设置倒是非常清晰容易理解,上面超时针对不同工作模式,但是因为超时带来的问题是非常多的。

延伸阅读:

http://hi.baidu.com/pibuchou/blog/item/a1e330dd71fb8a5995ee3753.html

http://hi.baidu.com/pibuchou/blog/item/7cbccff0a3b77dc60b46e024.html

http://hi.baidu.com/pibuchou/blog/item/10a549818f7e4c9df703a626.html

http://www.apoyl.com/?p=466

1

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles