zoukankan      html  css  js  c++  java
  • nginx中的502错误

    遇到这种情况,首先看一下慢日志

    [17-Aug-2015 13:13:43] WARNING: [pool www] child 27780, script '/data/s.com/index.php' (request: "GET /index.php") executing too slow (5.226952 sec), logging
    [17-Aug-2015 13:13:43] NOTICE: child 27780 stopped for tracing
    [17-Aug-2015 13:13:43] NOTICE: about to trace 27780
    [17-Aug-2015 13:13:43] ERROR: failed to ptrace(PEEKDATA) pid 27780: Input/output error (5)
    [17-Aug-2015 13:13:43] NOTICE: finished trace of 27780
    [17-Aug-2015 13:14:16] WARNING: [pool www] child 28262, script '/data/s.com/index.php' (request: "GET /index.php/Admin/Login") executing too slow (5.205117 sec), logging
    [17-Aug-2015 13:14:16] NOTICE: child 28262 stopped for tracing
    [17-Aug-2015 13:14:16] NOTICE: about to trace 28262
    [17-Aug-2015 13:14:16] ERROR: failed to ptrace(PEEKDATA) pid 28262: Input/output error (5)
    [17-Aug-2015 13:14:16] NOTICE: finished trace of 28262
    [17-Aug-2015 13:15:16] WARNING: [pool www] child 27802, script '/data/s.com/ss/connect.php' (request: "GET /ss/connect.php") executing too slow (5.579121 sec), logging
    [17-Aug-2015 13:15:16] NOTICE: child 27802 stopped for tracing
    [17-Aug-2015 13:15:16] NOTICE: about to trace 27802
    [17-Aug-2015 13:15:16] ERROR: failed to ptrace(PEEKDATA) pid 27802: Input/output error (5)
    [17-Aug-2015 13:15:16] NOTICE: finished trace of 27802
    [17-Aug-2015 13:16:11] WARNING: [pool www] child 27803, script '/data/s.com/ss/url_api.inc.php' (request: "POST /ss/url_api.inc.php") executing too slow (5.130357 sec), logging
    [17-Aug-2015 13:16:11] NOTICE: child 27803 stopped for tracing
    [17-Aug-2015 13:16:11] NOTICE: about to trace 27803
    [17-Aug-2015 13:16:11] NOTICE: finished trace of 27803
    

    究竟为何会有这么多错误,部分PHP程序的执行时间超过了Nginx的等待时间


    那么我们先看一下php-fpm的配置文件php-fpm.conf

    include=/etc/php-fpm.d/*.conf
    ;;;;;;;;;;;;;;;;;;
    ; Global Options ;
    ;;;;;;;;;;;;;;;;;;
    
    [global]
    pid = /var/run/php-fpm/php-fpm.pid
    ;pid设置,默认在安装目录中的var/run/php-fpm.pid,建议开启
    
    error_log = /data/logs/error/php-fpm.error.log
    ;错误日志,默认在安装目录中的/data/logs/error/php-fpm.error.log
    
    log_level = error
    ;错误级别. 可用级别为: alert(必须立即处理), error(错误情况),
    ;warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.
    
    emergency_restart_threshold = 10
    emergency_restart_interval = 1m
    ;表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi进程数如果超过
    ;emergency_restart_threshold个,php-fpm就会优雅重启。这两个选项一般保持默认值。
    
    process_control_timeout = 10s
    ;设置子进程接受主进程复用信号的超时时间. 可用单位: s(秒), m(分), h(小时), 或者 d(天) 默认单位: s(秒). 默认值: 0
    
    daemonize = yes
    ;后台执行fpm,默认值为yes,如果为了调试可以改为no。在FPM中,可以使用不同的设置来运行多个进程池。 这些设置可以针对每个进程池单独设置
    
    ;listen.backlog = -1
    ;backlog数,-1表示无限制,由操作系统决定,此行注释掉就行
     
    ;listen.allowed_clients = 127.0.0.1
    ;允许访问FastCGI进程的IP,设置any为不限制IP,如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。
    ;默认值是any。每个地址是用逗号分隔. 如果没有设置或者为空,则允许任何服务器请求连接
    ;;;;;;;;;;;;;;;;;;;;
    ; Pool Definitions ;
    ;;;;;;;;;;;;;;;;;;;;
    
    ; See /etc/php-fpm.d/*.conf
    

     看一下具体pool的配置文件

    ; Start a new pool named 'www'
    [www]
    
    listen = /dev/shm/php-fpm.socket
    ;listen = 127.0.0.1:9000
    ;php-fpm 监听端口,即nginx中php处理的地址,一般默认值即可
    ;可用格式为: 'ip:port', 'port', '/dev/shm/php-fpm.socket',每个进程池都需要设置
    ;此处是放在内存中,更高效
    
    listen.owner = www
    listen.group = webgrp
    listen.mode = 0666
    ;unix socket设置选项,如果使用tcp方式访问,这里注释即可
    
    user = www
    group = webgrp
    ;启动进程的帐户和组
    
    pm = static
    ;对于专用服务器,pm可以设置为static
    ;选项有static和dynamic
    ;如果选择static,则由pm.max_children指定固定的子进程数
    ;如果选择dynamic,则由其余三个参数决定
    
    pm.max_children = 16
    ;子进程最大数
    
    pm.start_servers = 8
    ;启动时的进程数
    
    pm.min_spare_servers = 5
    ;保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程
    
    pm.max_spare_servers = 25
    ;保证空闲进程数最大值,如果空闲进程大于此值,此进行清理
    
    pm.max_requests = 10240
    ;设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的
    ;如果设置为 0 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0
    
    ;pm.status_path = /status
    ;php-fpm状态页面的网址. 如果没有设置, 则无法访问状态页面. 默认值: none. munin监控会使用到
    
    ;ping.path = /ping
    ;php-fpm监控页面的ping网址. 如果没有设置, 则无法访问ping页面, 该页面用于外部检测php-fpm是否存活并且可以响应请求,请注意必须以斜线开头 (/)。
    
    ;ping.response = pong
    ;用于定义ping请求的返回相应, 返回为 HTTP 200 的 text/plain 格式文本. 默认值: pong
    
    request_terminate_timeout = 600s
    ;设置单个请求的超时中止时,. 该选项可能会对php.ini设置中的'max_execution_time'因为某些特殊原因没有中止运行的脚本有用
    ;设置为0表示 Off,当经常出现502错误时可以尝试更改此选项
    
    request_slowlog_timeout = 5s
    ;当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中. 设置为 0 表示 Off
    slowlog = /data/logs/error/www-slow.log
    ;慢请求的记录日志,配合request_slowlog_timeout使用
    
    rlimit_files = 65535 
    ;设置文件打开描述符的rlimit限制. 默认值: 系统定义值默认可打开句柄是1024,可使用 ulimit -n查看,ulimit -n 2048修改
    rlimit_core = 0
    ;设置核心rlimit最大限制值. 可用值: 'unlimited' 、0或者正整数. 默认值: 系统定义值.
    
    catch_workers_output = yes
    ;重定向运行过程中的stdout和stderr到主要的错误日志文件中. 如果没有设置, stdout 和 stderr 将会根据FastCGI的规则被重定向到 /dev/null . 默认值: 空.
    ;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 www@my.domain.com
    ;php_flag[display_errors] = on
    php_admin_value[error_log] = /var/log/php-fpm/www-error.log
    php_admin_flag[log_errors] = on
    ;php_admin_value[memory_limit] = 32M
    

    那么我们看一下502错误的罪魁祸首

    1、php执行时间长,导致进程不够用

    1)、request_terminate_timeout的值如果设置为0或者过长的时间,可能会引起file_get_contents,fsockopen的资源问题,如果file_get_contents请求的远程资源如果反应过慢,file_get_contents就会一直卡在那里不会超时

    php.ini 里面max_execution_time 可以设置 PHP 脚本的最大执行时间,但是,在php-cgi(php-fpm) 中,该参数不会起效。真正能够控制 PHP 脚本最大执行时间的是 php-fpm.conf 配置文件中的request_terminate_timeout参数

    参数优先级 nginx >  php-fpm > php

    request_terminate_timeout 默认值为 0 秒,也就是说,PHP 脚本会一直执行下去。当所有的 php-fpm 进程都卡在 file_get_contents() 函数时,这台 Nginx+PHP 的 WebServer 已经无法再处理新的 PHP 请求了,Nginx 将给用户返回502 Bad Gateway。修改该参数,设置一个 PHP 脚本最大执行时间是必要的;但是,治标不治本,例如改成 30s,如果发生 file_get_contents() 获取网页内容较慢的情况,这就意味着 150 个 php-fpm 进程,每秒钟只能处理 5 个请求,WebServer 同样很难避免”502 Bad Gateway”。

    解决办法是增大php-fpm数量,增大request_terminate_timeout,设置为300s或者一个合理的值,或者给file_get_contents加一个超时参数

    2、php-fpm进程死掉,max_requests参数配置不当,可能会引起间歇性502错误

    设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的,如果设置为 ‘0’ 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0,这段配置的意思是,当一个 PHP-CGI 进程处理的请求数累积到 一定数量个后,自动重启该进程

    但是为什么要重启进程呢?

    一 般在项目中,我们多多少少都会用到一些 PHP 的第三方库,这些第三方库经常存在内存泄漏问题,如果不定期重启 PHP-CGI 进程,势必造成内存使用量不断增长。因此 PHP-FPM 作为 PHP-CGI 的管理器,提供了这么一项监控功能,对请求达到指定次数的 PHP-CGI 进程进行重启,保证内存使用量不增长

    正是因为这个机制,在高并发的站点中,经常导致 502 错误,我猜测原因是 PHP-FPM 对从 NGINX 过来的请求队列没处理好

    解决方法是,把这个值尽量设置大些,尽可能减少 PHP-CGI 重新 SPAWN 的次数,同时也能提高总体性能。在我们自己实际的生产环境中发现,内存泄漏并不明显,因此我们将这个值设置得非常大(204800)

    这套机制目的只为保证 PHP-CGI 不过分地占用内存,为何不通过检测内存的方式来处理呢?通过设置进程的峰值内在占用量来重启 PHP-fpm进程,会是更好的一个解决方案

    3、引起的错误

    NOTICE] child 19214 stopped for 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: Input/output error (5) 
    [NOTICE] finished trace of 19208 
    [WARNING] [pool www] child 19218, script 'blahblah.php' executing too slow (30.035029 sec), logging
    

     解决方法

    It appears you have request_slowlog_timeout enabled. This normally takes any request longer than N seconds, logs that it was taking a long time, then logs a stack trace of the script so you can see what it was doing that was taking so long.
    
    In your case, the stack trace (to determine what the script is doing) is failing. If you're running out of processes, it is because either:
    
    After php-fpm stops the process to trace it, the process fails to resume because of the error tracing it
    The process is resuming but continues to run forever.
    
    My first guess would be to disable request_slowlog_timeout. Since it's not working right, it may be 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 will kill the script for sure
    

    4、504错误,可以进行如下修改

    fastcgi_connect_timeout      180;
    fastcgi_read_timeout            600;
    fastcgi_send_timeout            600;
    

    5、总结参数可以如下设置

    #php.ini
    max_execution_time = 300
    #php-fpm.conf
    request_terminate_timeout = 300
    #nginx
    location ~ .php$ {
    	include /etc/nginx/fastcgi_params;
    	fastcgi_pass  unix:/var/run/php5-fpm.sock;
    	fastcgi_read_timeout 300; 
    }
    

    可以调整以上参数为500

    php-fpm 进程数不够用、php执行时间长、或者是php-fpm进程死掉,都会出现502错误

    参见文章如下

    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

  • 相关阅读:
    Starting Tomcat v7.0 Server at localhost (2)hasencountered a problemServer Tomcat v7.0 Server at localhost (2)failed tostart
    如何获取系统当前时间
    解决TextEncoder 和 TextDecoder在IE下不兼容 vue 用iconv-lite插件代替 解决中文乱码问题
    vue 读取本地TXT GBK编码文件
    HTML常用标签和属性大全
    echarts中的个性化设计
    MySQL常用优化指南和思路
    微服务框架 Service Mesh
    spring boot actuator监控
    关于Swagger @ApiModel 返回内容注释不显示问题
  • 原文地址:https://www.cnblogs.com/chenpingzhao/p/4736702.html
Copyright © 2011-2022 走看看