Simulation 502 and 504

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_timeoutparameters.
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.inimedium max_execution_timeand php-fpm.d/www.confmedium request_terminate_timeout. max_execution_timeIt is the script execution time. After the script execution time is exceeded, an error will be reported. request_terminate_timeoutIt 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_timeparameters. It is worth noting that set_time_limit()functions and configuration instructions max_execution_timeonly 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
Insert picture description here

(2) Then we modify the request_terminate_timeoutparameters and remove the comments. The default 0 means unlimited, and it is changed to 1. Restart php-fpm, 502 appears.


Summary: 504 is that the timeout time of nginx is less than the timeout time of php-fpm and the script execution time of php.ini; 502 is that the timeout time of php-fpm is less than the timeout time of nginx and the script execution time of php.ini.
Thinking: The page calls a time-consuming interface to update the data. If 504 appears, has the data been updated? Please modify the different parameters to test it yourself, you will be more impressed.
Tip: The above parameter setting is too small, don’t forget to change it back after the test.

Guess you like

Origin blog.csdn.net/z772532526/article/details/105341140