使用 logrotate 对 apache/nginx 日志切割

logrotate 是 linux 系统用来分割日志的系统工具,可以方便将日志按周期(日,周,月)和大小进行分割。

当我们的服务器访问量比较大时,服务器的 access.log 可能会 G/天的级别增长,而我们希望日志可以按天周月或当日志文件大小达到某个限额时进行分割。

列举下如何使用它对 apache 或 nginx 日志进行切分:

apache 集成的 rotatelogs 工具

apache 其实自身已经集成了一个叫  rotatelogs 的工具,就在 apache 安装目录的 bin 下面,可以很方便的进行日志切割规则设置,在你的配置文件或者虚拟主机配置中如下设置

#年月日时分秒 1G大小分割
ErrorLog "|/usr/local/apache/bin/rotatelogs /var/log/httpd/error_log_%Y%m%d%H%M%S 1024M"
#年月日 1G大小
CustomLog "|/usr/local/apache/bin/rotatelogs /var/log/httpd/access_log_%Y%m%d 1024M" common
#年月日 每86400秒 即1天分割一次 480 为时区的偏移量 北京东八区 60 * 8 = 480 s
CustomLog "|/usr/local/apache/bin/rotatelogs /var/log/httpd/access_log_%Y%m%d 86400 480" common
logrotate工具

/etc/logrotate.conf 主配置文件

# see "man logrotate" for details
# rotate log files weekly 以周作为周期
weekly

# keep 4 weeks worth of backlogs 保留4个分割文件
rotate 4

# create new (empty) log files after rotating old ones 创建新的日志文件
create

# use date as a suffix of the rotated file 以日期为后缀
dateext

# uncomment this if you want your log files compressed 默认不压缩
#compress

# RPM packages drop log rotation information into this directory 自定义日志分割配置 nginx 的放这就好
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
        minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

默认配置如上

/etc/logrotate.d/* 独立配置文件

我们可以将自定义的日志分割配置放在此处,会被加载进入主配置文件中,比如我们新建一个

/etc/logrotate.d/nginx 配置文件:

nginx的默认日志目录是 /home/wwwlogs/,默认pid目录是/usr/local/nginx/logs/nginx.pid 这些需要根据自己的配置做相应调整。

/home/wwwlogs/*.log {
    daily #以天为周期分割日志
    minsize 1024M #最小 比如每日分割 但如果大小未到 1024M 则不分割
    maxsize 2048M #最大 当日志文件超出 2048M 时,即便再下一个时间周期之前 日志也会被分割
    rotate 7 #保留七天
    missingok #忽略错误
    notifempty #如果文件为空则不分割 not if empty
    dateext #以日期为单位
    size 1024M #以大小为限制做日志分割 size 会覆盖分割周期配置 1024 1024k 1024M 1024G
    sharedscripts #开始执行附加的脚本命令 nginx写日志是按照文件索引进行的 必须重启服务才能写入新日志文件
    postrotate
        if [ -f /usr/local/nginx/logs/nginx.pid ]; then
            kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` #重启nginx服务
        fi
    endscript
}

我自己用的,一个日志文件按1G大小分割

/home/wwwlogs/*.log {
    rotate 7
    missingok
    notifempty
    dateext
    size 1024M
    sharedscripts
    postrotate
        if [ -f /usr/local/nginx/logs/nginx.pid ]; then
            kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` #重启nginx服务
        fi
    endscript
}

/etc/cron.daily/logrotate 每日任务

cron.daily 是 crond 的一个每日任务,每天凌晨零点执行一次

配置检测

/usr/sbin/logrotate -dv 来开启debugger详情模式来查看配置是否正确

例如检测全局

/usr/sbin/logrotate -dv /etc/logrotate.conf

检测nginx配置

/usr/sbin/logrotate -dv /etc/logrotate.d/nginx

强制执行

/usr/sbin/logrotate -fv /etc/logrotate.d/nginx
常见问题

1、because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.

日志文件权限问题,在相应的配置中设置如下配置就好 root 组的 root 用户,自己做相应的权限声明即可

#su user group
su root root

OK,具体的用法可以 man logrotate 一下!

转载于:https://my.oschina.net/sallency/blog/752863

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值