Nginx/Openresty 不断创建一个空的日志文件,并在前一个文件中写入内容,导致日志永远不会轮换

Nginx/Openresty 不断创建一个空的日志文件,并在前一个文件中写入内容,导致日志永远不会轮换

我的 Django 应用在 nginx/openresty 服务器上运行。运行该服务器的用户是,root:root但该应用通常归 拥有webapp:webapp。这意味着日志由 创造和写入root

问题在于日志没有轮换,因为正在创建一个空日志,并且服务器没有写入日志,而是继续写入前一个日志:

webapp@webapphost:~$ ls -lh logs/openresty/
total 6.7G
-rw-rw-rw- 1 root root    0 Jan 30 13:24 access.log
-rw-rw-rw- 1 root root 6.4G Mar 17 11:34 access.log.1
-rw-rw-rw- 1 root root 135M Dec 21 20:29 access.log.12.gz
-rw-rw-rw- 1 root root 146M Nov 16 12:39 access.log.15.gz
-rw-rw-rw- 1 root root 8.4K Jan 29 17:34 access.log.2.gz
-rw-rw-rw- 1 root root 3.0K Jan 19 10:24 access.log.4.gz
-rw-rw-rw- 1 root root 216K Jan 18 10:54 access.log.6.gz
-rw-rw-rw- 1 root root  58K Jan 12 10:58 access.log.8.gz
-rw-rw-rw- 1 root root 1.5M Mar 17 06:16 error.log
-rw-rw-rw- 1 root root 7.4K Jan 29 16:38 error.log.1.gz
-rw-rw-rw- 1 root root 1.2K Jan 19 10:22 error.log.2.gz
-rw-rw-rw- 1 root root 4.5K Jan 18 08:31 error.log.3.gz
-rw-rw-rw- 1 root root 1.6K Jan 12 08:55 error.log.4.gz
-rw-rw-rw- 1 root root  12K Jul 13  2020 error.log.7.gz

webapp@webapphost:~$ ls -lh /var/log/openresty/
total 352M
-rw-rw-rw- 1 root root    0 Feb 16 15:24 access.log
-rw-rw-rw- 1 root root 277M Mar 17 11:37 access.log.1
-rw-rw-rw- 1 root root  13M Oct 29 19:24 access.log.11.gz
-rw-rw-rw- 1 root root  38M Oct  6 11:19 access.log.12.gz
-rw-rw-rw- 1 root root 5.1M Jul 23  2020 access.log.13.gz
-rw-rw-rw- 1 root root 8.0K Jul 13  2020 access.log.14.gz
-rw-rw-rw- 1 root root 8.2M Jul 13  2020 access.log.15.gz
-rw-rw-rw- 1 root root 1004 Jan 29 17:34 access.log.2.gz
-rw-rw-rw- 1 root root 6.4M Jan 29 17:34 access.log.3.gz
-rw-rw-rw- 1 root root 599K Jan 19 10:24 access.log.4.gz
-rw-rw-rw- 1 root root  11K Jan 18 10:54 access.log.5.gz
-rw-rw-rw- 1 root root 3.7M Jan 18 10:19 access.log.6.gz
-rw-rw-rw- 1 root root 514K Jan 12 10:58 access.log.7.gz
-rw-rw-rw- 1 root root  50K Mar 17 06:16 error.log
-rw-rw-rw- 1 root root 2.7K Jan 29 10:24 error.log.1.gz
-rw-rw-rw- 1 root root 1.6K Jan 17 10:58 error.log.2.gz

如您所见,*.log* files as empty, but*.log.1` 不断无限增长。

我的 logrotate 配置如下:

webapp@webapphost:~$ cat /etc/logrotate.d/openresty
/home/webapp/logs/openresty/*.log /var/log/openresty/*.log {
        su root root
        size 100M
        missingok
        rotate 14
        compress
        nodelaycompress
        notifempty
        create 0666 root root
        sharedscripts
        prerotate
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                run-parts /etc/logrotate.d/httpd-prerotate; \
        fi \
        endscript
        postrotate
                invoke-rc.d openresty rotate >/dev/null 2>&1
                service openresty reload >/dev/null 2>&1
        endscript
}

logrotate因为“日志不需要旋转”而没有发生任何事情时,因为它是空的:

webapp@webapphost:~$ sudo logrotate -d /etc/logrotate.d/openresty
reading config file /etc/logrotate.d/openresty

Handling 1 logs

rotating pattern: /home/webapp/logs/openresty/*.log /var/log/openresty/*.log  104857600 bytes (14 rotations)
empty log files are not rotated, old logs are removed
considering log /home/webapp/logs/openresty/access.log
  log does not need rotating
considering log /home/webapp/logs/openresty/error.log
  log does not need rotating
considering log /var/log/openresty/access.log
  log does not need rotating
considering log /var/log/openresty/error.log
  log does not need rotating
not running prerotate script, since no logs will be rotated
not running postrotate script, since no logs were rotated

强制旋转也-f不起作用。

但是如果我执行以下操作,强制旋转就会起作用:

invoke-rc.d openresty rotate
sudo systemctl reload openresty
sudo logrotate -f /etc/logrotate.d/openresty

相关内容