Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What if there is a 502 Bad Gateway error in Nginx

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the Nginx 502 Bad Gateway error how to do, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Max_children=40, each children takes up 20M-30M memory on average. The more children, the more concurrency can be accepted at the same time. Generally, the value of children is the highest concurrent number of websites + floating value. This value is then × memory footprint, which is the memory you need to use.

Max_requests = N means that when each children accepts N requests, it kills itself and then re-establishes a children.

PV / max_children = the number of request accepted by each children [default is to browse a PHP program only once, or asynchronously? What about the interface?]

For example, if the above value is 1000 and you define 10240, it will take more than 10 days for fpm to kill children and rebuild, so that if there is a memory leak, it will cause the process to occupy too much memory and cannot be released, thus reducing the processing power of fpm and causing some inexplicable errors.

But if you set this value too low, fpm will frequently kill children and rebuild it, which will also incur extra overhead.

The best optimization, of course, is to constantly debug and find a balance according to the operation of your website.

There is also a lazy practice for max_children. If your php is 5.3, you can set the style of fpm to apache-like, and the number of children will be automatically controlled by fpm. The corresponding configuration parameters are

Start_servers: number of initiating processes

Min_spare_servers: minimum number of processes

Max_spare_servers: maximum number of processes

When the server is idle, fpm will actively kill some extra children to save resources. When the server is busy, the server will automatically build more children.

#

Nginx 502 Bad Gateway means that the requested PHP-CGI has been executed, but the PHP-CGI process is terminated due to some reason (usually the problem of reading resources) that the execution is not finished.

Generally speaking, Nginx 502 Bad Gateway is related to the setting 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 generic, but needs to be calculated by yourself.

There are 502 problems in the process of installation and use, generally because the default php-cgi process is 5, which may be caused by insufficient phpcgi processes, so you need to modify / usr/local/php/etc/php-fpm.conf to increase the max_ child value appropriately.

The method of calculation is as follows:

If your server performance is good enough, and the broadband resources are sufficient, and the PHP script does not have an endless loop or BUG, you can directly set the request_terminate_timeout to 0s. 0s means to keep the PHP-CGI running without a time limit. If you can't do this, that is to say, there may be some BUG in your PHP-CGI, or your broadband is insufficient or your PHP-CGI is suspended for other reasons, then it is recommended that you assign a value to request_terminate_timeout, which can be set according to the performance of the server. Generally speaking, the better the performance, the higher you can set it, even 20-30 minutes.

And how is the value of max_children calculated? In principle, the larger the better, the more php-cgi processes will be processed quickly, and the number of queued requests will be very small. Setting max_children also needs to be set according to the performance of the server.

Generally speaking, a server normally consumes about 20m of memory per php-cgi.

According to the official answer, checked the relevant possibilities, and combined with the answers of netizens, came to the following solution.

1. Check the number of processes in php fastcgi (max_ child value)

Code: netstat-anpo | grep "php-cgi" | wc-l

5 (if displayed)

2. View the current process

Code: top

Observe the number of fastcgi processes. If the number of processes used is equal to or more than 5, it means that it needs to be increased (depending on the actual condition of your machine)

3. Adjust the relevant settings of / usr/local/php/etc/php-fpm.conf

ten

60s

Max_children up to 10 processes, according to each process 20MB memory, up to 200MB.

The execution time of the request_terminate_timeout is 60 seconds, or 1 minute.

# # #

The operating environment of the website is Nginx + php fastcgi mode. The operation has been unstable these days, always making mistakes and reporting 502 errors.

Today, I asked my former colleague for advice. He told me to check php-fpm 's log, where a lot of useful information was recorded.

So I checked and found that there were a lot of error messages:

Sep 308 line 32 Sep 23.289973 [NOTICE] fpm_unix_init_main (), getrlimit (nofile): max:51200, cur:51200

If it is inconsistent with nginx.conf: worker_rlimit_nofile 65500;, you must check and set the restart service.

Mar 01 14 line 39 fpm_children_make 15.881047 [NOTICE] fpm_children_make (), child 12364 (pool default) started

Mar 01 14 fpm_got_signal 39 fpm_got_signal 21.715825 [NOTICE], line 48: received SIGCHLD

Mar 01 14V 39 fpm_children_bury 21.715899 [NOTICE] fpm_children_bury (), line 215: child 11947 (pool default) exited with code 0 after 175.443305 seconds from start

Some wrong information, it is easy to say, directly online to check the information.

After searching, the following optimization strategies are summarized:

1. Open the file handle of the promotion server.

# vi / etc/security/limits.conf plus

* soft nofile 65500

* hard nofile 65500

2. Upgrade the number of process files opened by nginx

Nginx.conf: worker_rlimit_nofile 65500

3. To modify the php-fpm.conf file, you mainly need to modify 2 places.

The command ulimit-n checks the limit on the number of open files, and the option rlimit_files in php-fpm.conf ensures that it is consistent with this value.

10240

65500

4 、

# vi / etc/sysctl.conf

Bottom add

Fs.file-max=65500

Restart PHP after the above modifications. / usr/local/webserver/php/sbin/php-fpm restart

Check whether ulimit-n takes effect, otherwise the configuration of restarting the server or / etc/sysctl.conf or / etc/security/limits.conf will take effect.

So far, there has been no above error message. Everything's working fine.

Thank you for reading this article carefully. I hope the article "what to do if there are 502 Bad Gateway errors in Nginx" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report