Nginx的try_files指令使用實(shí)例

發(fā)布時(shí)間:2024-03-14
nginx的配置語(yǔ)法靈活,可控制度非常高。在0.7以后的版本中加入了一個(gè)try_files指令,配合命名location,可以部分替代原本常用的rewrite配置方式,提高解析效率。
try_files指令說(shuō)明
try_files指令語(yǔ)法:try_files file … uri 或 try_files file … = code默認(rèn)值:無(wú)作用域:server location其作用是按順序檢查文件是否存在,返回第一個(gè)找到的文件或文件夾(結(jié)尾加斜線(xiàn)表示為文件夾),如果所有的文件或文件夾都找不到,會(huì)進(jìn)行一個(gè)內(nèi)部重定向到最后一個(gè)參數(shù)。
需要注意的是,只有最后一個(gè)參數(shù)可以引起一個(gè)內(nèi)部重定向,之前的參數(shù)只設(shè)置內(nèi)部uri的指向。最后一個(gè)參數(shù)是回退uri且必須存在,否則會(huì)出現(xiàn)內(nèi)部500錯(cuò)誤。命名的location也可以使用在最后一個(gè)參數(shù)中。與rewrite指令不同,如果回退uri不是命名的location那么$args不會(huì)自動(dòng)保留,如果你想保留$args,則必須明確聲明。
try_files $uri $uri/ /index.php?q=$uri&$args;
實(shí)例分析
示例一
try_files 將嘗試你列出的文件并設(shè)置內(nèi)部文件指向。
例如:
try_files /app/cache/ $uri @fallback; index index.php index.html;它將檢測(cè)$document_root/app/cache/index.php,$document_root/app/cache/index.html 和 $document_root$uri是否存在,如果不存在著內(nèi)部重定向到@fallback(@表示配置文件中預(yù)定義標(biāo)記點(diǎn)) 。
你也可以使用一個(gè)文件或者狀態(tài)碼(=404)作為最后一個(gè)參數(shù),如果是最后一個(gè)參數(shù)是文件,那么這個(gè)文件必須存在。
示例二
例如nginx不解析php文件,以文本代碼返回
try_files $uri /cache.php @fallback;
因?yàn)檫@個(gè)指令設(shè)置內(nèi)部文件指向到 $document_root/cache.php 并返回,但沒(méi)有發(fā)生內(nèi)部重定向,因而沒(méi)有進(jìn)行l(wèi)ocation段處理而返回文本 。
(如果加上index指令可以解析php是因?yàn)閕ndex會(huì)觸發(fā)一個(gè)內(nèi)部重定向)
示例三
跳轉(zhuǎn)到變量
server { listen 8000; server_name 192.168.119.100; root html; index index.html index.php; location /abc { try_files /4.html /5.html @qwe; #檢測(cè)文件4.html和5.html,如果存在正常顯示,不存在就去查找@qwe值 } location @qwe { rewrite ^/(.*)$ http://www.baidu.com; #跳轉(zhuǎn)到百度頁(yè)面 }示例四
跳轉(zhuǎn)指定文件
server { listen 8000; server_name 192.168.119.100; root html; index index.php index.html; location /abc { try_files /4.html /5.html /6.html; }示例五
將請(qǐng)求跳轉(zhuǎn)到后端
upstream tornado { server 127.0.0.1:8001; } server { server_name imike.me; return 301 $scheme://www.imike.me$request_uri; } server { listen 80; server_name www.imike.me; root /var/www/www.imike.me/v0.3/www; index index.html index.htm; try_files $uri @tornado; location @tornado { proxy_pass_header server; proxy_set_header host $http_host; proxy_set_header x-real-ip $remote_addr; proxy_set_header x-scheme $scheme; proxy_pass http://tornado; } }常見(jiàn)錯(cuò)誤
常見(jiàn)錯(cuò)誤一
try_files 按順序檢查文件是否存在,返回第一個(gè)找到的文件,至少需要兩個(gè)參數(shù),但最后一個(gè)是內(nèi)部重定向也就是說(shuō)和rewrite效果一致,前面的值是相對(duì)$document_root的文件路徑。也就是說(shuō)參數(shù)的意義不同,甚至可以用一個(gè)狀態(tài)碼 (404)作為最后一個(gè)參數(shù)。如果不注意會(huì)有死循環(huán)造成500錯(cuò)誤。
location ~.*\.(gif|jpg|jpeg|png)$ { root /web/wwwroot; try_files /static/$uri $uri; }原意圖是訪(fǎng)問(wèn)http://example.com/test.jpg時(shí)先去檢查/web/wwwroot/static/test.jpg是否存在,不存在就取/web/wwwroot/test.jpg
但由于最后一個(gè)參數(shù)是一個(gè)內(nèi)部重定向,所以并不會(huì)檢查/web/wwwroot/test.jpg是否存在,只要第一個(gè)路徑不存在就會(huì)重新向然后再進(jìn)入這個(gè)location造成死循環(huán)。結(jié)果出現(xiàn)500 internal server error
location ~.*\.(gif|jpg|jpeg|png)$ { root /web/wwwroot; try_files /static/$uri $uri 404; }這樣才會(huì)先檢查/web/wwwroot/static/test.jpg是否存在,不存在就取/web/wwwroot/test.jpg再不存在則返回404 not found
常見(jiàn)錯(cuò)誤二
nginx try_files $query_string為空的解決辦法
server { listen 80; server_name localhost.dev; index index.php index.html index.htm; set $root_path '/var/www/phalcon/public'; root $root_path; location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param script_filename $document_root$fastcgi_script_name; include fastcgi_params; } location ~* ^/(css|img|js|flv|swf|download)/(.+)$ { root $root_path; } location ~ /\.ht { deny all; } }發(fā)現(xiàn)php無(wú)法獲取$_get信息
try_files $uri $uri/ /index.php;
改為
try_files $uri $uri/ /index.php?$query_string;
即可解決
上一個(gè):迷茫!錯(cuò)過(guò)了上?;諠t(yī)用針管韌性測(cè)試儀又要等1年
下一個(gè):970evoplus值得買(mǎi)嗎(970evo970plus)

如何用手機(jī)做電腦的手寫(xiě)板(手機(jī)連接電腦做手寫(xiě)板)
什么是公司解散清算中的法律責(zé)任
什么是分散網(wǎng)絡(luò)化制造
為什么運(yùn)行中電壓互感器二次回路不允許短路?
火的手游排行前10名(火的十大手游)
微型盆景管理須知六法則!
伙伴云使用教程(伙伴云網(wǎng)址)
鋼結(jié)構(gòu)設(shè)計(jì)制圖分幾個(gè)階段?
拉力試驗(yàn)機(jī)測(cè)試結(jié)果計(jì)算公式
zcc66099同步升壓替代sgm66099社區(qū)
十八禁 网站在线观看免费视频_2020av天堂网_一 级 黄 色 片免费网站_绝顶高潮合集Videos