Nginx 使用 logrotate 进行日志滚动

100人浏览   2025-03-12 00:39:31

Nginx 日志滚动(官方)

向 Nginx 主进程发送 USR1 信号。

USR1 信号量被 Nginx 自定义了,为重新打开日志;当 kill 命令发送 USR1时,nginx 会重新打开日志文件,并重新创建进程。

# nginx 官方提供的日志滚动方式
$ mv access.log access.log.0
$ kill -USR1 `cat master.nginx.pid`
$ sleep 1
$ gzip access.log.0    # do something with access.log.0

logrotate 管理 Nginx 日志

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

logrotate 是一个日志文件管理工具。用于分割日志,删除旧的日志,并创建新的日志文件,起到日志滚动的作用。

logrotate 是基于 linux 的 CRON 来运行的,其脚本是 /etc/cron.daily/logrotate

安装 logrotate

Linux 一般会默认安装logrotate,它默认的配置文件在:

# 配置文件
$ cat /etc/logrotate.conf | grep -v '^#' | grep -v '^$'
weekly
rotate 4
create
dateext
include /etc/logrotate.d
/var/log/wtmp {
    monthly
    create 0664 root utmp
    minsize 1M
    rotate 1
}
/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}
# logrotate 配置文件目录,在 /etc/logrotate.conf 中会引用该目录下的所有文件
$ ls -lt /etc/logrotate.d/

安装 logrotate:

$ yum install logrotate

配置 logrotate

# nginx logratate 配置文件
$ vi /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
    # 指定转储周期为每天
    daily
    # 使用当期日期作为命名格式
    dateext
    # 如果日志丢失,不报错继续滚动下一个日志
    missingok
    # 保留 31 个备份
    rotate 31
    # 不压缩
    nocompress
    # 整个日志组运行一次的脚本
    sharedscripts
    # 转储以后需要执行的命令
    postrotate
        # 重新打开日志文件
        [ ! -f /usr/local/nginx/nginx.pid ] || kill -USR1 `cat /usr/local/nginx/nginx.pid`
    endscript
}

配置文件参数说明:



logrotate 命令参数

logrotate [OPTION...] <configfile>
-d, --debug :debug模式,测试配置文件是否有错误。
-f, --force :强制转储文件。
-m, --mail=command :压缩日志后,发送日志到指定邮箱。
-s, --state=statefile :使用指定的状态文件。
-v, --verbose :显示转储过程。

手动执行 logrotate

# '-d' 调试模式(不切分日志文件),并输出详细处理过程日志
$ logrotate -d -f /etc/logrotate.d/nginx

# '-f' 强制切分日志,'-v' 输出详细信息
$ logrotate -vf /etc/logrotate.d/nginx
reading config file nginx
Allocating hash table for state file, size 15360 B
Handling 1 logs
rotating pattern: /usr/local/nginx/logs/*.log  forced from command line (100 rotations)
empty log files are rotated, old logs are removed
considering log /usr/local/nginx/logs/access.log
  log needs rotating
considering log /usr/local/nginx/logs/error.log
  log needs rotating
rotating log /usr/local/nginx/logs/access.log, log->rotateCount is 100
dateext suffix '-20201121'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
rotating log /usr/local/nginx/logs/error.log, log->rotateCount is 100
dateext suffix '-20201121'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /usr/local/nginx/logs/access.log to /usr/local/nginx/logs/access.log-20201121
renaming /usr/local/nginx/logs/error.log to /usr/local/nginx/logs/error.log-20201121
running postrotate script

# 切分后的日志文件
$ ls -lt /usr/local/nginx/logs
总用量 0
-rw-r--r-- 1 nginx root 0 11月 21 18:58 access.log
-rw-r--r-- 1 nginx root 0 11月 21 18:57 access.log-20201121
-rw-r--r-- 1 nginx root 0 11月 21 18:58 error.log
-rw-r--r-- 1 nginx root 0 11月 21 18:57 error.log-20201121
-rw-r--r-- 1 nginx root 0 11月 21 18:58 images.log
-rw-r--r-- 1 nginx root 0 11月 21 18:57 images.log-20201121

相关推荐