What should I do if nginx reports error 502? Solution sharing
What should I do if nginx reports error 502? This article will talk about the solution to nginx error 502. I hope it will be helpful to everyone!
http request process: Under normal circumstances, when submitting a dynamic request, nginx will directly transfer the request to php-fpm, and php-fpm will then allocate the php-cgi process to process related requests, and then return in sequence. Finally, nginx feeds the results back to the client browser.
Nginx 502 Bad Gateway error is a problem with FastCGI
Solution
If you encounter 502 problems, you can give priority to following Follow these two steps to solve it.
1. Check whether the current number of PHP FastCGI processes is sufficient (max_children value)
1 |
|
If the actual number of "FastCGI processes" used is close to the default "FastCGI "Number of processes", then it means that "Number of FastCGI processes" is not enough and needs to be increased.
2. The execution time of some PHP programs exceeds the waiting time of Nginx (php lacks memory)
Increase the FastCGI timeout time in the nginx.conf configuration file, for example :
memory_limit=64M in <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'> fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;</pre><div class="contentsignin">Copy after login</div></div>
php.ini, restart nginx.
If this modification still cannot solve the problem, you can refer to the following solutions:
3, max-children and max-requests
One nginx php (fpm) xcache is running on the server, with an average daily visit volume of about 300W pv
Such a situation often occurs recently: the php page opens very slowly, the cpu usage suddenly drops to a very low level, and the system load suddenly When it rises to a very high level, if you check the traffic of the network card, you will find that it suddenly dropped to a very low level. This situation only lasted for a few seconds and then recovered.
Checking the log file of php-fpm found some clues:
1 2 3 4 |
|
Before these sentences, there are more than 1,000 lines of closing children and Turn on children's log
It turns out that php-fpm has a parameter max_requests, which specifies the maximum number of requests each child can handle before being closed. The default setting is 500. Because PHP polls requests to each child, under heavy traffic, each child takes about the same amount of time to reach max_requests, which causes all children to be closed basically at the same time.
During this period, nginx cannot transfer the php file to php-fpm for processing, so the cpu will drop to a very low level (no need to process php, let alone execute sql), and the load will rise to a very high level (close and Turn on children, nginx waits for php-fpm), and the network card traffic is also reduced to very low (nginx cannot generate data to transmit to the client)
Increase the number of children, and set max_requests to less than 0 or a relatively large value :
Open/usr/local/php/etc/php-fpm.conf
Adjust the following two parameters (according to the actual situation of the server, too large will not work) )
1 2 |
|
Then restart php-fpm.
5. Increase the buffer capacity
Open the nginx error log and find an error message like "pstream sent too big header while reading response header from upstream" . After checking the information, the general idea is that there is a bug in the nginx buffer. The buffer occupied by the page consumption of our website may be too large. Referring to the modification method written by a foreigner, the buffer capacity setting is increased, and the 502 problem is completely solved. Later, the system administrator adjusted the parameters and retained only two setting parameters: client head buffer and fastcgi buffer size.
6. request_terminate_timeout
If 502 occurs mainly during some posts or database operations, rather than common in static page operations, then you can check Check one of the php-fpm.conf settings: request_terminate_timeout
This value is max_execution_time
, which is the execution script time of fast-cgi.
0s is closed, which means it will be executed indefinitely. (When I was dressing, I changed a number without looking carefully)
In optimizing fastcgi, you can also change this value for 5 seconds to see the effect.
If the number of php-cgi processes is not enough, the php execution time is long, or the php-cgi process dies, a 502 error will occur.
Extended knowledge:
Nginx 502 Bad Gateway means that the requested PHP-CGI has been executed, but for some reason (usually a problem with reading resources) The PHP-CGI process is terminated due to incomplete execution. 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 and the other is request_terminate_timeout, but this value is not universal and needs to be calculated by yourself. When a 502 problem occurs during installation and use, it is usually because the default php-cgi process is 5. It may be caused by not enough php-cgi processes. You need to modify /usr/local/php/etc/php-fpm.conf Increase the max_children value appropriately.
The calculation method is as follows:
如果你的服务器性能足够好,且宽带资源足够充足,PHP脚本没有系循环或BUG的话你可以直接将 request_terminate_timeout设置成0s。0s的含义是让PHP-CGI一直执行下去而没有时间限制。而如果你做不到这一点,也就 是说你的PHP-CGI可能出现某个BUG,或者你的宽带不够充足或者其他的原因导致你的PHP-CGI假死那么就建议你给 request_terminate_timeout赋一个值,这个值可以根据服务器的性能进行设定。一般来说性能越好你可以设置越高,20分钟-30分 钟都可以。而max_children这个值又是怎么计算出来的呢?这个值原则上是越大越好,php-cgi的进程多了就会处理的很快,排队的请求就会很少。 设置max_children也需要根据服务器的性能进行设定,一般来说一台服务器正常情况下每一个php-cgi所耗费的内存在20M左右。
按照官方的答案,排查了相关的可能,并结合了网友的答案,得出了下面的解决办法。
1、查看php fastcgi的进程数(max_children值)
1 |
|
5(假如显示5)
2、查看当前进程
top观察fastcgi进程数,假如使用的进程数等于或高于5个,说明需要增加(根据你机器实际状况而定)
3、调整/usr/local/php/etc/php-fpm.conf 的相关设置
<value name=”max_children”>10</value><value name=”request_terminate_timeout”>60s</value>
max_children最多10个进程,按照每个进程20MB内存,最多200MB。request_terminate_timeout执行的时间为60秒,也就是1分钟。
推荐教程:nginx教程
The above is the detailed content of What should I do if nginx reports error 502? Solution sharing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.
