在 CentOS 系统中,Nginx 日志切割通常是通过 logrotate 工具来实现的。以下是配置 Nginx 日志切割的步骤:
sudo yum install logrotate
nginx.conf 的文件:sudo vim /etc/logrotate.d/nginx
nginx.conf 文件中:/var/log/nginx/*.log {
daily
missingok
rotate 7
compress
notifempty
create 0640 nginx adm
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}
这里的配置表示:
保存并关闭 nginx.conf 文件。
测试 logrotate 配置是否正确:
sudo logrotate -f /etc/logrotate.d/nginx
如果没有错误信息,说明配置正确。
现在,Nginx 日志切割已经配置好了。logrotate 会根据配置文件中的设置自动切割 Nginx 日志文件。你也可以手动触发日志切割,使用以下命令:
sudo logrotate -f /etc/logrotate.d/nginx
这将强制执行 Nginx 日志切割,即使距离上次切割时间还很短。