在項(xiàng)目開(kāi)發(fā)中經(jīng)常會(huì)遇到nginx配置文件中,會(huì)有很多條location配置,卻讀不懂其中的含義而煩惱
Location是nginx中的塊級(jí)指令(block directive),通過(guò)配置Location指令塊,可以決定客戶(hù)端發(fā)過(guò)來(lái)的請(qǐng)求URI如何處理(是映射到本地文件還是轉(zhuǎn)發(fā)出去)及被哪個(gè)location處理。
Location基本語(yǔ)法
修飾符(modifier) location [ = | ~ | ~* | ^~ ] uri { ... } location根據(jù)不同的修飾符可以分為兩大類(lèi) 前綴location(prefix location): 無(wú)修飾符的普通location 帶=的精準(zhǔn)匹配location 帶^~的非正則表達(dá)式location 正則表達(dá)式location(regular expressions location): ~ 區(qū)分大小寫(xiě)的正則location ~* 不區(qū)分大小寫(xiě)的正則location
?location的匹配示例:
ocation = / { [ configuration A ] } #用戶(hù)請(qǐng)求"/"時(shí),匹配A,例如:www.pcm.com/ location / { [ configuration B ] } #當(dāng)用戶(hù)請(qǐng)求"/index.html"時(shí),匹配B,例如:www.pcm.com/index.html location /documents/ { [ configuration C ] } #當(dāng)用戶(hù)請(qǐng)求"/documents/"時(shí),匹配C,例如:www.pcm.com/documents/index.html location ^~ /images/ { [ configuration D ] } #當(dāng)用戶(hù)請(qǐng)求"/images/"時(shí),匹配D,:www.pcm.com/images/1.jpg location ~* .(gif|jpg|jpeg)$ { [ configuration E ] } #當(dāng)用戶(hù)請(qǐng)求".gif|.jpg|.jpeg"時(shí),匹配E,例如:www.pcm.com/documents/1.jpg #上面的反斜杠是轉(zhuǎn)義字符,$的意思是結(jié)尾
下面是一個(gè)location配置參考,以及不同的請(qǐng)求uri到達(dá)nginx時(shí)會(huì)執(zhí)行的規(guī)則
location / { [ configuration B ] } location /documents/ { [ configuration C ] } location ^~ /images/ { [ configuration D ] } location ~* .(gif|jpg|jpeg)$ { [ configuration E ] } 請(qǐng)求URI 執(zhí)行的規(guī)則 / A /index.html B /documents/document.html C /images/1.gif D /documents/1.jpg E
更多Nginx相關(guān)技術(shù)文章,請(qǐng)?jiān)L問(wèn)Nginx使用教程欄目進(jìn)行學(xué)習(xí)!
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載。
THE END