Windows 配置nginx管理面板和快速开启,关闭命令 bat
nginx管理程序.bat
NGINX_DIR 替换为自己的安装路径
@echo off
chcp 65001
rem 提供Windows下nginx的启动,重启,关闭功能
echo ==================begin========================
cls
::ngxin 所在的盘符
set NGINX_PATH=D:
::nginx 所在目录
set NGINX_DIR=D:\ENV\nginx-1.25.3\
color 0a
TITLE Nginx 管理程序增强版
CLS
echo.
echo. ** Nginx 管理程序 ***
echo. *** create 2023-12-10 ***
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. [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%"=="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
::*************************************************************************************
::底层
::*************************************************************************************
: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``
快速开启nginx.bat
替换成自己的nginx路径
@echo off
chcp 65001
D:
::进入nginx安装目录
cd D:\ENV\nginx-1.25.3
::start nginx -c ./conf/nginx.conf
rem 如果启动前已经启动nginx并记录下pid文件,会kill指定进程
nginx.exe -s stop
rem 测试配置文件语法正确性
nginx.exe -t -c conf/nginx.conf
rem 显示版本信息
nginx.exe -v
rem 按照指定配置去启动nginx
::nginx.exe -c conf/nginx.conf
::启动nginx
start nginx.exe
echo ================= 项目列表 =================
echo;
echo;
echo ======= 项目1
echo http://192.168.0.200:8001/
echo;
echo;
echo ==========================================
echo;
echo;
echo ************ 启动成功!!! (此窗口可关闭)************
echo;
echo;
::按任意键继续
pause
快速关闭nginx.bat
@echo off
taskkill /f /im nginx.exe
nginx.conf配置示例
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
################ 项目1
server {
listen 90000;
server_name localhost;
# 前端
location / {
root html/projectname/;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
# 后端
location ^~ /api/ {
proxy_pass http://192.168.0.100:8038/;
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ^~ /admin/ {
proxy_pass http://192.168.0.101:8283/admin/;
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
################ 项目2
server {
listen 90000;
server_name localhost;
# 前端
location / {
root html/projectname2/;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
# 后端
location ^~ /api/ {
proxy_pass http://192.168.0.100:8038/;
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ^~ /admin/ {
proxy_pass http://192.168.0.101:8283/admin/;
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}