設置nginx日志滾動


需求:設置nginx每天凌晨12點輪轉,系統版本為debian7,nginx版本為tengine2.2.0

1、修改logrotate主配置文件,打開壓縮和以時間為后綴命名

# vim /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
compress

# packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp, or btmp -- we'll rotate them here
/var/log/wtmp {
    missingok
    monthly
    create 0664 root utmp
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0660 root utmp
    rotate 1
}

# system-specific logs may be configured here

2、添加一條nginx的logrotate配置(正常配置到這里就可以了)

# vim /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
    daily
    rotate 7
    compress
    missingok
    sharedscripts
    postrotate
        if [ -f /usr/local/nginx/sbin/nginx-tengine.pid ]; then
            kill -USR1 `cat /usr/local/nginx/sbin/nginx-tengine.pid`
        fi
    endscript
}

3、刪除系統自帶的每日輪轉任務

# mv /etc/cron.daily/logrotate /tmp/

4、添加一條計划任務,每天凌晨12點輪轉

# crontab -e
0 0 * * *  /usr/sbin/logrotate /etc/logrotate.conf

5、修改crontab時區,避免時區問題

# vim /etc/crontab
CRON_TZ=Asia/Shanghai

# /etc/init.d/cron restart


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM