Windows下配置Nginx环境变量,无需在Nginx文件下启动

注,本文是根据某篇博客的文章整理,文章地址:https://www.cnblogs.com/Marydon20170307/p/15944960.html,感谢老哥的整理!同时修复了部分小问题,小弟这里整理下,以方便查阅!

Windows上面使用Nginx工具时,老是需要在Nginx所在目录下去是用,不能想Linux上直接输入命令后使用,只能说很不方便,不过,科技改变生活(其实就是懒),还是有直接在控制台输入命令后使用Nginx的方法的,使用方法如下:

配置Nginx环境变量

将Nginx的文件目录配置在Path环境变量下,如下:

我的Nginx路径为:

1
D:\Program Files\nginx-1.20.2

image-20230117152725236

如果只这样配,Nginx服务是无法启动的,会报如下错误:

使用 start nginx启动服务,窗口会一闪而过,没有Nginx进程,

使用nginx -t检测文件,会报如下错误:

image-20230117153107710

这里报的是文件logs目录下的文件无法打开,以及nginx.conf文件未找到导致的,如果,只想这样使用,需要以如下命令使用

1
$ nginx -t -p "D:\Program Files\nginx-1.20.2"

这里是正常的,得到的回显如下:

image-20230117153728541

但是,正常人谁又记得住Nginx所在目录啊(还是懒!),因此,需要使用一些手段让Nginx更方便的使用!

编写脚本

整理还是借助环境变量来辅助使用Nginx,在电脑上配置环境变量如下:

环境变量名 环境变量值(Nginx安装或解压路径)
NGINX_HOME D:\Program Files\nginx-1.20.2

image-20230117154129062

编写脚本如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
@echo off
if "%1"=="-?" goto help
if "%1"=="-h" goto help
if "%1"=="-v" goto vVtTqspecg
if "%1"=="-V" goto vVtTqspecg
if "%1"=="-t" goto vVtTqspecg
if "%1"=="-T" goto vVtTqspecg
if "%1"=="-q" goto vVtTqspecg
if "%1"=="-s" goto vVtTqspecg
if "%1"=="-p" goto vVtTqspecg
if "%1"=="-e" goto vVtTqspecg
if "%1"=="-c" goto vVtTqspecg
if "%1"=="-g" goto vVtTqspecg
if "%1"=="start" goto start
if "%1"=="search" goto search
if "%1"=="kill" goto kill
goto errors

:help
nginx -v
echo Usage: nginx2 [-?,-h] [-v] [-V] [-t] [-T] [-q]
echo [-s signal] [-p prefix] [-e filename] [-c filename] [-g directives]
echo [start] [search] [kill]
echo=
echo Options:
echo -?,-h : this help
echo -v : show version and exit
echo -V : show version and configure options then exit
echo -t : test configuration and exit
echo -T : test configuration, dump it and exit
echo -q : suppress non-error messages during configuration testing
echo -s signal : send signal to a master process: stop, quit, reopen, reload
echo -p prefix : set prefix path (default: NONE)
echo -e filename : set error log file (default: logs/error.log)
echo -c filename : set configuration file (default: conf/nginx.conf)
echo -g directives : set global directives out of configuration file
echo start : start nginx master process(customize include)
echo search : show the nginx master process list(customize include)
echo kill : kill all nginx master processes(customize include)
echo=
exit /B

:vVtTqspecg
nginx %1 %2 -p "%NGINX_HOME%"
exit /B

:start
start nginx -p "%NGINX_HOME%"
exit /B

:search
tasklist /fi "imagename eq nginx.exe"
exit /B

:kill
taskkill /F /IM nginx.exe
exit /B

:errors
echo nginx2: invalid option: "%1 %2"
echo=
exit /B

保存时修改脚本文件名称为nginx2.bat(注:这里的文件名是后面使用Nginx时的命令,根据个人爱好定义即可!),同时复制到Nginx安装或解压目录下,如:

image-20230117154705315

脚本基本使用的时之前文章的脚本,但是因为我的Nginx路径中有空格,这里修改了一下

脚本的使用

因为控制台里面已经有nginx的命令,这里使用的是nginx2命令操作Nginx服务,相关命令如下:

  • 启动

    1
    $ nginx2 start
  • 检测配置文件

    1
    $ nginx2 -t
  • 查看Nginx进程

    1
    $ nginx2 search
  • 关闭Nginx服务

    1
    $ nginx2 kill
  • 其他命令如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    echo   -?,-h           : this help
    echo -v : show version and exit
    echo -V : show version and configure options then exit
    echo -t : test configuration and exit
    echo -T : test configuration, dump it and exit
    echo -q : suppress non-error messages during configuration testing
    echo -s signal : send signal to a master process: stop, quit, reopen, reload
    echo -p prefix : set prefix path (default: NONE)
    echo -e filename : set error log file (default: logs/error.log)
    echo -c filename : set configuration file (default: conf/nginx.conf)
    echo -g directives : set global directives out of configuration file
    echo start : start nginx master process(customize include)
    echo search : show the nginx master process list(customize include)
    echo kill : kill all nginx master processes(customize include)