Nginx 日志轮转似乎无法正常工作

Nginx 日志轮转似乎无法正常工作

我发现我的分区安装/var已满。在跟踪磁盘使用情况时,我发现我的 Nginx 日志文件很大,而且它似乎没有写入正确的文件。

ls -ltr/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脚本中。

如果成功,我会报告。

答案1

第一天

/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.2gzip 压缩。我猜测该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

相关内容