In depth: Solutions to Nginx 502 Bad Gateway errors

  • 2020-06-03 06:02:15
  • OfStack

max_children=40, each children takes up an average of 20ES3en-30ES4en memory. The more children, the more concurrent you can accept at the same time. 1 general children is the highest number of concurrent sites + floating value.
max_requests = N means that after each children accepts N requests, it kills itself and then re-establishes one children.
PV/max_children = number of request accepted per children [Default is to browse 1 PHP program only called once, perhaps asynchronous call? Interface?]
For example, if the above value is 1000 and you defined it as 10240, it would take more than 10 days for fpm to kill children and rebuild it. If there is a memory leak, the process will consume too much memory to be released, thus reducing the processing power of fpm and causing 1 unaccountable error.
But if you set this value too small, fpm frequently kills children and rebuilds, which also incurs additional overhead.
The best optimization is, of course, to find a balance between constant debugging and the performance of your site.
There is also a lazy method for max_children. If your php is 5.3, you can set style to ES35en-ES36en, and the number of children is automatically controlled by fpm. The corresponding configuration parameters are
start_servers: Number of start processes
min_spare_servers: Minimum number of processes
max_spare_servers: Maximum number of processes
When the server is idle, fpm will kill some extra children to save resources. When the server is busy, the server will automatically create more children.
Nginx 502 Bad Gateway means that the requested ES60en-ES61en has been executed, but the process of ES62en-ES63en has been terminated for some reason (usually a read resource problem) because it has not been executed.
Nginx 502 Bad Gateway and ES68en-ES69en.conf in general.
php-fpm.conf has two crucial parameters, one is max_children,
The other one is request_terminate_timeout, but this value is not generic and needs to be calculated by yourself.
In installed in the process of using the 502 questions, 1 kind is because the default php - cgi process is 5, May 502, because of insufficient use phpcgi process needs to be modified/usr/local/php etc/php - fpm. conf will max_children value increasing of them.
The calculation is as follows:

You can set request_terminate_timeout to 0s if your server is performing well enough and your broadband resources are sufficient and the PHP script does not have an infinite loop or BUG. 0s means to let PHP-CGI1 run straight without any time limit. If you can't do this, then your ES106en-ES107en may have some BUG, or your broadband is not sufficient, or some other reason causes your ES109en-ES110en to fake death, 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 the better the performance, the higher you can set it, anywhere from 20 minutes to 30 minutes.

How does max_children work out? In principle, the larger the better. More es118EN-cgi processes will process more quickly, with fewer queued requests. Setting max_children also needs to be set according to server performance,
Generally speaking, the memory cost of es123EN-ES124en for each ES123en-ES124en normally exists around 20M.
According to the official answer, the relevant possibilities have been screened and netizens' answers combined to come up with the following solution.
1. Check the number of processes of php fastcgi (max_children value)
netstat-anpo | grep "php-cgi" | wc-l
5 (if 5 is shown)
2. View the current process
Code: top
Observe the number of fastcgi processes. If the number of processes used is 5 or more, you need to increase it (depending on your machine).
3, adjust/usr local/php etc/php - fpm. conf associated Settings
< value name max_children = "" > 10 < /value >
< value name request_terminate_timeout = "" > 60s < /value >
max_children up to 10 processes, per process 20MB memory up to 200MB.
request_terminate_timeout executes for 60 seconds, or 1 minute.
Website environment is Nginx +php fastcgi mode. These days running 1 is not stable, always error, report 502 error.
Today, I consulted a former colleague and he told me to check the log of php-ES194en, which recorded a lot of useful information.
So I checked 1 and found that there were a lot of error messages:
Sep 30 08:32:23.289973 [NOTICE] fpm_unix_init_main(), line 271: getrlimit(nofile): max:51200, cur:51200
If and ES199en.ES200en: worker_rlimit_nofile 65500; No 1 must check, set restart service
Mar 01 14:39:15.881047 [NOTICE] fpm_children_make(), line 352: child 12364 (pool default) started
Mar 01 14:39:21.715825 [NOTICE] fpm_got_signal(), line 48: received SIGCHLD
Mar 01 14:39:21.715899 [NOTICE] fpm_children_bury(), line 215: child 11947 (pool default) exited with code 0 after 175.443305 seconds from start

Some report wrong information, say good, search information directly on the Internet.
After searching, the following optimization strategies are summarized:
1. Open and open the file handle of the promoted server
# vi/etc/security/limits conf plus
* soft nofile 65500
* hard nofile 65500
2. Increase the number of open process files of nginx
nginx.conf : worker_rlimit_nofile 65500;
3. Modify the ES230en-ES231en. conf file, mainly needing 2 modifications.
Command ES234en-ES235en to see the limited number of open files. The option rlimit_files in ES236en-ES237en.conf ensures that the value is equal to 1.
< value name max_requests = "" > 10240 < /value >
< value name rlimit_files = "" > 65500 < /value >
# vi /etc/sysctl.conf
At the bottom of the add
After the above modifications, restart PHP. / usr local webserver/php/sbin/php - fpm restart
See if ulimit - n came into effect, or restart the server or/etc/sysctl conf, / etc security/limits conf configuration to take effect
So far there has been no such error message. 1 cut works normally.

Related articles: