在window下使用nginx,一般网上推荐编写bat文件或者lograte的Windows版来分割日志。经过实践,直接使用power shell脚本更加方便,同时也不需要对nginx作出任何操作,提高服务的稳定性。
$nginxExePath = "C:\Users\ECSadmin\Documents\nginx-1.27.1.1\nginx.exe"
$nginxLogPath = "C:\Users\ECSadmin\Documents\nginx-1.27.1.1\logs\access.log"
$nginxRootDir = "C:\Users\ECSadmin\Documents\nginx-1.27.1.1"
$backupDir = "D:\logs_backup"
$daysToKeep = 10
New-Item -Path $backupDir -ItemType Directory -Force | Out-Null
if (Test-Path $nginxLogPath) {
$nginxBackupName = "nginx_access_$(Get-Date -Format 'yyyyMMdd').log"
Copy-Item $nginxLogPath "$backupDir\$nginxBackupName" -Force
Set-Content $nginxLogPath -Value $null -Force
}
Get-ChildItem $backupDir | Where-Object {
$_.LastWriteTime -lt (Get-Date).AddDays(-$daysToKeep)
} | Remove-Item -Force
评论