Let's take a look at PHP-FPM's configuration file php-fpm.conf
include=/etc/php-fpm.d/*.conf;;;;;;;;;;;;;;;;;;;;;;;;; Global Options;;;;;;;;;;;;;;;;;;; [global]pid =/var/run/php-fpm/php-fpm.pid;pid settings, default in the installation directory of Var/run/php-fpm.pid, it is recommended to turn on error_log =/data/logs/error/ Php-fpm.error.log; error log, default/data/logs/error/php-fpm.error.loglog_level = Error in the installation directory, error level. Available levels are: alert (must be processed immediately), error (Error condition), warning (warning condition), notice (general important information), debug (debug information). Default: Notice.emergency_restart_threshold = 10emergency_restart_interval = 1m; indicated in Emergency_restart_ If the number of php-cgi processes with SIGSEGV or sigbus errors within the value of interval is exceeded; Emergency_restart_threshold, php-fpm gracefully restarts. These two options generally leave the default values. Process_control_timeout = 10s; Sets the time-out period for the child process to accept the primary process multiplexing signal. Available units: s (seconds), M (min), H (Hours), or D (day) default units: s (seconds). Default value: 0daemonize = yes, background performs FPM, default is yes, if for debugging you can change to No. In FPM, you can use different settings to run multiple process pools. These settings can be set individually for each process pool, Listen.backlog = -1;backlog, 1 for unrestricted, determined by the operating system, line commented out, listen.allowed_clients = 127.0.0.1; Allow access to the IP of the fastcgi process, set any to unlimited IP, if you want to set other hosts Nginx can also access this FPM process, listen to set the cost of access to the IP.; The default value is any. Each address is separated by commas. If not set or empty, allow any server to request connection;;;;;;;;;;;;;;;;;;;; Pool definitions ;;;;;;;;;;;;;;;;;;;;;; See/etc/php-fpm.d/*.conf
Look at the configuration file for the specific pool
; Start a new pool named ' www ' [www]listen =/dev/shm/php-fpm.socket;listen = 127.0.0.1:9000;php-fpm Listening port, which is the address of PHP processing in Nginx, The general default is the available format: ' Ip:port ', ' Port ', '/dev/shm/php-fpm.socket ', each process pool needs to be set; Here is in memory, more efficient Listen.owner = Wwwlisten.group = Webgrplisten.mode = 0666;unix Socket setting option, if you use TCP access, the comment here is user = Wwwgroup = webgrp; Start process account and group PM = static; For a dedicated server, The PM can be set to static, the options are static and dynamic, and if Static is selected, the number of fixed child processes is specified by Pm.max_children, and if dynamic is selected, it is determined by the remaining three parameters Pm.max_children = 16 The maximum number of child processes pm.start_servers = 8; Number of processes at startup pm.min_spare_servers = 5; The minimum number of idle processes is guaranteed, if the idle process is less than this value, a new child process Pm.max_spare_servers = 25 is created; The maximum number of idle processes is guaranteed, if the idle process is greater than this value, this is cleaned pm.max_requests = 10240; Sets the number of requests for the service before each child process is reborn. is useful for third-party modules that may have a memory leak, and if set to 0, requests are accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0;pm.status_path = URL of the/STATUS;PHP-FPM status page. If not set, the status page cannot be accessed. Default value: None. Munin monitoring will be used to; Ping.path =/ping;php-fpm the ping URL of the monitoring page. If not set, you cannot access the ping page, which is used to externally detect whether the PHP-FPM is alive and can respond to requests, note that you must start with a slash (/).;p ing.response = Pong; used to define the return of the ping request, return the HTTP 200 Text/plain formatted text. Default value: PongrequeSt_terminate_timeout = 600s; Sets the time-out for a single request to abort. This option may be useful for scripts in the php.ini setting where ' max_execution_time ' is not aborted for some special reason, and is set to 0 for OFF, and you can try to change this option when a 502 error occurs frequently request_slowlog_ Timeout = 5s; When a time-out is requested for the setting, the corresponding PHP call stack information is written to the slow log. A setting of 0 means offslowlog =/data/logs/error/www-slow.log; a slow-request logging, with request_slowlog_timeout using rlimit_files = 65535; Sets the Rlimit limit for the file open descriptor. Default: System-defined value the default open handle is 1024, can be viewed using ulimit-n, ulimit-n 2048 modifies Rlimit_core = 0, and the core Rlimit maximum limit value is set. Available values: ' Unlimited ', 0, or positive integer. Default value: System-defined value. Catch_workers_output = yes; redirects the stdout and stderr in the running process to the primary error log file. If not set, stdout and stderr will be redirected to/dev/null according to the FASTCGI rules. Default value: Empty.; security.limit_extensions =. php. php3 php4. php5;env[hostname] = $HOSTNAME; Env[path] =/usr/local/bin:/usr/bin:/bin; ENV[TMP] =/tmp;env[tmpdir] =/tmp;env[temp] =/tmp;php_admin_value[sendmail_path] =/usr/sbin/sendmail-t-i-f [email&nb Sp;protected];p hp_flag[display_errors] = Onphp_admin_value[error_log] =/var/log/php-fpm/www-error.logphp_admin_ Flag[log_errors] = On;php_admin_value[memory_limit] = 32M
So let's take a look at the 502 wrong culprit.
1. Resource problems caused by request_terminate_timeout
Request_terminate_timeout value if set to 0 or too long may cause file_get_contents,fsockopen resource problems
If the remote resource requested by file_get_contents is too slow, file_get_contents will remain stuck there and will not time out.
We know that php.ini inside Max_execution_time can set the maximum execution time for PHP scripts, but in php-cgi (PHP-FPM), this parameter does not work. The real ability to control the maximum execution time of a PHP script is the request_terminate_timeout parameter in the php-fpm.conf configuration file . Nginx > PHP-FPM > PHP priority
The default value for Request_terminate_timeout is 0 seconds, meaning that the PHP script will continue to execute. This way, when all the PHP-FPM processes are stuck in the file_get_contents () function, the nginx+php WebServer can no longer process the new PHP request, andNginx will return "502 bad Gate" to the user. ". To modify this parameter, it is necessary to set the maximum execution time for a PHP script, but the symptom is not a cure. For example, to 30s, if file_get_contents () to get a slow page content, which means that 150 php-cgi process, only 5 requests per second, WebServer also difficult to avoid "502 bad Gateway." The workaround is to set the request_terminate_timeout to 10s or a reasonable value, or to add a timeout parameter to file_get_contents
2. Improper configuration of max_requests parameter may cause intermittent 502 error
Sets the number of requests for the service before each child process is reborn. is useful for third-party modules that may have a memory leak.
If set to ' 0 ', the request is always accepted. Equivalent to the PHP_FCGI_MAX_REQUESTS environment variable. Default value: 0, this configuration means that when the number of requests processed by a php-cgi process accumulates to a certain number, the process is automatically restarted
But why restart the process?
Generally in the project, we will more or less use some PHP third-party libraries, these third-party libraries often have a memory leak problem, if you do not periodically restart the php-cgi process, it is bound to cause memory usage is increasing. So php-fpm, as the manager of the php-cgi, provides a monitoring function that restarts the PHP-CGI process with a specified number of requests to ensure that the amount of memory is not increased
It is because of this mechanism, in high-concurrency sites, often cause 502 errors, I guess the reason is that php-fpm to the request from NGINX queue not handled well
The solution is to set this value as large as possible, to minimize the number of php-cgi re-SPAWN, and to improve overall performance. In our own actual production environment we found that the memory leak was not obvious, so we set the value to very large (204800). We must set this value according to their actual situation, can not blindly increase
The purpose of this mechanism is to ensure that the php-cgi does not take up too much memory, and why does it not be handled by detecting memory? Restarting the PHP-FPM process by setting the peak intrinsic consumption of the process will be a better solution
3. Errors caused
NOTICE] Child 19214 stopped-tracing [NOTICE] about to trace 19214 [ERROR] Ptrace (peekdata) failed:input/output error (5) [NOTICE] finished trace of 19214 [WARNING] [pool www] child 19208, script ' blahblah.php ' executing too slow (30.041419 SEC), logging [NOTICE] child 19208 stopped for tracing [NOTICE] about to trace 19208 [ERROR] Ptrace (peekdata) FAILED:INP Ut/output error (5) [NOTICE] finished trace of 19208 [WARNING] [pool www] child 19218, script ' blahblah.php ' executing too Slow (30.035029 sec), logging
Workaround
It appears you have request_slowlog_timeout enabled. This normally takes any request longer than N seconds, logs that it is taking a long time, then logs a stack trace of the Script so can see what it is doing that is taking so long. In your case, the stack trace (to determine what the script was doing) is failing. If you're running out of processes, it's because either:after php-fpm stops the process to trace it, the process fails to Resume because of the error tracing itthe process is resuming and continues to run forever. My first guess would is to disable request_slowlog_timeout. Since it ' s not working right, it is doing more harm than good. If this doesn ' t fix the issue of running out of processes, then set the php.ini max_execution_time to something that'll Kill the script for sure
4, if reported 504 error
Fastcgi_connect_timeout 180;fastcgi_read_timeout 600;fastcgi_send_timeout 600;
See article below
Http://www.cnblogs.com/argb/p/3604340.html
Http://yangjunwei.com/a/725.html
Http://www.pooy.net/nginx-open-php-fpm-error-log.html
Http://serverfault.com/questions/406532/i-o-error-with-php5-fpm-ptracepeekdata-failed
http://ju.outofmemory.cn/entry/102988
502 Errors in Nginx