centos

CentOS Nginx日志轮转策略有哪些

小樊
31
2025-12-29 09:41:33
栏目: 智能运维

CentOS 下 Nginx 日志轮转策略

一 核心策略与常用参数

二 典型策略模板

/var/log/nginx/*.log {
    daily
    missingok
    rotate 7
    compress
    delaycompress
    notifempty
    create 0640 nginx nginx
    sharedscripts
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)
    endscript
}
/var/log/nginx/*.log {
    daily
    missingok
    rotate 30
    compress
    delaycompress
    notifempty
    create 0640 nginx nginx
    sharedscripts
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)
    endscript
}
/var/log/nginx/*.log {
    daily
    minsize 1M
    rotate 7
    compress
    delaycompress
    notifempty
    create 0640 nginx nginx
    sharedscripts
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)
    endscript
}
/var/log/nginx/*.log {
    daily
    rotate 30
    compress
    delaycompress
    notifempty
    create 0640 nginx nginx
    dateext
    dateformat -%Y%m%d
    sharedscripts
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)
    endscript
}

以上模板覆盖按天、按大小、按保留天数、按日期命名等常见需求,可直接放入 /etc/logrotate.d/nginx 使用。

三 生效机制与验证

四 注意事项与最佳实践

0
看了该问题的人还看了