2017-06-29 39 views

回答

0

嘗試以下鏈接 Nginx loggging tips

+0

謝謝!,但我認爲這個解決方案適用於centOS,我在windows平臺上配置了nginx。 –

0

Nginx的沒有任何內置模塊rolover日誌在Windows或其它任何平臺上,你可以重新打開nginx的日誌,如果你正在運行使用nginx的nginx.exe,但如果你正在運行使用已安裝的服務,你必須停止服務,然後旋轉日誌,然後再次啓動服務,

使用下面的批處理腳本Windows機器上旋轉nginx的錯誤日誌和訪問日誌nginx的

REM script to rotates nginx logs from java/windows cmd prompt 

REM rolloverNginxLogs.bat [maxFileSize in bytes] [retainCount] 
REM e.g rolloverNginxLogs.bat 10000 5 

@echo off 
setlocal EnableDelayedExpansion 
pushd C:\nginx\logs 

set ERRORLEVEL=0 
set maxFileSize=%1 
set retainCount=%2 
set errorLogFile=error.log 
set accessLogFile=access.log 

set rotateAccessLogFile="" 
set rotateErrorLogFile="" 
set rotateFile="" 

REM set current time, replace " " with "0" for hours less than 10 
set currentTime=%time:~0,2%%time:~3,2%%time:~6,2% 
if %currentTime% LSS 100000 (set currentTime=%currentTime: =0%) 

REM check if access.log file rotation required 
for %%A in (%accessLogFile%) do (
    echo.Size of "%%A" is %%~zA bytes 
    if /I %%~zA GTR %maxFileSize% (
     set rotateAccessLogFile=true 
     set rotateFile=true 
    ) 
) 

REM check if error.log file rotation required 
for %%A in (%errorLogFile%) do (
    echo.Size of "%%A" is %%~zA bytes 
    if /I %%~zA GTR %maxFileSize% (
     set rotateErrorLogFile=true 
     set rotateFile=true 
    ) 
) 

REM if required rotate logs otherwise exit 
if "%rotateFile%" EQU "true" (
    goto ROTATELOG 
) else (
    goto DONE 
) 

:ROTATELOG 
REM check whether the service is running if running then stop service 
for /F "tokens=3 delims=: " %%H in ('sc query "nginx" ^| findstr "STATE"') do (
    if /I "%%H" EQU "RUNNING" (
    net stop "nginx"  
) 
) 

REM rotate error log 
if "%rotateErrorLogFile%" EQU "true" (
    ren error.log error-%DATE%-%currentTime%.log 
    for /f "skip=%retainCount% eol=: delims=" %%F in ('dir /b /o-d /a-d error*.log') do @del "%%F" 
) 

REM rotate access log 
if "%rotateAccessLogFile%" EQU "true" (
    ren access.log access-%DATE%-%currentTime%.log 
    for /f "skip=%retainCount% eol=: delims=" %%F in ('dir /b /o-d /a-d access*.log') do @del "%%F" 
) 

REM check if the service is not running, start service 
for /F "tokens=3 delims=: " %%H in ('sc query "nginx" ^| findstr "STATE"') do (
    if /I "%%H" NEQ "RUNNING" (

    echo starting nginx service 
    net start "nginx" 
) 
) 

:DONE 
popd 
exit %ERRORLEVEL%