windows 配置nginx環境變量(玩出新花樣)


1.情景展示

在開發過程中,當我們將nginx放置到windows操作系統下時,如何在黑窗口下使用命令,對nginx服務器進行操作?

2.具體分析

說明:如果你不想看推導過程,可以直接看解決方案,我推出的解決方案是全網獨有的,所以說,不看推導過程,這對於個人能力的提升而言沒有任何幫助。

當我們從nginx官網上下載下來windows版本,解壓后,如何通過命令啟動nginx服務器?

打開命令窗口;

輸入:nginx,回車執行;

沒有nginx這個命令?這才是正常的現象。

我們現在來回想一下:java是如何配置環境變量的?nginx是不是也可以像java那樣配置好環境變量直接使用?

說干就干,一起來試一下:

win+r--》輸入:sysdm.cpl

高級--》環境變量;

新建--》變量名可設置為:NGINX_HOME(名稱隨意,但最好遵循見名知意的原則);

變量值設置為:nginx的根目錄。

然后,將其引入到系統變量Path當中。

格式:%變量名%

確定;

重新打開黑窗口,運行nginx命令;

我們可以看到:

第一,nginx命令現在可以使用了,但是啟動失敗;

第二,報錯信息是:在C:\Users\Marydon目錄下,找不到conf/nginx.conf文件。

這是怎么回事?我們明明將nginx的根目錄已經配好了,為啥nginx要去C:\Users\Marydon目錄下找nginx的配置文件?不應該是去nginx的根目錄下找嗎?

原來,Nginx 在啟動時需要找到nginx.conf配置文件, conf-path 是根據相對路徑來找的,什么意思?

意思就是:在哪個目錄下運行nginx命令,nginx就去該目錄下找對應的配置文件,這能找到?能找到就見鬼了!

-?,-h,-v,-V這4個基礎命令,是可以正常使用的;

剩下的命令都需要用到配置文件,如:start nginx、nginx -s stop/reload/reopen/quit等控制命令無法使用,如何解決?

3.解決方案

方案一:在nginx的根目錄下運行

我們來到nginx的安裝目錄,在路徑欄輸入:cmd,並按回車鍵;

將會在此目錄下運行cmd並打開命令窗口;

輸入:nginx,並按回車鍵;

或者,輸入:nginx.exe,並按回車鍵;

或者,輸入:start nginx,並按回車鍵。

沒有報錯信息,說明使用nginx服務器啟動成功。

以上3種啟動方式,打開的都是nginx.exe

相當於:我們雙擊運行nginx.exe。

當然,這種方式,適合偶爾使用的情況。

另一種方式,就是:創建nginx.exe的桌面快捷方式,什么時候需要用了,什么時候打開,當然,這種方式只能用於啟動nginx服務器。

方案二:配置環境變量和創建bat命令

接着第二步分析繼續進行,此時環境變量已經配好。

第一,nginx支持在啟動時指定 conf-path 的絕對路徑,亦或是重新編譯 Nginx 來指定 conf-path。

所以,我們在啟動nginx時,可以通過 -p 命令來指定nginx所在的絕對路徑。

第二,創建bat命令。

使用 bat 來運行 nginx 命令。

新建一個文本文件,將以下代碼粘貼進去:

查看代碼
@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 reload
if "%1"=="reopen" goto reopen
if "%1"=="find" goto find
goto error

:help
nginx -v
echo Usage: nginx2 [-h,help] [-v,version] [start] [stop] [stop -a] [reload] [reopen] [find]
echo=
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 %NGINX_HOME%
exit /B

:stop
if "%2"=="-a" (taskkill /F /IM nginx.exe) else (if "%2"=="" (nginx -s stop -p %NGINX_HOME%) else goto error)
exit /B

:reload
nginx -s reload -p %NGINX_HOME%
exit /B

:reopen
nginx -s reopen -p %NGINX_HOME%
exit /B

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

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

保存,將文件重命名為:nginx2.bat;

將其拷貝到nginx根目錄下;

 

重新打開cmd命令,啟動nginx服務器。

在瀏覽器輸入:localhost進行訪問,進入nginx歡迎頁。

我們可以使用的命令有:

優點:解決了nginx服務器通過相對路徑訪問配置文件的問題。

缺點:但是這樣雖然可操作nginx服務器,但是,不需要配置文件的常用命令,還需要我們使用nginx的命令來操作;

另外,由於啟動、關閉、重啟服務器和重新加載配置文件,是我們自定義的命令,一旦我們換台機器,咱們還需要記住原來的操作命令,相當於需要記憶兩套命令;

這樣一來,其實,與我們的原來目的背道而馳,因為雖然間接實現了,但我們是以喪失原nginx命令為前提的,也就是所謂的:顧此失彼。

雖然,這種方式比較雞肋,但對於window批處理文件的命令還是值得學習的。

bat命令說明:

第一,@echo off 此命令需要將 @ 和 echo off 分開說明:

@讓跟在其后面的命令的執行過程不打印出來;echo off 則讓所有命令的執行過程不打印出來。

第二,goto 與 :  這兩個命令需要配套使用,: 相當於標簽(標簽名稱不區分大小寫),goto 指定跳到那個標簽后面執行它下面的內容;

