Nginx 安裝與配置規(guī)則入門詳解
一、nginx 安裝與運(yùn)行 (Mac OS環(huán)境)
1. 安裝 nginx
可通過 Homebrew 可直接安裝:
$brew install nginx
安裝好后,默認(rèn)首頁的文件在 /usr/local/var/www
文件夾下
默認(rèn)的配置文件地址在 /usr/local/etc/nginx/nginx.conf
nginx 默認(rèn)用的 8080 端口,如果發(fā)現(xiàn)端口被占用了(通過 $lsof -i:8080
查看端口占用情況),可以殺掉使用該端口的進(jìn)程($kill 進(jìn)程PID
)?;蛘咝薷?nginx 的默認(rèn)端口(/usr/local/etc/nginx/nginx.conf
)
2. 啟動(dòng) nginx
$brew services start nginx
或者進(jìn)入到目錄 /usr/local/bin
下$./nginx
啟動(dòng)成功后,瀏覽器訪問http://localhost:8080/
,就可以看到 nginx 服務(wù)器返回的靜態(tài)資源了(默認(rèn)是資源/usr/local/var/www/index.html)
3. 停止 nginx
$nginx -s stop
4. 重啟 nginx
$nginx -s reload
5. 查看 nginx 配置路徑信息
$brew info nginx
二、nginx 規(guī)則配置
更多配置可查看
https://www.nginx.com/resources/wiki/start/#pre-canned-configurations
http://nginx.org/en/docs/
http://www.nginx.cn/doc/
1. location
location 語法文章
2. root 與 alias
nginx 中可通過 root 和 alias 指定資源的訪問路徑。
1)root:
location / { root /usr/local/var/www/; index index.html index.htm; }
上面這個(gè)規(guī)則:請求 http://localhost:8080/index.html
這個(gè)地址時(shí),訪問的資源是: /usr/local/var/www/index.html.
請求 http://localhost:8080/test/a.png
這個(gè)地址時(shí),訪問的資源是: /usr/local/var/www/test/a.png.
也就是說,訪問的資源地址其實(shí)是 root 指定的路徑 + location 匹配到的路徑。
2)alias:
alias 即別名,與 root 的匹配規(guī)則稍有不同。
location /a/ { alias /usr/local/var/www/b/; }
上面這個(gè)規(guī)則:請求 http://localhost:8080/a/
這個(gè)地址時(shí),訪問的資源是: /usr/local/var/www/b/index.html.
請求 http://localhost:8080/a/1.gif
這個(gè)地址時(shí),訪問的資源是: /usr/local/var/www/b/1.gif.
也就是說,訪問的資源地址就是 alias 指定的路徑,與 location 匹配到的路徑無關(guān)(會把 location 匹配到的路徑丟掉)。
3)root 與 alias 的區(qū)別:
alias 只能作用在 location 中,而 root 可以存在 server、http 和 location 中。
alias 后面必須要用 “/” 結(jié)束,否則會找不到文件,而 root 則對 “/” 可有可無。
3. try_file
location /test/ { try_files $uri $uri/ /a/1.png; }
try_files 去嘗試到網(wǎng)站目錄讀取用戶訪問的文件,如果第一個(gè)變量存在,就直接返回;不存在則繼續(xù)讀取第二個(gè)變量,如果存在,直接返回;不存在則跳轉(zhuǎn)到第三個(gè)參數(shù)上。
$uri 是 nginx 的一個(gè)變量,存放著用戶訪問的地址。比如訪問http://www.xxx.com/index.html,\$uri就是 /index.html.
$uri/ 代表訪問的是一個(gè)目錄,比如:http://www.xxx.com/hello/test/ ,那么\$uri/ 就是 /hello/test/.
例如上面這條規(guī)則:請求 http://localhost:8080/test/2.png
這個(gè)地址時(shí),try_files 會判斷他是文件,還是一個(gè)目錄,結(jié)果發(fā)現(xiàn)他是文件,與第一個(gè)參數(shù) $uri 變量匹配。然后去到網(wǎng)站目錄下去查找 test/2.png 文件是否存在,如果存在直接讀取返回。如果不存在則跳轉(zhuǎn)到第三個(gè)參數(shù),即返回網(wǎng)站根目錄 + /a/1.png 文件(/usr/local/var/www/a/1.png)。
更多用法:https://www.jb51.net/article/156899.htm
4. rewrite
rewrite 語法
rewrite 功能就是實(shí)現(xiàn) url 重寫以及重定向。
語法rewrite regex replacement [flag];
rewrite只能放在server{}
,location{}
,if{}
中,并且只能對域名后邊的除去傳遞的參數(shù)外的字符串起作用,例如 http://www.xxx.com/a/b/index.html?param=1&u=str
只對 /a/b/index.html 重寫。
rewrite 的執(zhí)行順序:
- 執(zhí)行server塊的rewrite指令
- 執(zhí)行l(wèi)ocation匹配
- 執(zhí)行選定的location中的rewrite指令
flag 標(biāo)志位:
- last : 相當(dāng)于Apache的[L]標(biāo)記,表示完成rewrite
- break : 停止執(zhí)行當(dāng)前虛擬主機(jī)的后續(xù) rewrite 指令集
- redirect : 返回302臨時(shí)重定向,地址欄會顯示跳轉(zhuǎn)后的地址
- permanent : 返回301永久重定向,地址欄會顯示跳轉(zhuǎn)后的地址
location /home/ { rewrite ^/home/test/ http://www.baidu.com; }
上面這個(gè)規(guī)則:訪問 http://localhost:8080/home/test/
這個(gè)地址時(shí),頁面會重定向到 http://www.baidu.com。
一些小tips:
如何 nginx 重定向 url,但不改變?yōu)g覽器中 url 的顯示?
proxy_pass 可指定反向代理
更多用法:https://www.jb51.net/article/134233.htm
三、一些命令行的配置(mac OS)
1. 如何在命令行用 vscode 打開文件
cd /usr/local/bin/ ln -s "/Applications/Visual Studio Code.app/Contents/MacOS/Electron" vscode
其中 /Applications/Visual Studio Code.app/Contents/MacOS/Electron
為 vscode 的可執(zhí)行文件,ln -s 命令就是將其通過軟連接的方式放到 /usr/local/bin/ 目錄下。這樣就可以在命令行的其他地方通過 vscode 命令打開文件了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。