您的位置:首页 > 运维架构 > Nginx

logrotate切割nginx日志

2017-06-27 10:11 381 查看
链接

配置

测试

添加定时任务

1 链接

个人博客: alex-my.xyz

CSDN: blog.csdn.net/alex_my

2 配置

使用系统自带的logrorate来切个nginx日志,位于/usr/sbin/logrotate

假设服务器上有两个网站的nginx配置分别如下:

去除其它配置信息,只保留了日志相关

A网站

...
access_log  /data/logs/a.com/access.log;
error_log   /data/logs/a.com/error.log;
...


B网站

...
access_log  /data/logs/b.com/access.log;
error_log   /data/logs/b.com/error.log;
...


/etc/logrotate.d/下创建一个配置文件 nginx, 内容如下:

# 这里可以添加你想切个的目录,也可以直接使用正则表达式

/data/logs/a.com/*.log
/data/logs/b.com/*.log
{
daily
rotate 30
missingok
dateext
compress
delaycompress
notifempty
sharedscripts
postrotate
if [ -f /usr/local/nginx/nginx.pid ]; then
kill -USR1 `cat /usr/local/nginx/nginx.pid`
fi
endscript
}


需要注意的是你们的nginx.pid位置,不一定是在/usr/local/nginx/nginx.pid

配置说明

配置说明
daily指定转储周期为每天
weekly指定转储周期为每周
monthly指定转储周期为每月
rotate转储次数,超过将会删除最老的那一个
missingok忽略错误,如“日志文件无法找到”的错误提示
dateext切换后的日志文件会附加上一个短横线和YYYYMMDD格式的日期
compress通过gzip 压缩转储旧的日志
delaycompress当前转储的日志文件到下一次转储时才压缩
notifempty如果日志文件为空,不执行切割
sharedscripts只为整个日志组运行一次的脚本
prerotate/endscript在转储以前需要执行的命令可以放入这个对,这两个关键字必须单独成行
postrotate/endscript在转储以后需要执行的命令可以放入这个对,这两个关键字必须单独成行

3 测试

执行以下命令进行测试

logrotate -vf /etc/logrotate.d/nginx


然后到相应的日志目录下查看 (/data/logs/a.com/, /data/logs/b.com/)

应该会有类似以下的文件:

access.log

access.log-20170626

error.log

error.log-20170626

4 添加定时任务

每日0点执行脚本

在终端运行 crontab -e

插入以下语句

0 0 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx logrotate