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)
netstat -anpo | grep "php-cgi"| wc -l
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 <pre class='brush:php;toolbar:false;'> fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;</pre>
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
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>
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
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!

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINXUnit improves application flexibility and performance with its dynamic configuration and high-performance architecture. 1. Dynamic configuration allows the application configuration to be adjusted without restarting the server. 2. High performance is reflected in event-driven and non-blocking architectures and multi-process models, and can efficiently handle concurrent connections and utilize multi-core CPUs.

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINXUnit supports multiple programming languages and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.