This article was reproduced from: http://www.ttlsa.com/html/3112.html
A. The play says
Whether you are doing operations or development, even if you are a tourist, you will occasionally encounter 502 bad gateway or 504 gateway time-out. appear this page, the service restart, and then really do not restart the server, the problem is solved, but this problem will still haunt you, especially to do operations and maintenance personnel. Night Black Wind High is sleeping, a telephone rang, let you restart service or IISRESET, certainly is extremely uncomfortable, immediately to greet his mother. Hehe, this paper summarizes 502 and 504 fault analysis and solution methods.
Two. Status Code interpretation
502 Bad Gateway: An invalid response was received from the upstream server when the server acting as a gateway or proxy attempted to execute the request.
504 Gateway time-out: When a server acting as a gateway or proxy attempts to execute a request, it fails to receive a response from the upstream server (the server that the URI identifies, such as HTTP, FTP, LDAP), or the secondary server (such as DNS).
Three. 502 Bad Gateway Cause analysis
Submitting the request to the gateway, such as PHP-FPM, is performed, but for some reason the PHP-FPM process terminates execution because it has not been executed. In this case, the problem is very clear, and the gateway services such as the configuration of PHP-FPM.
There are two parameters in the php-fpm.conf configuration file that you need to consider, namely Max_children and Request_terminate_timeout.
Max_children the maximum number of child processes, the maximum number of responses to PHP-FPM is reached at high concurrent requests, and 502 errors will occur for subsequent requests. You can view the current number of connections by using the netstat command.
Request_terminate_timeout sets the time-out for a single request to expire. You should also note the Max_execution_time parameter in php.ini. The 502 error also occurs when the request is terminated.
When you accumulate a large number of PHP requests, you restart PHP-FPM release resources, but a two minutes less, 502 again to present, what is the cause of this? At this point should also consider the database, to see whether the database process has a large number of locked processes, the database deadlock caused the timeout, the front end of the continuation of the request, but the SQL statement is still waiting to release the lock, this time will restart the database service or kill to kill the deadlock SQL process.
For long-time requests, consider using asynchronous methods, and refer to the research on implementing asynchronous operations with PHP.
Four. 504 Gateway time-out reason analysis
The 504 error is generally related to the nginx.conf configuration. Mainly related to the following parameters: Fastcgi_connect_timeout, Fastcgi_send_timeout, Fastcgi_read_timeout, Fastcgi_buffer_size, Fastcgi_ Buffers, Fastcgi_busy_buffers_size, Fastcgi_temp_file_write_size, Fastcgi_intercept_errors. In particular, the first three timeout periods. If the fastcgi buffer is too small it will cause the fastcgi process to be suspended and thus evolve to a 504 error.
Five. Summary
In summary, the 502 error is mainly from four directions:
1. Max_children
2. Request_terminate_timeout, Max_execution_time
3. Database
4. Does the Gateway service start as PHP-FPM
The 504 error mainly looks at nginx.conf about the configuration of gateways such as fastcgi.
Site 502 and 504 error analysis