近日做服务迁移发现nginx日志未做分割,导致日志文件很大。于是查了一下日志分割应用配置。
阿里云ecs默认自带logrotate,logrotate配置目录/etc/logrotate.d。看了一下,nginx对应的logroate
配置是存在的。检查了一下配置,发现nginx检测的目录是/var/log/nginx/*.log。
```
/var/log/nginx/*.log {
daily
rotate 100
missingok
compress
delaycompress
notifempty
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 cat /var/run/nginx.pid
fi
endscript
}
```
因为我们nginx的日志文件目录是放在/home/data/nginx 下,于是调整了一下目录。
```
/home/data/nginx/*.log {
daily
rotate 100
missingok
compress
delaycompress
notifempty
sharedscripts
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 cat /var/run/nginx.pid
fi
endscript
}
```
logrotate 提供了检测配置是否ok的方式,logrotate -d -v /etc/logrotate.conf.
执行了一下,提示如下。
```
rotating pattern: /home/data/nginx/logs/**/*.log /home/data/nginx/logs/*.log after 1 days (100 rotations)
empty log files are not rotated, old logs are removed
....
```
发现新配置已经检测到。
以上配置好,不一定会日志分割,具体原因不详
日志查看正常
```
cat /var/lib/logrotate/logrotate.status
```
强制分割
```
vi /etc/cron.daily/logrotate
/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status -f -v /etc/logrotate.conf
```