Welcome to my website, have a nice day!
Dream it, Do it, Make it!

Windows下Nginx的启动、停止、重启等命令及使用BAT文件管理NGINX服务

这里以我的安装目录D:\Programs\nginx-1.14.2\为例:

C:\Users\4spaces.org>d:

D:\>cd D:\Programs\nginx-1.14.2\

D:\Programs\nginx-1.14.2>

常用命令

1.检查配置文件是否有效

D:\Programs\nginx-1.14.2>nginx -t -c conf/nginx.conf

2.查看版本

D:\Programs\nginx-1.14.2>nginx -v
nginx version: nginx/1.14.2

3.查看安装编译参数

D:\Programs\nginx-1.14.2>nginx -V
nginx version: nginx/1.14.2
built by cl 16.00.40219.01 for 80x86
built with OpenSSL 1.0.2q  20 Nov 2018
TLS SNI support enabled
configure arguments: --with-cc=cl --builddir=objs.msvc8 --with-debug --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --http-scgi-temp-path=temp/scgi_temp --http-uwsgi-temp-path=temp/uwsgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs.msvc8/lib/pcre-8.42 --with-zlib=objs.msvc8/lib/zlib-1.2.11 --with-select_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_slice_module --with-mail --with-stream --with-openssl=objs.msvc8/lib/openssl-1.0.2q --with-openssl-opt=no-asm --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module

4.启动NGINX服务

D:\Programs\nginx-1.14.2>start nginx

5.重载NGINX服务

D:\Programs\nginx-1.14.2>nginx -s reload

6.停止NGINX服务

D:\Programs\nginx-1.14.2>nginx -s stop

7.杀掉NGINX进程

C:\Users\4spaces.org>taskkill /F /IM nginx.exe > nultaskkill /F /IM nginx.exe > nul

使用批处理命令BAT管理NGINX服务

@echo off
rem 提供Windows下nginx的启动,重启,关闭功能

echo ==================begin========================

cls 
::ngxin 所在的盘符
set NGINX_PATH=D:

::nginx 所在目录
set NGINX_DIR=D:\Programs\nginx-1.14.2\
color 0a 
TITLE Nginx 服务管理

CLS 

echo. 
echo. ** Nginx 管理程序  *** 
echo. 

:MENU 

echo. ***** nginx 进程list ****** 
::tasklist|findstr /i "nginx.exe"
tasklist /fi "imagename eq nginx.exe"

echo. 

    if ERRORLEVEL 1 (
        echo nginx.exe不存在
    ) else (
        echo nginx.exe存在
    )

echo. 
::*************************************************************************************************************
echo. 
    echo.  [1] 启动Nginx  
    echo.  [2] 关闭Nginx  
    echo.  [3] 重启Nginx 
    echo.  [4] 刷新控制台  
    echo.  [5] 重新加载Nginx配置文件
    echo.  [6] 检查测试nginx配置文件
    echo.  [7] 查看nginx version
    echo.  [8] 打开错误日志
    echo.  [9] 删除错误日志
    echo.  [0] 退 出 
echo. 

echo.请输入选择的序号:
set /p ID=
    IF "%id%"=="1" GOTO start 
    IF "%id%"=="2" GOTO stop 
    IF "%id%"=="3" GOTO restart 
    IF "%id%"=="4" GOTO MENU
    IF "%id%"=="5" GOTO reloadConf 
    IF "%id%"=="6" GOTO checkConf 
    IF "%id%"=="7" GOTO showVersion 
    IF "%id%"=="8" GOTO openErrorLog
    IF "%id%"=="9" GOTO delErrorLog
    IF "%id%"=="0" EXIT
PAUSE 

::*************************************************************************************************************
::启动
:start 
    call :startNginx
    GOTO MENU

::停止
:stop 
    call :shutdownNginx
    GOTO MENU

::重启
:restart 
    call :shutdownNginx
    call :startNginx
    GOTO MENU

::检查测试配置文件
:checkConf 
    call :checkConfNginx
    GOTO MENU

::重新加载Nginx配置文件
:reloadConf 
    call :checkConfNginx
    call :reloadConfNginx
    GOTO MENU

::显示nginx版本
:showVersion 
    call :showVersionNginx
    GOTO MENU   

::打开错误日志
:openErrorLog 
    call :openErrorLogNginx
    GOTO MENU   

::删除错误日志
:delErrorLog 
    call :delErrorLogNginx
    GOTO MENU

::*************************************************************************************
::底层
::*************************************************************************************
:shutdownNginx
    echo. 
    echo.关闭Nginx...... 
    taskkill /F /IM nginx.exe > nul
    echo.OK,关闭所有nginx 进程
    goto :eof

:startNginx
    echo. 
    echo.启动Nginx...... 
    IF NOT EXIST "%NGINX_DIR%nginx.exe" (
        echo "%NGINX_DIR%nginx.exe"不存在
        goto :eof
     )

    %NGINX_PATH% 
    cd "%NGINX_DIR%" 

    IF EXIST "%NGINX_DIR%nginx.exe" (
        echo "start '' nginx.exe"
        start "" nginx.exe
    )
    echo.OK
    goto :eof


:checkConfNginx
    echo. 
    echo.检查测试 nginx 配置文件...... 
    IF NOT EXIST "%NGINX_DIR%nginx.exe" (
        echo "%NGINX_DIR%nginx.exe"不存在
        goto :eof
     )

    %NGINX_PATH% 
    cd "%NGINX_DIR%" 
    nginx -t -c conf/nginx.conf

    goto :eof

::重新加载 nginx 配置文件
:reloadConfNginx
    echo. 
    echo.重新加载 nginx 配置文件...... 
    IF NOT EXIST "%NGINX_DIR%nginx.exe" (
        echo "%NGINX_DIR%nginx.exe"不存在
        goto :eof
     )

    %NGINX_PATH% 
    cd "%NGINX_DIR%" 
    nginx -s reload

    goto :eof

::显示nginx版本
:showVersionNginx
    echo. 
    %NGINX_PATH% 
    cd "%NGINX_DIR%" 
    nginx -V
    goto :eof

::打开错误日志
:openErrorLogNginx
    echo. 
    echo.打开错误日志...... 
    %NGINX_PATH% 
    cd "%NGINX_DIR%"  
    start logs/error.log
    goto :eof

::删除错误日志
:delErrorLogNginx
    echo. 
    echo.删除错误日志...... 
    %NGINX_PATH% 
    cd "%NGINX_DIR%"  
    del logs\error.log
    goto :eof

新建startNginx.bat文件,并将上面的代码复制进去,需要使用时,双击BAT运行即可,运行完相应命令,输入0退出即可。

nginx-service-manage-console.jpg

注:如果控制台中文乱码,用记事本打开保存为ANSI编码即可。

nginx-bat-ansi.jpg

感谢@山岚

欢迎到Github完善脚本:https://github.com/aitlp/nginx-service-bat

参考文章:

  1. nginx for Windows
  2. Windows下Nginx的启动、停止、重启等命令
  3. window下Nginx启动bat脚本
赞(0)
未经允许禁止转载:Ddmit » Windows下Nginx的启动、停止、重启等命令及使用BAT文件管理NGINX服务

评论 2

  1. #1

    赞了!可以新增两个功能:1.打开logs里面的error.log文件;2.删除log文件。关键代码:
    ::打开错文日志
    ::openErrorLogsFile
    start .\logs\error.log
    goto :eof
    ::删除错文日志
    ::delErrorLogsFile
    del .\logs\error.log
    goto :eof

    山岚5年前 (2019-09-10)回复
    • 多谢!

      michael5年前 (2019-09-10)回复

登录

找回密码

注册