首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据文件大小旋转nginx日志

如何根据文件大小旋转nginx日志
EN

Stack Overflow用户
提问于 2017-06-29 14:50:15
回答 2查看 1.2K关注 0票数 0

我无法在nginx中配置日志轮换。我已经使用windows服务包装器在windows机器上安装了nginx服务。我曾尝试使用nginx -s reopen重新打开日志文件,但命令提示错误。

EN

回答 2

Stack Overflow用户

发布于 2017-06-29 15:02:44

尝试使用以下链接Nginx loggging tips

票数 0
EN

Stack Overflow用户

发布于 2017-07-06 20:44:33

在windows或其他平台上,Nginx没有任何内置的rolover模块,如果你使用nginx.exe运行nginx,你可以重新打开nginx日志,但如果你使用已安装的服务运行nginx,你必须停止服务,然后旋转日志,然后重新启动服务。

使用以下批处理脚本在windows机器上轮换nginx错误日志和访问日志

代码语言:javascript
运行
复制
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%
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44818207

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档