2

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??

Arthur Blake
  • 95
  • 1
  • 1
  • 6

7 Answers7

6

To rotate nginx logs in Windows, create a batch file like this one:

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)

Then create a scheduled task in Windows to run that batch file how ever often you want to rotate the logs.

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.

To do this, create a new user called 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.

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"
Community
  • 1
  • 1
Tom Wadley
  • 122,421
  • 1
  • 22
  • 29
6

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.

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.

Mat
  • 188,820
  • 38
  • 367
  • 383
Arthur Blake
  • 95
  • 1
  • 1
  • 6
  • This is the post about running nginx as a windows service: http://misterdai.wordpress.com/2009/10/16/nginx-windows-service/ and by the way it works really well. – Arthur Blake Aug 24 '11 at 13:16
2

with windows server 2008 R2, I create this batch file, and I schedule it one time a day at midnight:

@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
Frizz1977
  • 912
  • 11
  • 19
1

1.first create a file to store your log file list, like "nginx_log.lst" with content:

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":

@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

etng
  • 11
  • 2
0

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 answer above.

Vasu
  • 25
  • 5
0

I wrote small utility which rotates log files after stoppig nginx (which is running as windows service) for few seconds.

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

Thanks

Mandar
  • 19
  • 1
  • 2
    thanks for the information - however, nginx is designed to rotate logs without stopping at all, with the reopen signal (by issuing nginx -s reopen) - this was fixed in the windows version some time back. You may want to consider rewriting your program to use this mechanism. – Arthur Blake May 17 '11 at 10:50
0
@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
  • Welcome to Stack Overflow. Code dumps without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your question and explain how it works better than what the OP provided. See [answer]. – Chris May 28 '20 at 20:07