繁体   English   中英

nginx 在 windows 上的日志轮换

[英]log rotation for nginx on windows

我在 web 上找到了很多关于在 linux 下旋转 nginx 日志的参考资料。只需将 USR1 信号发送到进程。 但是... unix 类似的信号在 windows 上不存在,我无法找到这方面的任何信息。 我怎样才能在 windows 上用 nginx 完成同样的事情?

要在 Windows 中轮换 nginx 日志,请创建一个这样的批处理文件

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set YMD=%%c-%%a-%%b)
move C:\path\to\nginx\logs\Access.log C:\path\to\nginx\logs\Access_%YMD%.log
move C:\path\to\nginx\logs\Error.log C:\path\to\nginx\logs\Error_%YMD%.log
call C:\path\to\nginx\nginx -p C:\path\to\nginx -s reopen

第一行只是创建一个时间戳(归功于Jay

然后在 Windows 中创建一个计划任务来运行该批处理文件,您希望以何种频率轮换日志。

如果 nginx 作为服务运行(例如通过此处描述的 Windows 服务包装器),您不能简单地调用 nginx 命令,例如nginx -s reopen直接nginx -s reopen 相反,您必须以运行服务的用户身份运行命令。

为此,请创建一个名为nginx的新用户(例如),并将服务和计划任务配置为以该用户身份运行 您还必须确保您的用户具有“作为批处理作业登录”的权限。

如果您想在命令行上测试您的轮换脚本而不必使用计划任务,您可以使用

runas /user:nginx "C:\path\to\rotateLogs.bat"

实际上,(尽管进行了大量的谷歌搜索),答案可以直接在文档页面中找到

命令是:

nginx -s reopen

但这似乎只有在从命令行运行 nginx 时才有效——目前这是目前在 Windows 上运行 nginx 的唯一官方方法。

我的下一个挑战是弄清楚如何在将 nginx 作为 Windows 服务运行时如何使其工作,如下所述: Nginx Windows Service

使用 Windows Server 2008 R2,我创建了这个批处理文件,并每天在午夜安排一次:

@echo off
SET DATE=%date%
SET DAY=%DATE:~0,2%
SET MONTH=%DATE:~3,2%
SET YEAR=%DATE:~6,4%
SET DATE_FRM=%YEAR%-%MONTH%-%DAY%


ECHO %DATE_FRM%

REM ECHO %YEAR%
REM ECHO %MONTH%
REM ECHO %DAY% 

move D:\nginx-1.11.1\logs\access.log D:\nginx-1.11.1\logs\access_%DATE_FRM%.log
move D:\nginx-1.11.1\logs\error.log D:\nginx-1.11.1\logs\error_%DATE_FRM%.log
call D:\nginx-1.11.1\nginx -p D:\nginx-1.11.1 -s reopen

1.首先创建一个文件来存储您的日志文件列表,例如“nginx_log.lst”,内容如下:

D:\\projects\\example.com\\data\\log\\access.log D:\\projects\\example.com\\data\\log\\error.log

2.将以下内容保存到“nginx_log_rotate.bat”等bat文件中:

@echo off
set YMD=%date:~0,4%%date:~5,2%%date:~8,2%
set LOG_FILE=
FOR /F "eol=; delims=, " %%i in (nginx_log.lst) do (
    echo "%%i"
    move "%%i" "%%i.%YMD%"
)
pushd C:\tools\nginx
nginx -s reopen
popd
pause
@echo on

3.创建一个调度任务,随心所欲地运行bat

由于某些原因,下面的批处理文件对我有用。

For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set YMD=%%c-%%a-%%b)
move .\logs\access.log .\logs\access.%YMD%.log
move .\logs\error.log .\logs\error.%YMD%.log
nginx.exe -s reload

它或多或少与上面汤姆的回答相同。

我编写了一个小实用程序,它在 stoppig nginx(作为 Windows 服务运行)几秒钟后轮换日志文件。

它有特定的要求停止,然后复制日志文件,然后每晚重新启动服务。 您可以下载代码并随心所欲地更改它。

代码在这里: http : //mandar.tumblr.com/post/5419161330/nginx-logrotate-windows

谢谢

@echo off
SET DATE_FRM=%date%

REM set path of Nginx root folder.
SET NGINX_PATH="E:\nginx-1.14.2"

REM create old_logs folder if not exists , we will move old logs in this folder.
if not exist "%NGINX_PATH%\old_logs\NUL" mkdir "%NGINX_PATH%\old_logs"

