解決:LNMP架構下nginx的error_log日誌過大問題

php-fpm.conf 中的php

catch_workers_output = yes

設置能夠捕獲 php 程序的錯誤輸出
nginx

我在部署LNMP架構時都是設置此項爲yes,便於日誌分析架構

若是此項設置爲 yes ,同時ide

error_reporting = E_ALL

的話,那麼nginx的錯誤日誌會記錄大量記錄,內容以下:php-fpm

2013/08/09 14:38:14 [error] 6687#0: *33365028 FastCGI sent in stderr: "PHP Notice:  Undefined variable: xxxx in /data/www/www/controllers/main.php on line 50
PHP Notice:  Undefined variable: xxxx in /data/www/www/controllers/main.php on line 55
PHP Notice:  Undefined index:  sid in /data/www/www/controllers/main.php on line 88


固然了,這是在php代碼寫的不夠嚴謹的狀況下,如:日誌

一、直接使用開發

$sid = $_GET['sid'];

而未作 isset 判斷
部署

二、使用未定義變量 it

print $username;

像這樣的 E_NOTICE 類錯誤都會記錄到nginx錯誤日誌,日誌大小增加迅速ast


比較好的解決方法是開發嚴謹的程序

其次就是調整錯誤報告的級別,如去掉 E_NOTICE 類的錯誤

能夠在 php.ini 中設置,如:

error_reporting = E_ALL & ~E_NOTICE

便可,

配置文件中不支持

error_reporting = E_ALL ^ E_NOTICE

這種寫法,可是在php程序中能夠這麼使用

其實即便在配置文件中設置了

error_reporting = E_ALL & ~E_NOTICE

若是同時在程序中設置,如:

error_reporting(E_ALL);

那麼會以在程序中設置的爲準,因此日誌仍是會記錄 Notice 錯誤的

相關文章
相關標籤/搜索