One, 504 Gateway Timeout
Official explanation : When a server working as a gateway or proxy tried to execute a request, it failed to receive a response from the upstream server (the server identified by the URI, such as HTTP, FTP, LDAP) or the secondary server (such as DNS) in a timely manner.
In layman's terms : In LNMP, nginx did not receive information from php-fpm in time to return. I have been waiting for your php-fpm for too long with nginx. If you do not reply to me, I will tell the browser 504.
This simulation is also very simple, set the fastcgi_read_timeout
parameters.
Example: After setting a 5s timeout, reload the nginx configuration and test the php script sleep(6).
server {
...
fastcgi_read_timeout 5;
}
二、502 Bad Gateway
Official explanation : When a server working as a gateway or proxy tried to execute a request, it received an invalid response from the upstream server.
In layman's terms : In LNMP, nginx did not get a valid response from php-fpm. There may be two common situations, one is that php-fpm does not start, the other is that phpcgi executes the script overtime, and php-fpm kills the process. (In addition, each process of php-fpm will restart after receiving a certain number of requests to prevent memory leaks and cause intermittent 502)
The first is to close php-fpm directly, and 502 will appear.
The second type of setting the timeout period involves two parameters, which are php.ini
medium max_execution_time
and php-fpm.d/www.conf
medium request_terminate_timeout
. max_execution_time
It is the script execution time. After the script execution time is exceeded, an error will be reported. request_terminate_timeout
It is the request timeout period, which is commented out by default and used max_execution_time
.
(1) First, we test the script execution timeout error.
Use set_time_limit()
functions in scripts to temporarily modify max_execution_time
parameters. It is worth noting that set_time_limit()
functions and configuration instructions max_execution_time
only affect the execution time of the script itself, and the time outside of execution will not be counted, such as the sleep function, the waiting time for HTTP requests and so on. So we cannot write sleep() to verify this.
<?php
set_time_limit(2);
var_dump('start');
$start = time();
for($i = 0;;$i++) {
if(time() - $start > 2) {
break;
}
}
var_dump('end');
At this time, the two parameters are equal, and an error will occur
(2) Then we modify the request_terminate_timeout
parameters and remove the comments. The default 0 means unlimited, and it is changed to 1. Restart php-fpm, 502 appears.