第三,echo 向命令窗口打印一行指定的字符串,舉例:echo= 打印空的字符串,結果相當於換行。

第四,exit 該命令是退出程序,並且會關閉命令窗口;

exit /B 表示退出程序后不會關閉命令窗口(退回前一個命令所在窗口)。

第五:在bat文件中可以用百分號來引用環境變量,即:%環境變量%;

第六:獲取用戶輸入的數據,用%+數字接收,多個參數中間用空格加以區分。

第六:bat文件名稱是什么,你調用指令的名稱就是什么。

4.再次封裝

其實,只要,我們學習一下bat命令,那自定義封裝實現起來將會得心應手。

基於上面原作者的基礎上進行二次封裝

查看代碼
@echo off
if "%1"=="-?" (goto help) else (if "%1"=="-h" goto help)
if "%1"=="-v" (goto version)
if "%1"=="-V" (goto version2)
if "%1"=="-t" (goto test)
if "%1"=="-T" (goto test2)
if "%1"=="-q" (goto suppress)
if "%1"=="start" goto start
if "%1"=="-s" goto signal
if "%1"=="-p" goto prefix
if "%1"=="-e" goto error
if "%1"=="-c" goto configuration
if "%1"=="-g" goto global
if "%1"=="search" goto search
if "%1"=="kill" goto kill
goto errors

:help
nginx -v
echo Usage: nginx2 [-?,-h] [-v] [-V] [-t] [-T] [-q] [start] [-s signal] [-p prefix] [-e filename] [-c filename] [-g directives] [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   start           : start nginx master process
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   search            : show the nginx master process list(customize include)
echo   kill            : kill all nginx master processes(customize include)
echo=
exit /B

:version
nginx -v
exit /B

:version2
nginx -V
exit /B

:test
nginx -t -p %NGINX_HOME%
exit /B

:test2
nginx -T -p %NGINX_HOME%
exit /B

:suppress
nginx -q -p %NGINX_HOME%
exit /B

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

:signal
nginx -s %2 -p %NGINX_HOME%
exit /B

:prefix
nginx -p %2 -p %NGINX_HOME%
exit /B

:error
nginx -e %2 -p %NGINX_HOME%
exit /B

:configuration
nginx -c %2 -p %NGINX_HOME%
exit /B

:global
nginx -g %2 -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

原生的nginx命令 

 

這樣一來,在nginx可以用的原生指令,我們通過封裝,也能保證封裝后的指令和它基本上完全一致。

與nginx原生指令的區別,在於:

第一,啟動服務器;

原生:start nginx。

派生:nginx2 start。

第二,新增自定義指令;

ngnix2 search:查詢nginx服務進程;

ngnix2 kill:殺死所有nginx服務進程。

第三,指令名稱不同。 

原生:使用nginx+指令;

派生:使用nginx2+指令。

一次簡化

查看代碼

@echo off
if "%1"=="-?" (goto help) else (if "%1"=="-h" goto help)
if "%1"=="-v" (goto vVtTq)
if "%1"=="-V" goto vVtTq
if "%1"=="-t" goto vVtTq
if "%1"=="-T" goto vVtTq
if "%1"=="-q" goto vVtTq
if "%1"=="start" goto start
if "%1"=="-s" goto specg
if "%1"=="-p" goto specg
if "%1"=="-e" goto specg
if "%1"=="-c" goto specg
if "%1"=="-g" goto specg
if "%1"=="search" goto search
if "%1"=="kill" goto kill
goto errors

:help
nginx -v
echo Usage: nginx2 [-?,-h] [-v] [-V] [-t] [-T] [-q] [start] [-s signal] [-p prefix] [-e filename] [-c filename] [-g directives] [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   start           : start nginx master process
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   search            : show the nginx master process list(customize include)
echo   kill            : kill all nginx master processes(customize include)
echo=
exit /B

:vVtTq
nginx %1 -p %NGINX_HOME%
exit /B

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

:specg
nginx %1 %2 -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

二次簡化(終極版本)

@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

用了整整12個小時,終於搞定了。

說難吧,也不難,關鍵是不懂得bat的語法與nginx語法,導致中間走了不少彎路,版本修改和測試了無數次。

這是一條沒人走過的路,我做到了。

5.關於配置環境變量后不生效的問題 

2022年3月9日09:23:50

在win10操作系統下,配置好NGINX_HOME后,nginx命令,時而可以使用,時而無法使用。

通過查看path變量,我們可以看到:

path沒有將%NGINX_HOME%當成了字符串,而不是引用環境變量。

所以,才會導致nginx命令無法生效,怎么辦?

找到path環境變量,選擇“編輯文本”;

確定;

我們可以發現:

%NGINX_HOME%末尾多了一個英文封號;。

在win7操作系統下,我們知道:

在往path當中添加環境變量引用的時候,最后一個環境變量末尾是不能加封號的。

同樣地,我們也要給win10自動添加的封號去掉就可以啦。

重新打開一個命令窗口,進行測試即可。

寫在最后

  哪位大佬如若發現文章存在紕漏之處或需要補充更多內容,歡迎留言!!!

 相關推薦:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM