nginx自身并不能够切分或滚动日志,因此只能用一个bat脚本按天切割日志,并删除三天前的日志
@echo off
rem nginx滚动日志
rem nginx工作目录
set workspace=D:luozixi
ginx-1.16.0
rem 日志存放目录
set logdir=D:luozixi
ginx-1.16.0logs
rem 将当前日志重命名,用今日的日期
move %logdir%access.log %logdir%access_%date:~0,4%_%date:~5,2%_%date:~8,2%.log
move %logdir%error.log %logdir%error_%date:~0,4%_%date:~5,2%_%date:~8,2%.log
rem 重新打开日志文件,如果不做这一步,nginx会继续往已被重命名的日志文件中写入日志
%workspace%
ginx.exe -s reopen -p %workspace%
rem 删除三天前的日志
set DaysAgo=3
forfiles /p %logdir% /m *.log /d -%DaysAgo% /c "cmd /c del /f /q @path"
pause
然后将此bat脚本加到windows定时任务,每天凌晨执行
对%date:~0,4%_%date:~5,2%_%date:~8,2%
的说明:
C:Usersluozixi>echo %date%
2019/06/19 周三
C:Usersluozixi>echo %date:~0,4%
2019
C:Usersluozixi>echo %date:~0,4%_%date:~5,2%_%date:~8,2%
2019_06_19