The difference between HTTP status code 502 and 504 and their solutions

What does it mean when HTTP response returns 502 and 504?

First of all, we need to understand the difference between 502 errors and 504 errors. Taking PHP, which I am familiar with, as an example, 502 is PHP-FPM (PHP's process manager cannot find an allocable process from the process pool to handle the request, so it will Returning a 502 error is essentially an abnormal status of the PHP process - there are not enough processes or the PHP service is not started at all). In this case, you should check whether the PHP service is started. If it is started, check whether the process pool is too large. Small, all are in a busy state. In this case, the problem can usually be solved by increasing the number of available PHP processes; and the 504 error is a gateway timeout, which means that the PHP process responsible for processing HTTP requests has exceeded the agreed maximum time. The processing result has not yet been returned. The reason for this exception is usually that the SQL execution time is too long or there is an infinite loop in the code.

What should I do if I encounter 502?

Okay, let’s talk about how to judge whether the number of PHP processes is enough when encountering a 502 error. The method is very simple. The idea is to look at how many PHP-CGI processes are currently open, and then look at the PHP-CGI processes that are currently not idle. CGI processes, if these two numbers are close, it means that when a new request occurs, it is easy to cause a 502 error, and the number of previously opened PHP-CGI processes needs to be increased.
The command is as follows:
1. Check how many php-cgi processes are opened on the server.

ps -fe |grep "php-fpm"|grep "pool"|wc -l

2. How many php-cgi processes are currently processing tcp requests?

netstat -anp|grep "php-fpm"|grep "tcp"|grep "pool"|wc -l

When the memory capacity is sufficient, there should be a certain gap between these two numbers. If the gap is small, 502 errors are likely to occur when multiple new requests come.

What should I do if I encounter 504?

As mentioned above, 504 means that the execution code has timed out, so the most direct way is to check the slow log of the database first and see the latest slow log record of the database. If it just happened, and the length of execution It is extremely long, even close to the timeout of your server gateway, so don’t think about it. That’s the problem here. Just optimize the corresponding SQL. If there are no obvious abnormalities in the slow log of the database, then You have to consider whether there is logic in the code that takes too long, or code that communicates with the external interface. Because the network delay or the response time of the other party is too long, and your exception mechanism is not done well, your code keeps running. Waiting for the other party to respond will indirectly cause your code to time out.

Guess you like

Origin blog.csdn.net/one_and_only4711/article/details/119784520