首页 > Linux&Fedora专区 > nginx日志分割之logrotate
2015
04-10

nginx日志分割之logrotate

一般的情况下,我们对nginx的分割是通过shell+crontab来实现的,如:

#!/bin/bash
# This script run at 00:00

log_dir="/usr/local/nginx/logs"

date=`date -d "yesterday" +"%Y%m%d"`

/bin/mv ${log_dir}/access.log ${log_dir}/access${date}.log
/bin/mv ${log_dir}/$i/error.log ${log_dir}/error${date}.log

kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`

然后在crontab里面每天0点0分执行这个脚本:

[root@APP ~]#crontab -l
0 0 * * * /bin/sh /data/jb/nginx_log.sh

其实我们可以使用系统自带的logrotate来实现nginx的日志分割,感觉上更高级哦。

[root@xmxyk wwwlogs]#cat /etc/logrotate.d/nginx 
/home/wwwlogs/*.log  {
    daily
    rotate 30
    missingok
    minsize 10M
    notifempty
    compress
   copytruncate
    sharedscripts
    prerotate
       /usr/bin/chattr -a /home/wwwlogs/*.log 
    endscript
    sharedscripts
    postrotate
        kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` > /dev/null 2>/dev/null || true
        /usr/bin/chattr -a /home/wwwlogs/*.log
    endscript
}

参数说明

daily 指定转储周期为每天
rotate 30 指定日志文件删除之前转储的次数,0 指没有备份,30 指保留30个备份
missingok  如果日志不存在则忽略该警告信息
notifempty 如果是空文件的话,不转储
minsize    最小的分割大小,文件容量一定要超过 10M 后才进行 rotate
compress 通过gzip 压缩转储以后的日志
prerotate:在启动 logrotate 之前进行的命令,例如修改登录文件的属性等动作,即我们先对/home/wwwlogs/*.log进行解锁。

postrotate:在做完 logrotate 之后启动的命令,例如重新启动 (kill -HUP) 某个服务

sharedscripts表示所有日志共享这个脚本,就是说他俩轮转完成后只执行一次这个脚本,默认情况下是每个脚本轮转完成就执行一次。

copytruncate,拷贝后截断。 把当前log拷贝后截断。可以理解为把内容拷贝走作为备份,然后清空当前文件。但是这有一个问题就是拷贝和截断之间会有时间差,存在丢数据的可能。 

然后我们可以使用logrotate -d来分析一下是否需要分割日志(只是测试 ,不会实际操作)。

[root@xmxyk wwwlogs]#logrotate -d /etc/logrotate.conf 
reading config file /etc/logrotate.conf
including /etc/logrotate.d
reading config file dracut
reading config info for /var/log/dracut.log 
reading config file nginx
reading config info for /home/wwwlogs/*.log  
reading config file ppp
reading config info for /var/log/ppp/connect-errors 
reading config file syslog
reading config info for /var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler

reading config file yum
reading config info for /var/log/yum.log 
reading config info for /var/log/wtmp 
reading config info for /var/log/btmp 

Handling 7 logs

rotating pattern: /var/log/dracut.log  yearly (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/dracut.log
  log does not need rotating

rotating pattern: /home/wwwlogs/*.log   after 1 days (14 rotations)
empty log files are not rotated, only log files >= 10485760 bytes are rotated, old logs are removed
considering log /home/wwwlogs/access.log
  log does not need rotating
considering log /home/wwwlogs/bbs.xmxyk.net.log
  log does not need rotating
considering log /home/wwwlogs/dmqo.com.log
  log does not need rotating
considering log /home/wwwlogs/dqjo.com.log
  log does not need rotating
considering log /home/wwwlogs/hyhya.com.log
  log does not need rotating
considering log /home/wwwlogs/nginx_error.log
  log does not need rotating
considering log /home/wwwlogs/ngjx.com.log
  log does not need rotating
considering log /home/wwwlogs/sitestar.nsjd.com.log
  log does not need rotating
considering log /home/wwwlogs/txmi.net.log
  log does not need rotating
considering log /home/wwwlogs/www.nsjd.com.log
  log does not need rotating
considering log /home/wwwlogs/www.xmxyk.net.log
  log does not need rotating
considering log /home/wwwlogs/www.yongfuyinghua.com.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

rotating pattern: /var/log/ppp/connect-errors  yearly (5 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/ppp/connect-errors
  log /var/log/ppp/connect-errors does not exist -- skipping

rotating pattern: /var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
 monthly (4 rotations)
empty log files are rotated, only log files >= 10485760 bytes are rotated, old logs are removed
considering log /var/log/cron
  log does not need rotating
considering log /var/log/maillog
  log does not need rotating
considering log /var/log/messages
  log does not need rotating
considering log /var/log/secure
  log does not need rotating
considering log /var/log/spooler
  log does not need rotating
not running prerotate script, since no logs will be rotated
not running postrotate script, since no logs were rotated

rotating pattern: /var/log/yum.log  yearly (4 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/yum.log
  log does not need rotating

rotating pattern: /var/log/wtmp  monthly (1 rotations)
empty log files are rotated, only log files >= 1048576 bytes are rotated, old logs are removed
considering log /var/log/wtmp
  log does not need rotating

rotating pattern: /var/log/btmp  monthly (1 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/btmp
  log does not need rotating

如上可知,系统上没有任何一下日志rotate。

参考资料:

最后编辑:
作者:admin
这个作者貌似有点懒,什么都没有留下。

留下一个回复

你的email不会被公开。