利用logrotate分割Nginx日志

### 利用 logrotate 分割 Nginx 日志

1. 默认centos系统安装自带 logrotate

2. vim /etc/logrotate.d/nginx

“`

    /www/wwwlogs/*.log #此处为nginx存储日志的地方;

    {

    daily #指定转储周期为每天

    rotate 30 #转储次数,超过将会删除最老的那一个

    missingok #如果日志文件丢失,不要显示错误

    compress #通过gzip 压缩转储以后的日志

    delaycompress #当前转储的日志文件到下一次转储时才压缩

    notifempty #当日志文件为空时,不进行轮转

    postrotate #执行的指令

    if [ -f /www/server/nginx/logs/nginx.pid ]; then

    kill -USR1 `cat /www/server/nginx/logs/nginx.pid`

    fi

    endscript #PID路径根据实际路径填写;

    }

“`

“`

    /www/wwwlogs/*.log

    {

    daily

    rotate 30

    missingok

    compress

    delaycompress

    notifempty

    postrotate

    if [ -f /www/server/nginx/logs/nginx.pid ]; then

    kill -USR1 `cat /www/server/nginx/logs/nginx.pid`

    fi

    endscript

    }

“`

3. crontab -u root –e

“`

    0 0 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx

“`