nginx+fpm记录php的error log

41 篇文章 0 订阅

前言

将架构从apache+mod_php迁移到nginx+fpm上,会发现如果php脚本出错,nginx的eror_log里不会有记录,只在access_log里记录了500状态码。出现的原因:nginx将php的脚本执行通过fastcgi转到fpm上,所以出错的信息当然不会记录在nginx的error_log里了,但是对于测试环境下程序员的调试这是个悲剧啊,所以我们需要配置fpm的错误日志


php5-fpm.conf

php5-fpm的错误日志是在php5-fpm.conf全局配置中进行设置,查看一下php5-fpm.conf日志:

[plain] view plain copy
  1. ;;;;;;;;;;;;;;;;;;  
  2. ; Global Options ;  
  3. ;;;;;;;;;;;;;;;;;;  
  4.   
  5. [global]  
  6. ; Pid file  
  7. ; Note: the default prefix is /var  
  8. ; Default Value: none  
  9. ;pid = run/php-fpm.pid  
  10. pid = /var/run/php5-fpm.pid  
  11.   
  12. ; Error log file  
  13. ; If it's set to "syslog", log is sent to syslogd instead of being written  
  14. ; in a local file.  
  15. ; Note: the default prefix is /var  
  16. ; Default Value: log/php-fpm.log  
  17. ; error_log = log/php-fpm.log  
  18. #配置error_log,设置error_log的存放位置  
  19. error_log = /var/log/fpm/php5-fpm.error.log  
  20.   
  21. ; syslog_facility is used to specify what type of program is logging the  
  22. ; message. This lets syslogd specify that messages from different facilities  
  23. ; will be handled differently.  
  24. ; See syslog(3) for possible values (ex daemon equiv LOG_DAEMON)  
  25. ; Default Value: daemon  
  26. ;syslog.facility = daemon  
  27.   
  28. ; syslog_ident is prepended to every message. If you have multiple FPM  
  29. ; instances running on the same server, you can change the default value  
  30. ; which must suit common needs.  
  31. ; Default Value: php-fpm  
  32. ;syslog.ident = php-fpm  
  33.   
  34. ; Log level  
  35. ; Possible Values: alert, error, warning, notice, debug  
  36. ; Default Value: notice  
  37. #错误级别(生产环境下应该提高错误级别,推荐error)  
  38. log_level = notice  
  39.   
  40. #表示在emergency_restart_interval所设时间内出现SIGSEGV或者SIGBUS错误的php-cgi进程数如果超过emergency_restart_threshold个,php-fpm就会优雅重启  
  41. emergency_restart_threshold = 60  
  42. emergency_restart_interval = 60s  
  43.   
  44. ; Time limit for child processes to wait for a reaction on signals from master.  
  45. ; Available units: s(econds), m(inutes), h(ours), or d(ays)  
  46. ; Default Unit: seconds  
  47. ; Default Value: 0  
  48. ;process_control_timeout = 0  
  49.   
  50. ; The maximum number of processes FPM will fork. This has been design to control  
  51. ; the global number of processes when using dynamic PM within a lot of pools.  
  52. ; Use it with caution.  
  53. ; Note: A value of 0 indicates no limit  
  54. ; Default Value: 0  
  55. ; process.max = 128  
  56.   
  57. ; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.  
  58. ; Default Value: yes  
  59. #后台执行fpm,设置为yes  
  60. daemonize = yes  

pool.d目录下配置文件(例如:www.conf)

(1)保证配置文件中一个参数的开启,参数定义看英文注释很清楚:

[plain] view plain copy
  1. ; Redirect worker stdout and stderr into main error log. If not set, stdout and  
  2. ; stderr will be redirected to /dev/null according to FastCGI specs.  
  3. ; Note: on highloaded environement, this can cause some delay in the page  
  4. ; process time (several ms).  
  5. ; Default Value: no  
  6. catch_workers_output = yes  



(2)同时可以开启access_log和slow_log

