一、需求场景
在高并发 Web 服务中,Nginx 作为反向代理或静态服务器,每天会产生大量访问日志(access.log)和错误日志(error.log)。如果不进行日志轮转(Log Rotation),日志文件会越来越大,不仅占用磁盘空间,还会导致:
本文将带你深入掌握 Nginx 日志轮转的最佳实践,涵盖 按天、按周、按月、按年 等多种实用场景,并提供完整配置案例,助你实现自动化、可维护的日志管理体系。
二、什么是日志轮转?
日志轮转(Log Rotation) 是指定期将当前日志文件重命名归档,并创建一个新的空日志文件继续写入的过程。常见操作包括:
在 Linux 系统中,我们通常使用 logrotate 工具来实现这一功能。
三、logrotate 简介
logrotate 是 Linux 自带的日志管理工具,支持:
配置文件路径:
/etc/logrotate.conf 主配置
/etc/logrotate.d/ 第三方服务配置目录(推荐)
四、日志轮转通用配置
基础配置(推荐放入 /etc/logrotate.d/nginx)
/usr/local/nginx/logs/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 0644 nginx nginx
sharedscripts
postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
kill -USR1 cat /usr/local/nginx/logs/nginx.pid
fi
endscript
}
配置说明:
提示:kill -USR1 是 Nginx 的“重新打开日志文件”信号,不会中断服务。
五、不同场景下的策略
场景1:按天轮转(最常用)
适用于大多数中小型网站。
/etc/logrotate.d/nginx-day
/usr/local/nginx/logs/access.log {
daily
rotate 365
compress
missingok
notifempty
create 0644 nginx nginx
sharedscripts
postrotate
/bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null || true
endscript
}
效果:
场景2:按周轮转(减少文件数量)
适合日志量中等,希望减少碎片化文件的场景。
/etc/logrotate.d/nginx-week
/usr/local/nginx/logs/access.log {
weekly
rotate 52
compress
missingok
notifempty
create 0644 nginx nginx
sharedscripts
postrotate
/bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null || true
endscript
}
效果:
场景3:按月轮转(适合大站归档)
适合日志量巨大、需长期归档的大型网站。
/etc/logrotate.d/nginx-month
/usr/local/nginx/logs/access.log {
monthly
rotate 24
compress
missingok
notifempty
create 0644 nginx nginx
sharedscripts
postrotate
/bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null || true
endscript
}
效果:
场景4:按年轮转(历史归档)
适用于需要长期保留原始日志的合规性需求。
/etc/logrotate.d/nginx-year
/usr/local/nginx/logs/access.log {
yearly
rotate 10
compress
missingok
notifempty
create 0644 nginx nginx
sharedscripts
postrotate
/bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null || true
endscript
}
效果:
场景5:按大小轮转(突发流量应对)
适合突发高并发场景,防止单个日志过大。
/etc/logrotate.d/nginx-size
/usr/local/nginx/logs/access.log {
size 100M
rotate 10
compress
missingok
notifempty
create 0644 nginx nginx
sharedscripts
postrotate
/bin/kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid 2>/dev/null) 2>/dev/null || true
endscript
}
效果:
六、测试 logrotate 配置
logrotate -d /etc/logrotate.d/nginx
-d 表示 debug 模式,查看执行流程。
logrotate -f /etc/logrotate.d/nginx
-f 表示强制轮转,可用于测试。
cat /var/lib/logrotate/status
或
logrotate -v /etc/logrotate.conf
七、自定义日志路径与归档
示例:将日志按年月分类归档
postrotate
创建年月目录并移动压缩文件
LOG_DIR="/data/logs/nginx"
DATE=$(date -d "yesterday" +%Y-%m)
mkdir -p "
mv /usr/local/nginx/logs/.log- "
endscript
效果:
/data/logs/nginx/
├── 2025-04/
│ ├── access.log-20250405.gz
│ └── error.log-20250405.gz
├── 2025-03/
└── ...
八、常见问题与解决方案
问题:日志未切割
原因:logrotate 未运行
解决方案:检查 /etc/cron.daily/logrotate 是否存在
问题:切割后 Nginx 不写新日志
原因:未发送 USR1 信号
解决方案:确保 postrotate 正确执行
问题:权限错误
原因:新日志属主不对
解决方案:使用 create 0644 nginx nginx
问题:日志被重复压缩
原因:delaycompress 缺失
解决方案:添加 delaycompress
问题:磁盘满
原因:旧日志未删除
解决方案:设置合理的 rotate N
九、最佳实践建议
Nginx 日志轮转不是“可有可无”的功能,而是保障系统稳定、提升运维效率的关键一环。通过 logrotate,你可以轻松实现:
现在就动手配置吧!让你的 Nginx 日志管理更智能、更高效。
附:一键部署脚本(可选)
自动创建 nginx 日志轮转配置(按天)
cat > /etc/logrotate.d/nginx << 'EOF'
/usr/local/nginx/logs/*.log {
daily
rotate 365
compress
delaycompress
missingok
notifempty
create 0644 nginx nginx
sharedscripts
postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
fi
endscript
}
EOF
测试配置
logrotate -d /etc/logrotate.d/nginx
提示:请根据实际路径调整 nginx.pid 和日志路径。
十、推荐工具:灵燕空间-HTTPS证书图形化控制台
管理 Nginx 时,除了日志轮转,SSL 证书的申请、部署与监控也是运维中的高频痛点。手动更新证书容易遗忘,导致网站 HTTPS 中断。
推荐使用国产神器:灵燕空间 - HTTPS证书图形化控制台
为什么选择灵燕空间?
官网地址:https://www.lingyanspace.com
让你的 Nginx 运维更简单、更安全!