Home Operation and Maintenance Nginx What should I do if nginx reports error 502? Solution sharing

What should I do if nginx reports error 502? Solution sharing

Mar 04, 2022 pm 04:07 PM
nginx

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!

What should I do if nginx reports error 502? Solution sharing

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)

netstat -anpo | grep "php-cgi"| wc -l
Copy after login

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:

    Sep3008:32:23.289973[NOTICE] fpm_unix_init_main(), line 271: getrlimit(nofile): max:51200,cur:51200
    Sep3008:32:23.290212[NOTICE] fpm_sockets_init_main(), line 371:using inherited socket fd=10,“127.0.0.1:9000″
    Sep3008:32:23.290342[NOTICE] fpm_event_init_main(), line 109: libevent:using epoll
    Sep3008:32:23.296426[NOTICE] fpm_init(), line 47: fpm is running, pid 30587
Copy after login

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) )

    <valuename=”max_children”>5120</value>
    <valuename=”max_requests”>600</value>
Copy after login

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值)

netstat -anpo | grep “php-cgi” | wc -l
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

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.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

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.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

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 create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to run nginx apache How to run nginx apache Apr 14, 2025 pm 12:33 PM

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

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 check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

See all articles