[plain] view plain copy
  1. <span style="color:#ff0000;">access.log = /var/log/fpm/php5-fpm.access.log</span>  
  2.   
  3. ; The access log format.  
  4. ; The following syntax is allowed  
  5. ;  %%: the '%' character  
  6. ;  %C: %CPU used by the request  
  7. ;      it can accept the following format:  
  8. ;      - %{user}C for user CPU only  
  9. ;      - %{system}C for system CPU only  
  10. ;      - %{total}C  for user + system CPU (default)  
  11. ;  %d: time taken to serve the request  
  12. ;      it can accept the following format:  
  13. ;      - %{seconds}d (default)  
  14. ;      - %{miliseconds}d  
  15. ;      - %{mili}d  
  16. ;      - %{microseconds}d  
  17. ;      - %{micro}d  
  18. ;  %e: an environment variable (same as $_ENV or $_SERVER)  
  19. ;      it must be associated with embraces to specify the name of the env  
  20. ;      variable. Some exemples:  
  21. ;      - server specifics like: %{REQUEST_METHOD}e or %{SERVER_PROTOCOL}e  
  22. ;      - HTTP headers like: %{HTTP_HOST}e or %{HTTP_USER_AGENT}e  
  23. ;  %f: script filename  
  24. ;  %l: content-length of the request (for POST request only)  
  25. ;  %m: request method  
  26. ;  %M: peak of memory allocated by PHP  
  27. ;      it can accept the following format:  
  28. ;      - %{bytes}M (default)  
  29. ;      - %{kilobytes}M  
  30. ;      - %{kilo}M  
  31. ;      - %{megabytes}M  
  32. ;      - %{mega}M  
  33. ;  %n: pool name  
  34. ;  %o: ouput header  
  35. ;      it must be associated with embraces to specify the name of the header:  
  36. ;      - %{Content-Type}o  
  37. ;      - %{X-Powered-By}o  
  38. ;      - %{Transfert-Encoding}o  
  39. ;      - ....  
  40. ;  %p: PID of the child that serviced the request  
  41. ;  %P: PID of the parent of the child that serviced the request  
  42. ;  %q: the query string   
  43. ;  %Q: the '?' character if query string exists  
  44. ;  %r: the request URI (without the query string, see %q and %Q)  
  45. ;  %R: remote IP address  
  46. ;  %s: status (response code)  
  47. ;  %t: server time the request was received  
  48. ;      it can accept a strftime(3) format:  
  49. ;      %d/%b/%Y:%H:%M:%S %z (default)  
  50. ;  %T: time the log has been written (the request has finished)  
  51. ;      it can accept a strftime(3) format:  
  52. ;      %d/%b/%Y:%H:%M:%S %z (default)  
  53. ;  %u: remote user  
  54. ;  
  55. ; Default: "%R - %u %t \"%m %r\" %s"  
  56. ;access.format = %R - %u %t "%m %r%Q%q" %s %f %{mili}d %{kilo}M %C%%  
  57.    
  58. ; The log file for slow requests  
  59. ; Default Value: not set  
  60. ; Note: slowlog is mandatory if request_slowlog_timeout is set  
  61. slowlog = /var/log/fpm/php5-fpm.log.slow  
  62.    
  63. ; The timeout for serving a single request after which a PHP backtrace will be  
  64. ; dumped to the 'slowlog' file. A value of '0s' means 'off'.  
  65. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)  
  66. ; Default Value: 0  
  67. request_slowlog_timeout = 3  

fpm日志按天分割脚本

  1. #!/bin/bash -   
  2.   
  3. #1.fpm日志存放路径  
  4. php5_fpm_path="/var/log/php5-fpm"  
  5.   
  6. path_array=($php5_fpm_path)  
  7.   
  8.   
  9. #2.日志标识前缀数组  
  10. icontact_fpm_sign="test"  
  11. test_fpm_sign="php5-fpm"  
  12.   
  13. prefix_array=($icontact_fpm_sign $test_fpm_sign)  
  14.   
  15.   
  16. #3.日志success or slow 标识后缀数组  
  17. bool_array=("access" "slow" "error")  
  18.   
  19.   
  20. #4.nginx日志切割备份后缀  
  21. postfix=`date +%Y%m%d`".log"  
  22.   
  23.   
  24. #5.备份当前的日志文件,重命名改为日期后缀  
  25. for path in ${path_array[*]}  
  26. do  
  27.     for prefix in ${prefix_array[*]}  
  28.     do  
  29.         for bool in ${bool_array[*]}  
  30.         do  
  31.             file=$path/$prefix.$bool.log  
  32.             backfile=$path/$prefix.$bool.$postfix  
  33.             if [ -e $file ]  
  34.             then  
  35.                 mv $file    $backfile  
  36.             fi  
  37.         done  
  38.     done  
  39. done  
  40.   
  41.   
  42. #6.查找nginx进程号,让其产生新的日志文件  
  43. fpm_pid=`ps -aux |grep -E 'php-fpm: master process'|grep -v 'grep'|awk '{print $2}'`  
  44. #USR1:Reopen log files,刷新nginx日志文件  
  45. kill -USR1 $fpm_pid  


经验和bug分享

问题

为了减少线上服务器的错误信息输出,我将php-fpm.conf的Log_level = error,但是这样配置后我在php5-fpm的错误日志里再也看不到错误信息了。


原因定位

可以看一下我将Log_level = notice后的错误日志信息截图:




可以发现fpm将php的不管是warning或者是error的错误全部定位成了notice级别,所以线上想看fpm的错误日志只能将错误级别定位成notice了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值