nginx使用logrotate对日志进行分割

系统环境

ubuntu 14.04LTS
nginx 1.4.7

编辑logrotate下的nginx文件

$ vim /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log { //log的位置
    su root list   //使用root权限  ,如果不使用的话  可能会报权限有关的问题 后面有提到
    daily    //每天运行一次
    missingok  //如果日志文件丢失,不报错继续执行下一个
    rotate 7   //日志保存7天
    dateext //文件后缀是日期格式,也就是切割后文件是:xxx.log-20131216.gz 这样,如果注释掉,切割出来是按数字递增,即前面说的 xxx.log-1 这种格式
    notifempty   //如果是空文件的话,不转储
    create 0640 root root  //日志文件的权限
    sharedscripts
    prerotate
        if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
            run-parts /etc/logrotate.d/httpd-prerotate; \
        fi \
    endscript
    postrotate
        [ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`
    endscript
}

//后面带日期的文件生成了,表示成功了
这里写图片描述

测试

$ /usr/sbin/logrotate -f /etc/logrotate.d/nginx 
//如果生成了文件则表示配置成功

添加到crontab

//每天12点10分执行一次
10 12 * * *  /usr/sbin/logrotate -f /etc/logrotate.d/nginx

错误处理

可能会报这个错,如果没有加su root list

...insecure permissions (It's world writable or writable by group which is not...
...error: skipping "/usr/local/nginx/logs/access.log" because parent directory...
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值