Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
windows上nginx的日志旋转_Windows_Nginx_Signals_Logging_Rotation - Fatal编程技术网

windows上nginx的日志旋转

windows上nginx的日志旋转,windows,nginx,signals,logging,rotation,Windows,Nginx,Signals,Logging,Rotation,我在网上找到了很多关于在linux下旋转nginx日志的参考资料。。只需向进程发送USR1信号。但是类似unix的信号在windows上不存在,我也找不到任何有关这方面的信息。我如何在windows上使用nginx完成同样的事情呢?事实上(尽管谷歌搜索了很多次),答案是可以找到的 命令是: nginx -s reopen 但这似乎只有在从命令行运行nginx时才起作用——目前这是在windows上运行nginx的唯一官方方式 我的下一个挑战是弄清楚如何在将nginx作为windows服务运行时

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

事实上(尽管谷歌搜索了很多次),答案是可以找到的

命令是:

nginx -s reopen
但这似乎只有在从命令行运行nginx时才起作用——目前这是在windows上运行nginx的唯一官方方式


我的下一个挑战是弄清楚如何在将nginx作为windows服务运行时实现这一点,如下所述:。

我编写了一个小实用程序,它在停止nginx(作为windows服务运行)几秒钟后旋转日志文件

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

代码如下:


谢谢。首先创建一个文件来存储日志文件列表,如“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”:


三,。创建一个计划任务以按您的意愿运行bat

要在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
第一行只是创建了一个时间戳(贷记到)

然后在Windows中创建一个计划任务,以按您希望的频率循环日志来运行该批处理文件

如果nginx是作为服务运行的(如通过所述的Windows服务包装器),则不能直接调用nginx命令,如
nginx-s respone
。相反,您必须以运行服务的用户的身份运行这些命令

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

如果要在命令行上测试旋转脚本,而不必使用计划任务,可以使用

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

使用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

由于以下原因,批处理文件对我有效

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

这或多或少与上述内容相同。

感谢您提供的信息-然而,nginx设计为在不停止的情况下旋转日志,并发出重新打开信号(通过发出nginx-s REPOWEN)-这在windows版本中已在一段时间前得到修复。您可能想考虑改写程序来使用这个机制。这是关于将NGINX作为Windows服务运行的问题:顺便说一下,它工作得很好。欢迎来到堆栈溢出。没有任何解释的代码转储很少有帮助。堆栈溢出是关于学习,而不是提供盲目复制和粘贴的代码片段。请回答您的问题,并解释它如何比OP提供的更好地工作。看见
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
@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