nginx配置選項try_files的用法及說明
一、try_files是nginx中http_core核心模塊所帶的指令
主要是能替代一些rewrite的指令,提高解析效率。
官網(wǎng)的文檔為Module ngx_http_core_module
二、try_files的語法規(guī)則
- 格式1:try_files file ... uri;
- 格式2:try_files file ... =code;
可應(yīng)用的上下文:server,location段
1.try_files的語法解釋
Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the
fileparameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to theurispecified in the last parameter is made
- 關(guān)鍵點(diǎn)1:按指定的file順序查找存在的文件,并使用第一個找到的文件進(jìn)行請求處理
- 關(guān)鍵點(diǎn)2:查找路徑是按照給定的root或alias為根路徑來查找的
- 關(guān)鍵點(diǎn)3:如果給出的file都沒有匹配到,則重新請求最后一個參數(shù)給定的uri,就是新的location匹配
- 關(guān)鍵點(diǎn)4:如果是格式2,如果最后一個參數(shù)是 = 404 ,若給出的file都沒有匹配到,則最后返回404的響應(yīng)碼
2.舉例說明
location /images/ {
root /opt/html/;
try_files $uri $uri/ /images/default.gif;
}比如 請求 127.0.0.1/images/test.gif 會依次查找
- 文件/opt/html/images/test.gif
- 文件夾 /opt/html/images/test.gif/下的index文件
- 請求127.0.0.1/images/default.gif
3.其他注意事項
try-files 如果不寫上 $uri/,當(dāng)直接訪問一個目錄路徑時,并不會去匹配目錄下的索引頁 即 訪問127.0.0.1/images/ 不會去訪問 127.0.0.1/images/index.html
三、其他用法
location / {
try_files /system/maintenance.html
$uri $uri/index.html $uri.html
@mongrel;
}
location @mongrel {
proxy_pass http://mongrel;
}以上中若未找到給定順序的文件,則將會交給location @mongrel處理(相當(dāng)于匹配到了@mongrel來匹配)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx upstream的5種權(quán)重分配方式分享
Nginx upstream的5種權(quán)重分配方式分享,需要的朋友可以參考下2012-09-09
淺談Nginx10m+高并發(fā)內(nèi)核優(yōu)化詳解
這篇文章主要介紹了淺談Nginx10m+高并發(fā)內(nèi)核優(yōu)化詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
Nginx生產(chǎn)環(huán)境平滑升級的實現(xiàn)
本文主要介紹了Nginx生產(chǎn)環(huán)境平滑升級的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
nginx如何實現(xiàn)配置靜態(tài)資源服務(wù)器及防盜鏈
這篇文章主要為大家介紹了nginx實現(xiàn)配置靜態(tài)資源服務(wù)器及防盜鏈步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

