Requirements:
A PHP program runs for a while, but the time is not certain.
Problem:
When the PHP program is running for more than a period of time, the connection is forcibly disconnected.
PHP timeout Processing
In PHP. ini, max_execution_time can be used to set the maximum execution time of PHP scripts. However, this parameter does not take effect in PHP-CGI (PHP-FPM. The maximum execution time of PHP scripts can be controlled:
<Value name = "request_terminate_timeout"> 0 S </value>
That is to say, running max_execution_time in mod_php5.so mode will take effect, but it will not take effect if running in PHP-FPM mode.
Max_execution_time
Only the execution time of the PHP script is calculated, and the Time Beyond execution is not counted. What time is it outside of execution? Includes sleep, data interaction, socket interaction, and so on.
Request_terminate_timeout = 0 is not controlled by time and never times out.
Resource problems caused by request_terminate_timeout
If the value of request_terminate_timeout is set to 0 or is too long, it may cause resource problems of file_get_contents.
If the response to the remote resource requested by file_get_contents is too slow, file_get_contents will remain stuck there and will not time out. We know that max_execution_time in PHP. ini can set the maximum execution time of PHP scripts. However, this parameter does not take effect in PHP-CGI (PHP-FPM. The request_terminate_timeout parameter in the php-fpm.conf configuration file is really able to control the maximum execution time of PHP scripts.
The default value of request_terminate_timeout is 0 seconds. That is to say, the PHP script will continue to be executed. In this way, when all PHP-CGI processes are stuck in the file_get_contents () function, the nginx + PhP webserver can no longer process new PHP requests, nginx returns "502 Bad Gateway" to the user ". Modify this parameter to set the maximum execution time of a PHP script. For example, if file_get_contents () is changed to 30 s, and the webpage content is slow to be obtained, this means that 150 PHP-CGI processes can only process 5 requests per second, webServer is also difficult to avoid "502 Bad Gateway ". The solution is to set request_terminate_timeout to 10 s or a reasonable value, or add a timeout parameter to file_get_contents.
If the request times out frequently, open the slow log of PHP-FPM and use the log to confirm the evaluation timeout.
FastCGI request time control in ngnix
Fastcgi_connect_timeout
Syntax: fastcgi_connect_timeout time
Default Value: fastcgi_connect_timeout 60
Field used: HTTP, server, location
Specify the connection timeout time of the same FastCGI server. The value cannot exceed 75 seconds.
Fastcgi_read_timeout
Syntax: fastcgi_read_timeout time
Default Value: fastcgi_read_timeout 60
Field used: HTTP, server, location
The response timeout time of the front-end FastCGI server. If there are some FastCGI processes that have been output for a long time until they have finished running, or the front-end server response timeout error occurs in the error log, you may need to adjust this value.
Fastcgi_send_timeout
Syntax: fastcgi_send_timeout time
Default Value: fastcgi_send_timeout 60
Field used: HTTP, server, location
The command sets the time for the upstream server to wait for a FastCGI process to send data. If there are some FastCGI processes that have been output for a long time until they have finished running, you can modify this value, if you find some timeout errors in the error log on the server, you can add this value appropriately.
The timeout time of the Request server specified by the command indicates that the two handshakes are completed, instead of the full connection. If no data is transmitted by the client during this period, the server closes the connection.
Testing nginx + FastCGI Configuration
When request_terminate_timeout is set to never time out, the fastcgi_read_timeout setting time in nginx will affect the final time-out time.
In the test, if it is a timeout in PHP-FPM
502 Bad Gateway is displayed
<HTML>
<Head> <title> 502 Bad Gateway </title>
<Body bgcolor = "white">
<Center>
<HR> <center> nginx </center>
</Body>
</Html>
If the CGI configuration in nginx times out
504 gateway time-out is displayed
<HTML>
<Head> <title> 504 gateway time-out </title>
<Body bgcolor = "white">
<Center>
<HR> <center> nginx </center>
</Body>
</Html>
This article from the "Chapter ainemo _ Linux" blog, please be sure to keep this source http://zhangxylinux.blog.51cto.com/5041623/1543035