How to deal with the "Nginx 502 bad gateway" error
Problem Causes
The "Nginx 502 Bad Gateway" error indicates that the php-cgi process of the access request has started to execute, but the execution has not been completed due to reasons such as failure to read resources, resulting in the termination of the php-cgi process.
Common causes of problems are as follows:
• Insufficient disk space
• The number of php-cgi processes is not enough
• PHP execution timed out
• The php-cgi process is interrupted abnormally
Solution
You can refer to the following operations to check one by one.
In this article, configuration files such as /usr/local/php/etc/php-fpm.conf, /usr/local/php/etc/php.ini, /etc/nginx.conf need to change paths according to different installation environments, please The software path installed in your actual deployment shall prevail.
Not enough disk space
If the "Nginx 502 Bad Gateway" error is displayed directly when accessing a website, it may be caused by insufficient disk space of the ECS instance. You need to perform the following steps to troubleshoot:
1. Log in to the ECS instance remotely.
2. Run the df -h command to check whether the disk space is sufficient.
• If there is sufficient disk space, the problem is not due to a disk space problem.
• If the disk space is insufficient, you need to expand the cloud disk.
The number of php-cgi processes is not enough
If the "Nginx 502 Bad Gateway" error occurs after a period of time in an environment where Nginx has been installed and the website can be accessed normally, it may be caused by insufficient number of php-cgi processes.
At this point, you need to modify the /usr/local/php/etc/php-fpm.conf configuration file, and increase the value of the pm.max_children parameter in the file, that is, increase the number of php-cgi processes.
PHP execution timed out
If the "Nginx 502 Bad Gateway" error is directly displayed when accessing the website, it may be caused by the PHP execution timeout.
At this point, you need to modify the /usr/local/php/etc/php.ini configuration file, and change the value of the max_execution_time parameter in the file to 300. As shown in the image below, you need to modify the value of this parameter from 30 to 300.
The php-cgi process is interrupted abnormally
If the "Nginx 502 Bad Gateway" error is displayed directly when accessing a website, it may be caused by an abnormal interruption of the php-cgi process. You need to perform the following steps to check the method:
1. Log in to the ECS instance remotely.
2. Execute the following commands in sequence to check the number of php fastcgi processes and the value of the pm.max_children parameter.
netstat -anop | grep php-cgi | wc -l
netstat -anpo | grep php-fpm | wc -l
3. Execute the following command to view the current process.
ps aux | grep php-fpm
The display example is as follows, check the number of processes of fastcgi and php-fpm. If the number of processes is greater than or equal to 5, it means that the number of processes needs to be increased.
4. Modify the relevant parameters in the /usr/local/php/etc/php-fpm.conf configuration file.
• Change the value of the pm.max_children parameter to 5, indicating that 5 processes are supported.
Note pm.max_children supports setting up to 5 processes. According to 20 MB of memory for each process, it supports up to 100 MB, that is, the resource consumption time is 1 minute. If pm.max_children increases, the number of php-cgi processes increases, the process processing speed will be accelerated, and the queued requests will be reduced. However, setting pm.max_children also needs to be set according to the performance of the server. Under normal circumstances, the memory consumed by each php-cgi process on a server is about 20 MB. You need to determine pm.max_children according to the actual purchased server memory. value.
• Change the value of the request_terminate_timeout parameter to 60, indicating that the memory recovery mechanism is 1 minute (60 seconds).
Note The value of request_terminate_timeout can be set according to the performance of the server, and the common setting is 60 seconds. Usually the better the server performance, the higher the value you can set.
5. If the execution time of the PHP process exceeds the waiting time of Nginx, you need to appropriately increase the timeout time of the fastcgi_connect_timeout, fastcgi_send_timeout and fastcgi_read_timeout parameters in the /etc/nginx.conf configuration file of Nginx.
Related Articles
-
A detailed explanation of Hadoop core architecture HDFS
Knowledge Base Team
-
What Does IOT Mean
Knowledge Base Team
-
6 Optional Technologies for Data Storage
Knowledge Base Team
-
What Is Blockchain Technology
Knowledge Base Team
Explore More Special Offers
-
Short Message Service(SMS) & Mail Service
50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00