nginx使用logrotate对日志进行分割,nginxlogrotate


系统环境

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...

相关内容

    暂无相关文章