Windows上nginx的日志轮换

2ledvvac  于 2022-10-06  发布在  Windows
关注(0)|答案(7)|浏览(216)

我在网上找到了大量关于在Linux下旋转nginx日志的参考资料。只需向进程发送USR1信号。但是..。Windows上不存在类似Unix的信号,我也找不到任何有关这方面的信息。我如何在Windows上用nginx完成同样的事情??

e1xvtsh3

e1xvtsh31#

要在Windows中旋转nginx日志,创建一个批处理文件,如下所示:

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set YMD=%%c-%%a-%%b)
move C:pathtonginxlogsAccess.log C:pathtonginxlogsAccess_%YMD%.log
move C:pathtonginxlogsError.log C:pathtonginxlogsError_%YMD%.log
call C:pathtonginxnginx -p C:pathtonginx -s reopen

第一行只创建了一个时间戳(记入Jay)

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

如果nginx是作为服务运行(例如通过here描述的Windows服务 Package 器),则不能直接调用像nginx -s reopen这样的nginx命令。相反,您必须以运行服务的用户身份运行命令。

为此,创建一个名为nginx的新用户,并配置服务和计划任务以该用户身份运行。您还必须确保您的用户拥有“Logon as a batch job”权限。

如果希望在命令行上测试循环脚本,而不必使用计划任务,则可以使用

runas /user:nginx "C:pathtorotateLogs.bat"
z9smfwbn

z9smfwbn2#

实际上(尽管谷歌搜索了无数次),答案完全可以在in the doc pages找到。该命令是nginx -s reopen,但这似乎只有在从命令行运行nginx时才有效--目前这是在Windows上运行nginx的唯一官方方式。

我的下一个挑战是在Run nginx as a Windows service的答案中描述的那样,在将nginx作为Windows服务运行时,如何使其工作。

rkue9o1l

rkue9o1l3#

使用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.1logsaccess.log D:nginx-1.11.1logsaccess_%DATE_FRM%.log
move D:nginx-1.11.1logserror.log D:nginx-1.11.1logserror_%DATE_FRM%.log
call D:nginx-1.11.1nginx -p D:nginx-1.11.1 -s reopen
ix0qys7i

ix0qys7i4#

1.首先创建一个文件来存储您的日志文件列表,如“nginx_log.lst”,内容如下:
D:\Projects\Example.com\Data\log\Access.log D:\Projects\Example.com\Data\log\error.log

2.将以下内容保存到BAT文件中,如nginx_log_rotate.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:toolsnginx
nginx -s reopen
popd
pause
@echo on

3.创建一个计划任务以按您希望的方式运行BAT

tyky79it

tyky79it5#

我写了一个小工具,它在停止nginx(它作为Windows服务运行)几秒钟后轮换日志文件。

它有特定的要求停止,然后复制日志文件,然后重新启动每晚服务。您可以下载代码并以任何您想要的方式更改它。

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

谢谢

nkkqxpd9

nkkqxpd96#

出于下面的一些原因,批处理文件对我起作用了。

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

它与上面的Tom's answer大致相同。

ssgvzors

ssgvzors7#

@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_logsNUL" mkdir "%NGINX_PATH%old_logs"

REM move error.log in old_logs from logs folder and rename it
move %NGINX_PATH%logsaccess.log %NGINX_PATH%old_logsaccess_%DATE_FRM%.log
move %NGINX_PATH%logserror.log %NGINX_PATH%old_logserror_%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_logsaccess_%DATE_FRM%.log -DestinationPath %NGINX_PATH%old_logsaccess_%DATE_FRM%.log.zip -force
powershell Compress-Archive -Path %NGINX_PATH%old_logserror_%DATE_FRM%.log -DestinationPath %NGINX_PATH%old_logserror_%DATE_FRM%.log.zip -force

REM delete error%DATE_FRM%.log from old_logs.
del %NGINX_PATH%old_logsaccess_%DATE_FRM%.log
del %NGINX_PATH%old_logserror_%DATE_FRM%.log

相关问题