简体   繁体   中英

log rotation for nginx on windows

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

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"

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 .

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

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

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.

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

@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

I use NSSM to manage Nginx on Windows (checked on WhiteHorse ). Although this utility has built-in rotate functionality, it probably doesn't work properly. The documentation warns about risk of online rotation.

Two service entries should be configured:

  1. Nginx, and

  2. Nginx Rotate Logs.

The solution doesn't support rotation based on file size or file age. It's possible to setup schedule to rotate logs using nginx -s reopen .

First ,

create Nginx Rotate Logs service, using NSSM (note bene: you may use the same command to create main Nginx service).

Command below will create service entry:

nssm install "Nginx Rotate" C:\tools\nginx-1.21.6\nginx.exe

Next lines will configure service step-by-step, oneliner is not available here.

nssm set "Nginx Rotate" AppParameters "-s reopen"
nssm set "Nginx Rotate" AppExit Default Exit
nssm set "Nginx Rotate" AppStdout C:\tools\nginx-1.21.6\logs\access.log
nssm set "Nginx Rotate" AppStderr C:\tools\nginx-1.21.6\logs\error.log
nssm set "Nginx Rotate" AppRedirectHook 1
nssm set "Nginx Rotate" AppRotateFiles 1
nssm set "Nginx Rotate" AppRotateOnline 1
nssm set "Nginx Rotate" AppTimestampLog 1
nssm set "Nginx Rotate" Description "Some description"
nssm set "Nginx Rotate" DisplayName "Nginx Rotate Logs"
nssm set "Nginx Rotate" ObjectName DOMAIN\user "password"
nssm set "Nginx Rotate" Start SERVICE_DEMAND_START
nssm set "Nginx Rotate" Type SERVICE_WIN32_OWN_PROCESS

Then edit service:

nssm edit "Nginx Rotate"

At the last tab you need set:

Event = Log rotation + Before online log rotation

as shown in the picture below.

在此处输入图像描述

As a result, this service will be in manual mode. After start it rotates logs and stops.

Second ,

create a schedule task.

schtasks /create /sc daily /st 03:30 /tn "Nginx Rotate Logs" /tr "C:\Program Files\PowerShell\7\pwsh.exe -command start-service 'Nginx Rotate Logs'"

Important ,

use the same user both for Nginx, Nginx Rotate Logs and Schedule Task.

Usage :

While main Nginx is operating, the schedule task runs once a day, rotating files.

Aux :

You may add a daily task (or add to hook tab) to compress and purge old logs, for example:

# install PSCX module
Install-Module -Name Pscx -AllowPrerelease -AllowClobber

# compress
Get-ChildItem 'C:\tools\nginx-1.21.6\logs\*.log' | Where {$_.lastwritetime -lt (Get-Date).AddDays(-1)} | write-zip

# purge yesterday zip
Get-ChildItem 'C:\tools\nginx-1.21.6\logs\*.zip' | Where {$_.lastwritetime -lt (Get-Date).AddDays(-2)} | Remove-Item -Force
@echo off

net stop nginx


for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"

echo move %~dp0\Access.log %~dp0\Old\Access_%fullstamp%.log
move %~dp0\Access.log %~dp0\Old\Access_%fullstamp%.log

echo move %~dp0\Error.log %~dp0\Old\Error_%fullstamp%.log
move %~dp0\Error.log %~dp0\Old\Error_%fullstamp%.log

net start nginx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM