In-depth discussion: Nginx 502 Bad Gateway Error Resolution _php Tutorial

Source: Internet
Author: User
MAX_CHILDREN=40, each children an average of 20m-30m memory, the more children, you can accept the more concurrent quantity, the general children value is the maximum number of Web site concurrency + floating value, this value and x memory consumption, 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 = number of request received per children [default preset browse a PHP program that calls only once, perhaps asynchronously? What about the interface? ]
For example, the value above is 1000, and you define 10240, then FPM will take more than 10 days to kill children and rebuild, so if there is a memory leak, it will cause the process to consume too much memory and cannot be freed, which will reduce the processing power of FPM, and produce some inexplicable errors.
But if you set this value too small, fpm kills children and rebuilds frequently, which can lead to additional overhead.
The best optimization of course is based on the operation of your site, to continue to debug, find a balance point.
There's also a lazy approach to max_children, If your PHP is 5.3, then you can set the FPM style to Apache-like, at which time 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 actively kill some extra children to conserve resources, and the server will automatically build more children when the server is busy.
#########################
Nginx 502 Bad Gateway means that the requested php-cgi has been executed, but for some reason (usually the problem of reading resources) is not completed and the php-cgi process is terminated,
in general, Nginx 502 Bad Gateway and php-fpm.conf settings.
Php-fpm.conf has two key parameters, one is Max_children,
The other is request_terminate_timeout, but the value is not universal, but it needs to be calculated by itself.
502 problems occur during the installation, generally because the default php-cgi process is 5, possibly because the phpcgi process is not enough to cause 502, need to modify/usr/local/php/etc/php-fpm.conf Max_ The children value is increased appropriately.
The method is calculated as follows:

If your server performance is good enough, and the broadband resources are sufficient, PHP scripts do not have a dead loop or bug, you can directly set the request_terminate_timeout to 0s. The meaning of 0s is to let php-cgi go on without time limit. And if you can't do this, that is, your php-cgi may be a bug, or your broadband is not enough or other reasons cause your php-cgi suspended animation then suggest you give request_terminate_timeout a value, This value can be set based on the performance of the server. Generally, the better the performance, the higher you can set, 20 minutes-30 minutes.

And how is the value of Max_children calculated? This value is in principle the bigger the better, the php-cgi process is much faster, the queue requests will be very small. Setting Max_children also needs to be set according to the performance of the server.
In general, a server normally consumes about 20M of memory per php-cgi.
According to the official answer, to troubleshoot the relevant possible, and combined with the answer of the Netizen, the following solution is obtained.
1. View the number of processes in PHP fastcgi (max_children value)
Code: Netstat-anpo | grep "php-cgi" | Wc-l
5 (if show 5)
2. View current Process
Code: Top
Observe the number of fastcgi processes, assuming that the number of processes used is equal to or greater than 5, indicating an increase (depending on the actual condition of your machine)
3, adjust the relevant settings/usr/local/php/etc/php-fpm.conf
Ten
60s
Max_children up to 10 processes, per process 20MB memory, up to 200MB.
The request_terminate_timeout executes for 60 seconds, or 1 minutes.
#################################################
Web site operating environment is Nginx +php fastcgi mode. These days the operation has been unstable, always error, reported 502 error.
Today, I consulted with my former colleague, and he told me to check the log of php-fpm, where a lot of useful information was recorded.
So I checked, found that there are indeed a lot of error messages:
Sep 08:32:23.289973 [NOTICE] Fpm_unix_init_main (), line 271:getrlimit (nofile): max:51200, cur:51200
if and Nginx.conf:worker_rlimit_nofile 65500; Inconsistencies must be checked to set restart service
Mar 14:39:15.881047 [NOTICE] Fpm_children_make (), line 352:child 12364 (pool default) started
Mar 14:39:21.715825 [NOTICE] fpm_got_signal (), line 48:received SIGCHLD
Mar 14:39:21.715899 [NOTICE] Fpm_children_bury (), line 215:child 11947 (pool default) exited with code 0 after 175.443 305 seconds from Start


Some error message, just say, directly on the Internet to check information.
After searching, the following several optimization strategies are summed up:
1. Raise the file handle of the server open Open
# vi/etc/security/limits.conf Plus
* Soft Nofile 65500
* Hard Nofile 65500
2, increase the number of open Nginx process file
Nginx.conf:worker_rlimit_nofile 65500;
3, modify the php-fpm.conf file, the main need to modify 2.
Command Ulimit-n View the number of open files that are restricted, and the options in php-fpm.conf rlimit_files ensure that they are consistent with this value.
10240
65500
4.
# vi/etc/sysctl.conf
Bottom add
fs.file-max=65500
After the above modification, restart PHP. /USR/LOCAL/WEBSERVER/PHP/SBIN/PHP-FPM restart
When viewing whether the ulimit-n is in effect, restart the server or the/etc/sysctl.conf,/etc/security/limits.conf configuration takes effect
So far, there have been no error messages. Everything works fine.

http://www.bkjia.com/PHPjc/327220.html www.bkjia.com true http://www.bkjia.com/PHPjc/327220.html techarticle max_children=40, each children an average of 20m-30m memory, the more children, you can accept the more concurrent number, the general children value is the maximum number of concurrent sites + floating value, this value again ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.