windows版本的nginx控制器

在windows中直接使用nginx调试程序,增加一个脚本控制程序。

@echo off
chcp 65001 > nul
title Nginx Management Ultimate Script (Windows)

:: Force set variables with quotes, avoid empty value issues
set "NGINX_EXE=nginx.exe"
set "USER_CHOICE="

:: Step 1: Check nginx.exe existence (compatible with all paths)
if not exist "%NGINX_EXE%" (
    echo Error: %NGINX_EXE% not found in current directory!
    echo Please put this script in Nginx root folder.
    pause
    exit /b 1
)

:: Core loop (menu auto return)
:MENU_LOOP
cls

:: Step 2: List current Nginx processes (stable command)
echo ==============================================
echo          Current Nginx Processes
echo ==============================================
tasklist /fi "IMAGENAME eq %NGINX_EXE%" 2>nul
if %ERRORLEVEL% EQU 1 (
    echo No running Nginx processes found.
)
echo ==============================================
echo.

:: Step 3: Display menu (no truncated text, no special characters)
echo          Nginx Management Menu
echo ==============================================
echo  1. Start Nginx (Pre-check)
echo  2. Stop Nginx (Pre-check)
echo  3. Restart Nginx (Stop + Start)
echo  0. Exit Script
echo ==============================================

:: Step 4: Get user input (force clear previous value, avoid residual)
set "USER_CHOICE="
set /p "USER_CHOICE=Please enter number (0-3): "

:: Step 5: Operation logic (USE GOTO INSTEAD OF IF/ELSE IF - core fix for parsing error)
:: This avoids the nested if/else if parsing conflict that causes ". was unexpected"
if "%USER_CHOICE%"=="1" goto START_NGINX
if "%USER_CHOICE%"=="2" goto STOP_NGINX
if "%USER_CHOICE%"=="3" goto RESTART_NGINX
if "%USER_CHOICE%"=="0" goto EXIT_SCRIPT
:: Invalid input
echo.
echo Error: Invalid input! Only 0,1,2,3 are allowed.
goto AFTER_OPERATION

:: ---------------------- Start Nginx Logic ----------------------
:START_NGINX
echo.
echo [Start Nginx] Checking process status...
tasklist /fi "IMAGENAME eq %NGINX_EXE%" 2>nul | find /i "%NGINX_EXE%" >nul
if %ERRORLEVEL% EQU 0 (
    echo Info: Nginx is already running! No operation performed.
) else (
    echo Info: Nginx not running. Starting now...
    start /b "" "%NGINX_EXE%"
    if %ERRORLEVEL% EQU 0 (
        echo Success: Nginx started successfully!
    ) else (
        echo Error: Failed to start Nginx! (Port occupied or permission denied)
    )
)
goto AFTER_OPERATION

:: ---------------------- Stop Nginx Logic ----------------------
:STOP_NGINX
echo.
echo [Stop Nginx] Checking process status...
tasklist /fi "IMAGENAME eq %NGINX_EXE%" 2>nul | find /i "%NGINX_EXE%" >nul
if %ERRORLEVEL% EQU 0 (
    echo Info: Nginx found. Killing processes...
    taskkill /f /im "%NGINX_EXE%" >nul 2>nul
    :: Verify stop
    tasklist /fi "IMAGENAME eq %NGINX_EXE%" 2>nul | find /i "%NGINX_EXE%" >nul
    if %ERRORLEVEL% EQU 1 (
        echo Success: Nginx stopped successfully!
    ) else (
        echo Error: Failed to kill Nginx processes!
    )
) else (
    echo Info: Nginx is not running! No operation performed.
)
goto AFTER_OPERATION

:: ---------------------- Restart Nginx Logic ----------------------
:RESTART_NGINX
echo.
echo [Stop Nginx] Checking process status...
tasklist /fi "IMAGENAME eq %NGINX_EXE%" 2>nul | find /i "%NGINX_EXE%" >nul
if %ERRORLEVEL% EQU 0 (
    echo Info: Nginx found. Killing processes...
    taskkill /f /im "%NGINX_EXE%" >nul 2>nul
    :: Verify stop
    tasklist /fi "IMAGENAME eq %NGINX_EXE%" 2>nul | find /i "%NGINX_EXE%" >nul
    if %ERRORLEVEL% EQU 1 (
        echo Success: Nginx stopped successfully!
    ) else (
        echo Error: Failed to kill Nginx processes!
    )
) else (
    echo Info: Nginx is not running! No operation performed.
)
echo.
echo [Start Nginx] Checking process status...
tasklist /fi "IMAGENAME eq %NGINX_EXE%" 2>nul | find /i "%NGINX_EXE%" >nul
if %ERRORLEVEL% EQU 0 (
    echo Info: Nginx is already running! No operation performed.
) else (
    echo Info: Nginx not running. Starting now...
    start /b "" "%NGINX_EXE%"
    if %ERRORLEVEL% EQU 0 (
        echo Success: Nginx started successfully!
    ) else (
        echo Error: Failed to start Nginx! (Port occupied or permission denied)
    )
)
goto AFTER_OPERATION

:: ---------------------- After Operation (Return to Menu) ----------------------
:AFTER_OPERATION
echo.
:: echo Press any key to return to menu...
:: pause >nul
goto MENU_LOOP

:: ---------------------- Exit Script ----------------------
:EXIT_SCRIPT
echo.
echo Exiting script...
pause >nul
exit /b 0