Download and install Nginx on Windows, and configure environment variables to use Nginx globally. However, commands such as nginx -t still cannot be opened.
reason:
When Nginx is in use, conf-path is found according to the relative path. The matching path is C:\Users\20210121/conf/nginx.conf, which is completely wrong.
Solution:
Make a donginx.bat (the command changes according to the name, for example, here the command starts with donginx, if the bat name is aaa.bat, then the command starts with aaa) file, and change the nginx life.
1) donginx.bat code
@echo off
if "%1"=="help" (goto help) else (if "%1"=="-h" goto help)
if "%1"=="version" (goto version) else (if "%1"=="-v" goto version)
if "%1"=="start" goto start
if "%1"=="stop" goto stop
if "%1"=="reload" goto reloadmd
if "%1"=="reopen" goto reopen
if "%1"=="find" goto find
goto error
:help
nginx -v
echo Usage: donginx [-h,help] [-v,version] [start] [stop] [stop -a] [reload] [reopen] [find]
echoecho=
echo Options:
echo help,-h : this help
echo version,-v : show current nginx version
echo start : start nginx master process
echo stop : stop the newest nginx master process
echo stop -a : stop all nginx master processes
echo reload : reload configuration
echo reopen : reopen nginx
echo find : show the nginx master process list
echo=
exit /B
:version
nginx -v
exit /B
:start
start nginx -p C:\nginx-1.20.1
exit /B
:stop
if "%2"=="-a" (taskkill /F /IM nginx.exe) else (if "%2"=="" (nginx -s stop -p C:\nginx-1.20.1) else goto error)
exit /B
:reload
nginx -s reload -p C:\nginx-1.20.1
exit /B
:find
tasklist /fi "imagename eq nginx.exe"
exit /B
:error
echo donginx: invalid option: "%1 %2"
echo=
exit /B
explain
- @echo off: This command is divided into @ and echo off. @ makes the execution process of the command following it not printed out, and echo off makes the execution process of all commands not printed out (test it yourself and see the result to understand).
- goto and :: are used together, : is equivalent to a label, and goto specifies to jump to execute after that label.
- echo: Print a line of specified characters to the command window, echo= print an empty string, and the result is equivalent to a newline.
- exit: This command is to exit the program and close the command window (this is not what I want). If /B is specified, the command window will not be closed after exiting the program. /B means to return to the window where the previous command was located.
2) Put donginx.bat in the same directory as nginx.exe
3) In the environment variable system variable > path > new > C:\nginx-1.20.1
4) Any cmd or powershell window can execute donginx + commands
Order:
donginx help,-h : this help
donginx version,-v : show current nginx version
donginx start : start nginx master process
donginx stop : stop the newest nginx master process
donginx stop -a : stop all nginx master processes
donginx reload : reload configuration
donginx reopen : reopen nginx
donginx find : show the nginx master process list