[Compilation] How to solve the Nginx 502 Bad Gateway error?

888u

Last update at :2023-12-29,Edit by888u

502 Bad Gateway error message. Whether it is our own site or other sites, when browsing the web, we often encounter this kind of reminder in some nginx environments. Let me solve the problem when using nginx server. 502 (Bad Gateway) error.

The meaning of Nginx 502 Bad Gateway is that the requested PHP-CGI has been executed, but due to some reason (usually a problem of reading resources) the execution was not completed and the PHP-CGI process was terminated.
Generally speaking, Nginx 502 Bad Gateway is related to the settings of php-fpm.conf.
php-fpm.conf has two crucial parameters, one is max_children,
The other is request_terminate_timeout, but this value is not universal and needs to be calculated by yourself.
A 502 problem occurs during installation and use. Generally, it is because the default php-cgi process is 5. It may be caused by not enough phpcgi processes. You need to modify /usr/local/php/etc/php-fpm.conf to include them. The max_children value is increased appropriately.
The calculation is as follows:

If your server performance is good enough, broadband resources are sufficient, and there are no infinite loops or bugs in the PHP script, you can directly set request_terminate_timeout to 0s. The meaning of 0s is to let PHP-CGI continue to execute without time limit. And if you can't do this, that is to say, your PHP-CGI may have a BUG, ​​or your bandwidth is not sufficient, or other reasons cause your PHP-CGI to freeze, then it is recommended that you assign a value to request_terminate_timeout. This value can be set according to the performance of the server. Generally speaking, the better the performance, the higher you can set it, ranging from 20 minutes to 30 minutes.

How is the value of max_children calculated? In principle, the larger the value, the better. If there are more php-cgi processes, it will be processed quickly and there will be fewer queued requests. Setting max_children also needs to be set according to the performance of the server. Generally speaking, the memory consumed by each php-cgi on a server is about 20M under normal circumstances.
According to the official answer, we investigated the related possibilities and came up with the following solutions.

1. Check the number of processes of php fastcgi (max_children value)
Code: netstat -anpo | grep "php-cgi" | wc -l
5 (if 5 is displayed)
2. View the current process
Code: top
Observe the number of fastcgi processes. If the number of processes used is equal to or higher than 5, it means that it needs to be increased (depending on the actual situation of your machine)
3. Adjust the relevant settings of /usr/local/php/etc/php-fpm.conf
10
60s
max_children has a maximum of 10 processes, with 20MB of memory per process, up to 200MB.
The execution time of request_terminate_timeout is 60 seconds, which is 1 minute.


Opening the nginx access log, the number of visits is not high, with an average of less than 10 requesting IPs per second. Therefore, it stands to reason that this amount of visits cannot lead to too many PHP requests and 502.

Open the php-fpm.conf log, and there are many PHP warning issues,

Adjust the pm mode of php-fpm to pm = dynamic,

pm.start_servers = 20

pm.min_spare_servers = 5

pm.max_spare_servers = 35

pm.max_requests = 10000

After restarting php-fpm, use ps to view the php process:

ps -ef |grep -c php-fpm
22

At first, accessing PHP was very fast, and then it became slower and slower. Check the process again

ps -ef |grep -c php-fpm
28

From the logs and process changes, it can be concluded that there is a problem with the PHP program, which is caused by too many PHP requests. Probably there are too many PHP requests being called in the PHP page. After telling the development about this problem and the development solved it, the server no longer showed 502.

From the above configuration, we have configured a request number. Of course, this configuration is not optimal. If you still encounter such problems, you can adjust the parameters.

Recommended site searches: Which Hong Kong space is better, website domain name query, site group server, virtual host space, China free proxy server, overseas host, cloud virtual host, registration-free virtual host, dynamic IP dial-up server, virtual host, < /p>

[Compilation] How to solve the Nginx 502 Bad Gateway error?

All copyrights belong to 888u unless special state
取消
微信二维码
微信二维码
支付宝二维码