Labels: nginx, 502, 504
Most O & M personnel will refer to the nginx optimization document of Master Zhang banquet to solve nginx errors 502 or 504 for a long time. But under what circumstances is 502, and under what circumstances is 504, it is not very clear that PHP-CGI cannot handle it. Some time ago, curl simulation timed out and occasionally found that sometimes it was 502 and 504, just today we were free to test.
The max_execution_time parameter in PHP. ini can be used to set the maximum execution time of PHP scripts. However, this parameter does not take effect in nginx + 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 Apache + PHP-CGI Mode will take effect. 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.
If the value of request_terminate_timeout is set to 0 or too long, many processes will get stuck. We know that max_execution_time in PHP. ini can set the maximum execution time of PHP scripts, but this parameter in nginx + PHP-CGI (PHP-FPM) does not take effect.
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 the PHP-CGI processes are stuck, this nginx + PhP will no longer be able to process new PHP requests, and nginx will return "502 Bad Gateway" to the user ".
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.
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.
The following describes the test procedure.
We first wrote a simple PHP page
<? PHP
Echo "start ";
Sleep (61 );
Echo "end ";
?>
1. Set the PHP Execution timeout to 60 s.
<Value name = "request_terminate_timeout"> 60 S </value>
502 Bad Gateway is returned.
Set <value name = "request_terminate_timeout"> 59 S </value>
You can respond normally.
2. Set <value name = "request_terminate_timeout"> 0 S </value>. However, PHP can always Execute
Then set it (Here we use sleep, so the real impact is the fastcgi_read_timeout parameter)
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 60;
The result returns 504 gateway time-out.
To:
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Then the response is normal.
To sum up, the 502 error is determined by the request_terminate_timeout parameter, and the 504 error is
The fastcgi_connect_timeout, fastcgi_send_timeout, and fastcgi_read_timeout parameters are determined. Now that you know the principles, it is easy to solve nginx 502 and 504 errors. I believe that everyone who is smart knows how to solve them.
Analysis of nginx 502 and 504 errors