log rotation for nginx on windows

13,053

Solution 1

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 in the answers to Run nginx as a Windows service.

Solution 2

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"

Solution 3

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

Solution 4

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

Share:
13,053
Arthur Blake
Author by

Arthur Blake

Updated on June 04, 2022

Comments

  • Arthur Blake
    Arthur Blake almost 2 years

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