使用 logrotate进行nginx日志分割

1、创建nginx配置文件

[root@xuegod63 httpd]# vim /etc/logrotate.d/nginx    
/usr/local/nginx/logs/*.log {
daily
rotate 5
sharedscripts
postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
/usr/local/nginx/sbin/nginx -s reload
fi
endscript
}
/usr/local/nginx/logs/*.log {     #指定日志文件位置,可用正则匹配 
daily                             #调用频率,有:daily,weekly,monthly可选 rotate 5                          #一次将存储5个归档日志。对于第六个归档,时间最久的归档将被删除。 
sharedscripts                     #所有的日志文件都轮转完毕后统一执行一次脚本 postrotate                        #执行命令的开始标志     
if [ -f /usr/local/nginx/logs/nginx.pid ]; then    #判断nginx是否启动         /usr/local/nginx/sbin/nginx -s reload         #让nginx重新加载配置文件,生成新的日志文件,如果nginx没启动不做操作     
fi
 endscript  #执行命令的结束标志 }

重启rsyslog服务

[root@wangxuejin-test195 nginx-1.8.0]# systemctl restart rsyslog

尝试切割

[root@wangxuejin-test195 nginx-1.8.0]# logrotate -d /etc/logrotate.d/nginx
[root@wangxuejin-test195 nginx-1.8.0]# logrotate -vf /etc/logrotate.d/nginx

查看切割

发表评论

后才能评论