Nginx

Nginx 日誌輪換似乎無法正常工作

  • February 4, 2016

我發現我的分區/var已裝滿。在跟踪磁碟使用情況時,我發現我的 Nginx 日誌文件很大,而且似乎沒有寫入正確的文件。

ls -ltrin/var/log/nginx給了我這個:

total 17170060
-rw-r----- 1 nobody adm   112315467 Apr 28  2015 access.log.14.gz
-rw-r----- 1 nobody adm    70554720 Apr 29  2015 access.log.13.gz
-rw-r----- 1 nobody adm    68145066 Apr 30  2015 access.log.12.gz
-rw-r----- 1 nobody adm    77951766 Dec  7 06:25 access.log.10.gz
-rw-r----- 1 nobody adm    81764378 Dec  8 06:25 access.log.9.gz
-rw-r----- 1 nobody adm    75397419 Dec  9 06:25 access.log.8.gz
-rw-r----- 1 nobody adm    68738862 Dec 10 06:25 access.log.7.gz
-rw-r----- 1 nobody adm    73802613 Dec 11 06:25 access.log.6.gz
-rw-r----- 1 nobody adm    66144521 Dec 12 06:25 access.log.5.gz
-rw-r----- 1 nobody adm    65385900 Dec 13 06:25 access.log.4.gz
-rw-r----- 1 nobody adm    68117105 Dec 14 06:25 access.log.3.gz
-rw-r----- 1 nobody adm    69441057 Dec 15 06:25 access.log.2.gz
-rw-r----- 1 nobody adm           0 Jan 20 06:25 access.log
-rw-r----- 1 nobody adm     7762196 Feb  1 09:45 error.log
-rw-r----- 1 nobody adm 16676558550 Feb  1 10:24 access.log.1

如您所見,access.log.1是正在寫入的文件。

這是我的/etc/logrotate.d/nginx腳本:

/var/log/nginx/*.log {
       daily
       size 1G
       missingok
       rotate 10
       compress
       delaycompress
       notifempty
       create 0640 nobody adm
       sharedscripts
       prerotate
               if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                       run-parts /etc/logrotate.d/httpd-prerotate; \
               fi \
       endscript
       postrotate
               [ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
       endscript
}

相關行來自/etc/nginx/nginx.conf

pid /var/run/nginx.pid;
...snip...
   access_log /var/log/nginx/access.log;
   error_log /var/log/nginx/error.log;

更新

當我今天早上檢查時,同樣的問題正在發生。我service nginx reload從命令行執行,它釋放了舊文件並開始寫入新文件。根據這篇文章和我的觀察,我已經替換了

[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid\`

service nginx reload >/dev/null 2>&1

/etc/logrotate.d/nginx腳本中。

如果這成功了,我會報告。

第一天

/etc/logrotate.d/nginx正如我在 OP 中提到的那樣對我的腳本進行更改後…

/var/log/nginx/*.log {
       daily
       size 1G
       missingok
       rotate 10
       compress
       delaycompress
       notifempty
       create 0640 nobody adm
       sharedscripts
       prerotate
               if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                       run-parts /etc/logrotate.d/httpd-prerotate; \
               fi \
       endscript
       postrotate
               service nginx reload >/dev/null 2>&1
       endscript
}

access.log正確重命名為access.log.1,發布,然後它開始寫入新的access.log. 但是,它沒有 gzip access.log.1

第二天

access.log.1文件成為access.log.2並被壓縮。我猜該delaycompress指令是讓日誌文件直到第二天才被壓縮的原因。ls -ltr給我這個輸出:

total 3344744
-rw-r----- 1 nobody adm   68738862 Dec 10 06:25 access.log.10.gz
-rw-r----- 1 nobody adm   73802613 Dec 11 06:25 access.log.9.gz
-rw-r----- 1 nobody adm   66144521 Dec 12 06:25 access.log.8.gz
-rw-r----- 1 nobody adm   65385900 Dec 13 06:25 access.log.7.gz
-rw-r----- 1 nobody adm   68117105 Dec 14 06:25 access.log.6.gz
-rw-r----- 1 nobody adm   69441057 Dec 15 06:25 access.log.5.gz
-rw-r----- 1 nobody adm  748290175 Feb  1 10:50 access.log.4.gz
-rw-r----- 1 nobody adm   58575628 Feb  2 08:32 access.log.3.gz
-rw-r----- 1 nobody adm   85585414 Feb  3 06:25 access.log.2.gz
-rw-r----- 1 nobody adm    8779427 Feb  4 02:39 error.log
-rw-r----- 1 nobody adm 2023509844 Feb  4 06:25 access.log.1
-rw-r----- 1 nobody adm   88608092 Feb  4 09:17 access.log

引用自:https://unix.stackexchange.com/questions/259091