REM move error.log in old_logs from logs folder and rename it
move %NGINX_PATH%\logs\access.log %NGINX_PATH%\old_logs\access_%DATE_FRM%.log
move %NGINX_PATH%\logs\error.log %NGINX_PATH%\old_logs\error_%DATE_FRM%.log

REM reopn nginx logs, this will create new error.log for nginx.
call %NGINX_PATH%\nginx -p %NGINX_PATH% -s reopen

REM compress error%DATE_FRM%.log, this will create error_%DATE_FRM%.log.zip file.
powershell Compress-Archive -Path %NGINX_PATH%\old_logs\access_%DATE_FRM%.log -DestinationPath %NGINX_PATH%\old_logs\access_%DATE_FRM%.log.zip -force
powershell Compress-Archive -Path %NGINX_PATH%\old_logs\error_%DATE_FRM%.log -DestinationPath %NGINX_PATH%\old_logs\error_%DATE_FRM%.log.zip -force

REM delete error%DATE_FRM%.log from old_logs.
del %NGINX_PATH%\old_logs\access_%DATE_FRM%.log
del %NGINX_PATH%\old_logs\error_%DATE_FRM%.log

我使用NSSM管理 Windows 上的 Nginx (在WhiteHorse上检查)。 尽管此实用程序具有内置的旋转功能,但它可能无法正常工作。 该文档警告在线轮换的风险。

应该配置两个服务条目:

  1. Nginx,和

  2. Nginx 轮换日志。

该解决方案不支持基于文件大小或文件年龄的轮换。 可以使用nginx -s reopen设置轮换日志的计划。

首先

使用 NSSM 创建 Nginx Rotate Logs 服务(注意:您可以使用相同的命令创建主 Nginx 服务)。

下面的命令将创建服务条目:

nssm install "Nginx Rotate" C:\tools\nginx-1.21.6\nginx.exe

下一行将逐步配置服务,此处没有oneliner。

nssm set "Nginx Rotate" AppParameters "-s reopen"
nssm set "Nginx Rotate" AppExit Default Exit
nssm set "Nginx Rotate" AppStdout C:\tools\nginx-1.21.6\logs\access.log
nssm set "Nginx Rotate" AppStderr C:\tools\nginx-1.21.6\logs\error.log
nssm set "Nginx Rotate" AppRedirectHook 1
nssm set "Nginx Rotate" AppRotateFiles 1
nssm set "Nginx Rotate" AppRotateOnline 1
nssm set "Nginx Rotate" AppTimestampLog 1
nssm set "Nginx Rotate" Description "Some description"
nssm set "Nginx Rotate" DisplayName "Nginx Rotate Logs"
nssm set "Nginx Rotate" ObjectName DOMAIN\user "password"
nssm set "Nginx Rotate" Start SERVICE_DEMAND_START
nssm set "Nginx Rotate" Type SERVICE_WIN32_OWN_PROCESS

然后编辑服务:

nssm edit "Nginx Rotate"

在您需要设置的最后一个选项卡上:

Event = Log rotation + Before online log rotation

如下图所示。

在此处输入图像描述

因此,此服务将处于手动模式。 启动后,它会旋转日志并停止。

创建计划任务。

schtasks /create /sc daily /st 03:30 /tn "Nginx Rotate Logs" /tr "C:\Program Files\PowerShell\7\pwsh.exe -command start-service 'Nginx Rotate Logs'"

重要

对 Nginx、Nginx 轮换日志和计划任务使用相同的用户。

用法

当主 Nginx 运行时,计划任务每天运行一次,轮换文件。

辅助

您可以添加每日任务(或添加到挂钩选项卡)来压缩和清除旧日志,例如:

# install PSCX module
Install-Module -Name Pscx -AllowPrerelease -AllowClobber

# compress
Get-ChildItem 'C:\tools\nginx-1.21.6\logs\*.log' | Where {$_.lastwritetime -lt (Get-Date).AddDays(-1)} | write-zip

# purge yesterday zip
Get-ChildItem 'C:\tools\nginx-1.21.6\logs\*.zip' | Where {$_.lastwritetime -lt (Get-Date).AddDays(-2)} | Remove-Item -Force
@echo off

net stop nginx


for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"

echo move %~dp0\Access.log %~dp0\Old\Access_%fullstamp%.log
move %~dp0\Access.log %~dp0\Old\Access_%fullstamp%.log

echo move %~dp0\Error.log %~dp0\Old\Error_%fullstamp%.log
move %~dp0\Error.log %~dp0\Old\Error_%fullstamp%.log

net start nginx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM