【发布时间】:2011-04-01 22:45:52
【问题描述】:
我在网上找到了很多关于在 linux 下旋转 nginx 日志的参考资料。只需将 USR1 信号发送到进程即可。但是...... Windows 上不存在类似 unix 的信号,我无法找到任何相关信息。如何在 Windows 上使用 nginx 完成相同的操作??
【问题讨论】:
标签: windows nginx signals logging rotation
我在网上找到了很多关于在 linux 下旋转 nginx 日志的参考资料。只需将 USR1 信号发送到进程即可。但是...... Windows 上不存在类似 unix 的信号,我无法找到任何相关信息。如何在 Windows 上使用 nginx 完成相同的操作??
【问题讨论】:
标签: windows nginx signals logging rotation
实际上(尽管有大量的谷歌搜索)可以直接找到答案in the doc pages。命令是nginx -s reopen,但这似乎只在从命令行运行 nginx 时才有效——目前是目前在 Windows 上运行 nginx 的唯一官方方式。
我的下一个挑战是弄清楚如何在将 nginx 作为 Windows 服务运行时使其工作,如Run nginx as a Windows service 的答案中所述。
【讨论】:
@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
【讨论】:
由于某些原因,下面的批处理文件对我有用。
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
和上面的Tom's answer差不多。
【讨论】:
在 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
【讨论】:
要在 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 作为服务运行(例如通过 here 描述的 Windows Service Wrapper),您不能直接调用像 nginx -s reopen 这样的 nginx 命令。相反,您必须以运行服务的用户身份运行命令。
为此,创建一个名为 nginx 的新用户(例如)并将服务和计划任务配置为以该用户身份运行。您还必须确保您的用户拥有“Logon as a batch job”权限。
如果您想在命令行上测试轮换脚本而无需使用计划任务,您可以使用
runas /user:nginx "C:\path\to\rotateLogs.bat"
【讨论】:
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
【讨论】:
我编写了一个小实用程序,它在 stoppig nginx(作为 Windows 服务运行)几秒钟后旋转日志文件。
它有特定的要求停止,然后复制日志文件,然后重新启动服务。您可以下载代码并随意更改。
代码在这里:http://mandar.tumblr.com/post/5419161330/nginx-logrotate-windows
谢谢
【讨论】: