Nginx日志轮转配置指南
· 阅读需 3 分钟
Nginx日志轮转配置指南
注:本文档基于nginx日志目录为 /etc/nginx/logs/ 的环境配置。 如果您的nginx日志在其他目录(如默认的/var/log/nginx/),请相应调整路径。
1. 检查系统环境
首先确认系统已安装logrotate:
# 检查是否安装logrotate
which logrotate
# 如果未安装,使用以下命令安装
# CentOS/RHEL系统
sudo yum install logrotate
# Ubuntu/Debian系统
sudo apt-get install logrotate
2. 创建Nginx日志轮转配置
创建nginx专用的logrotate配置文件:
sudo vim /etc/logrotate.d/nginx
配置文件内容:
/etc/nginx/logs/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 0640 nginx nginx
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}