nginx php 日誌問題

2021-09-03 02:34:05 字數 1842 閱讀 9342

nginx是乙個web伺服器,因此nginx的access日誌只有對訪問頁面的記錄,不會有php 的 error log資訊。

nginx把對php的請求發給php-fpm fastcgi程序來處理,預設的php-fpm只會輸出php-fpm的錯誤資訊,在php-fpm的errors log裡也看不到php的errorlog

原因是php-fpm的配置檔案php-fpm.conf中預設是關閉worker程序的錯誤輸出,直接把他們重定向到/dev/null,所以我們在nginx的error log 和php-fpm的errorlog都看不到php的錯誤日誌。

除錯起來就很痛苦了。解決nginx下php-fpm不記錄php錯誤日誌的辦法:

1.修改php-fpm.conf中配置 沒有則增加

catch_workers_output = yes //此行要寫到使用者配置裡面

error_log = log/error_log

2.修改php.ini中配置,沒有則增加

log_errors = on

error_log = 「/usr/local/lnmp/php/var/log/error_log」

error_reporting=e_all&~e_notice

3.重啟php-fpm,

當php執行錯誤時就能看到錯誤日誌在"/usr/local/lnmp/php/var/log/error_log"中了 //經wing測試,日誌雖然生效了,但是還是記錄在php-fpm的錯誤日誌裡

請注意:

php-fpm.conf 中的php_admin_value[error_log] 引數 會覆蓋php.ini中的 error_log 引數

所以確保你在phpinfo()中看到的最終error_log檔案具有可寫許可權並且沒有設定php_admin_value[error_log] 引數,否則錯誤日誌會輸出到php-fpm的錯誤日誌裡。

error_log /home/wwwlogs/nginx_error.log /home/wwwlogs/nginx_error.log //此行是phpinfo()裡的資訊

2.找不到php.ini位置,使用php的phpinfo()結果檢視

3.如何修改php錯誤日誌不輸出到頁面或螢幕上

修改php.ini

display_errors = off //不顯示錯誤資訊(不輸出到頁面或螢幕上)

log_errors = on //記錄錯誤資訊(儲存到日誌檔案中)

error_reporting = e_all //捕獲所有錯誤資訊

error_log = //設定日誌檔名

程式中修改以上配置

ini_set(「display_errors」,0)

ini_set(「error_reporting」,e_all); //這個值好像是個php的常量

ini_set(「error_log」,"《日誌檔名》")

ini_set(「log_errors」,1);

4.如何將php的錯誤日誌輸出到nginx的錯誤日誌裡

在php 5.3.8及之前的版本中,通過fastcgi執行的php,在使用者訪問時出現錯誤,會首先寫入到php的errorlog中

如果php的errorlog無法寫入,則會將錯誤內容返回給fastcgi介面,然後nginx在收到fastcgi的錯誤返回後記錄到了nginx的errorlog中

在php 5.3.9及之後的版本中,出現錯誤後php只嘗試寫入php的errorlog中,如果失敗則不會再返回到fastcgi了,錯誤日誌會輸出到php-fpm的錯誤日誌裡。

所以如果想把php錯誤日誌輸出到nginx錯誤日誌,需要使用php5.3.8之前的版本,並且配置檔案中php的error_log對於php worker程序不可寫

Nginx php 編譯安裝

步驟一 yum y install gcc gcc c autoconf libjpeg libjpeg devel libpng libpng devel freetype freetype devel libxml2 libxml2 devel zlib zlib devel glibc gli...

簡單執行Nginx PHP

0.nginx 安裝 zlib tar zxvf zlib 1.2.7.tar.gz cd zlib 1.2.7 configure make make install pcre tar zxvf pcre 8.32.tar.gz cd pcre 8.32 configure make make i...

Nginx PHP 執行原理

nginx engine x 是乙個高效能的http和反向 伺服器,也是乙個imap pop3 smtp伺服器。nginx不只有處理http請求的功能,還能做反向 nginx通過反向 功能將動態請求轉向後端php fpm。下面我們來配置乙個全新的nginx php fpm 進入nginx目錄下,編輯...