要配置Nginx以保存超过6个月的日志,您需要定期检查并轮换日志文件。Nginx本身不提供基于时间的日志自动分割功能,但是可以通过logrotate工具或者手动轮换日志文件来实现。 vim /etc/logrotate.d/nginx
,没有则创建
# 源文件
/var/log/nginx/*.log {
create 0640 nginx root
daily
rotate 180
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
/bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
endscript
}
修改为
/var/log/nginx/*.log {
create 0640 nginx root
daily
rotate 180
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
重启服务:service rsyslog restart