windows windows上nginx的日志轮换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3535989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log rotation for nginx on windows
提问by Arthur Blake
I've found plenty of references on the web for rotating the nginx logs under linux.. just send the USR1 signal to the process. But... unix like signals don't exist on windows and I haven't been able to find any information on this. How can I accomplish the same thing with nginx on windows??
我在网上找到了很多关于在 linux 下轮换 nginx 日志的参考资料。只需将 USR1 信号发送到进程即可。但是...... windows 上不存在类似 unix 的信号,我无法找到任何有关此的信息。如何在 Windows 上使用 nginx 完成同样的事情?
回答by Arthur Blake
Actually, (despite tons of googling), the answer can be found squarely in the doc pages.
实际上,(尽管进行了大量的谷歌搜索),可以在文档页面中直接找到答案。
The command is:
命令是:
nginx -s reopen
But this only seems to work when running nginx from the command line – currently the only official way to run nginx on windows at this time.
但这似乎只有在从命令行运行 nginx 时才有效——目前这是目前在 Windows 上运行 nginx 的唯一官方方法。
My next challenge is to figure out how to make this work when running nginx as a windows service as described here: Nginx Windows Service.
我的下一个挑战是弄清楚如何在将 nginx 作为 Windows 服务运行时如何使其工作,如下所述:Nginx Windows Service。
回答by Tom Wadley
To rotate nginx logs in Windows, create a batch filelike this one:
要在 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
That first line just creates a timestamp (credit to Jay)
第一行只是创建一个时间戳(归功于Jay)
Then create a scheduled taskin Windows to run that batch file how ever often you want to rotate the logs.
然后在 Windows 中创建一个计划任务以运行该批处理文件,您希望以何种频率轮换日志。
If nginx is running as a service(such as through the Windows Service Wrapper described here) you can't simply call nginx commands like nginx -s reopen
directly. Instead you have to run the commands as the user who the service is running as.
如果 nginx 作为服务运行(例如通过此处描述的 Windows 服务包装器),则不能直接调用 nginx 命令nginx -s reopen
。相反,您必须以运行服务的用户身份运行命令。
To do this, create a new usercalled nginx
(for example) and configure both the service and the scheduled task to run as that user. You'll also have to make sure your user has "Logon as a batch job" rights.
为此,请创建一个名为nginx
(例如)的新用户,并将服务和计划任务配置为以该用户身份运行。您还必须确保您的用户具有“作为批处理作业登录”的权限。
If you want to test your rotation script on the command line without having to use a scheduled task you can use
如果您想在命令行上测试您的轮换脚本而不必使用计划任务,您可以使用
runas /user:nginx "C:\path\to\rotateLogs.bat"
回答by Frizz1977
with windows server 2008 R2, I create this batch file, and I schedule it one time a day at midnight:
使用 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
回答by etng
1.first create a file to store your log file list, like "nginx_log.lst" with content:
1.首先创建一个文件来存储您的日志文件列表,例如“nginx_log.lst”,内容如下:
D:\projects\example.com\data\log\access.log D:\projects\example.com\data\log\error.log
D:\projects\example.com\data\log\access.log D:\projects\example.com\data\log\error.log
2.save the following content to a bat file such as "nginx_log_rotate.bat":
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. create a schedule task to run the bat as you wish
3.创建一个调度任务,随心所欲地运行bat
回答by Mandar
I wrote small utility which rotates log files after stoppig nginx (which is running as windows service) for few seconds.
我编写了一个小实用程序,它在 stoppig nginx(作为 Windows 服务运行)几秒钟后轮转日志文件。
It had specific requirement to stop , then copy log files and then restart service nighly basis. You can download the code and change it whatever way you want.
它有特定的要求停止,然后复制日志文件,然后每晚重新启动服务。您可以下载代码并随心所欲地更改它。
Code is here : http://mandar.tumblr.com/post/5419161330/nginx-logrotate-windows
代码在这里:http: //mandar.tumblr.com/post/5419161330/nginx-logrotate-windows
Thanks
谢谢
回答by Vasu
For some reasons below batch file worked for me.
由于某些原因,下面的批处理文件对我有用。
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
It's more or less same as Tom's answerabove.
它或多或少与上面汤姆的回答相同。
回答by Ravindra Sambherao
@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