nginx中的try_files指令解释
生活随笔
收集整理的這篇文章主要介紹了
nginx中的try_files指令解释
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
try_files 指令的官方介紹比較讓人摸不著頭腦,經(jīng)網(wǎng)上一番總結(jié)查看,try_files最核心的功能是可以替代rewrite。
try_files 語法: try_files file ... uri ? ?或 ?try_files ?file ... = code 默認(rèn)值: 無 作用域: server location Checks for the existence of files in order, and returns the first file that is found. A trailing slash indicates a directory - $uri /. In the event that no file is found, an internal redirect to the last parameter is invoked. Do note that only the last parameter causes an internal redirect, former ones just sets the internal URI pointer. The last parameter is the fallback URI and *must* exist, or else an internal error will be raised. Named locations can be used. Unlike with rewrite, $args are not automatically preserved if the fallback is not a named location. If you need args preserved, you must do so explicitly: try_files $uri $uri/ /index.php?q=$uri&$args; 按順序檢查文件是否存在,返回第一個找到的文件。結(jié)尾的斜線表示為文件夾 -$uri/。如果所有的文件都找不到,會進(jìn)行一個內(nèi)部重定向到最后一個參數(shù)。 務(wù)必確認(rèn)只有最后一個參數(shù)可以引起一個內(nèi)部重定向,之前的參數(shù)只設(shè)置內(nèi)部URI的指向。 最后一個參數(shù)是回退URI且必須存在,否則將會出現(xiàn)內(nèi)部500錯誤。 命名的location也可以使用在最后一個參數(shù)中。與rewrite指令不同,如果回退URI不是命名的location那么$args不會自動保留,如果你想保留$args,必須明確聲明。 try_files $uri $uri/ /index.php?q=$uri&$args; 實例分析 try_files 將嘗試你列出的文件并設(shè)置內(nèi)部文件指向。 例如: try_files /app/cache/ $uri @fallback; 和 ?index index.php index.html; 它將檢測$document_root/app/cache/index.php,$document_root/app/cache/index.html 和 $document_root$uri是否存在,如果不存在著內(nèi)部重定向到 @fallback 。 你也可以使用一個文件或者狀態(tài)碼 (=404)作為最后一個參數(shù),如果是最后一個參數(shù)是文件,那么這個文件必須存在。 需要明確的是出最后一個參數(shù)外 try_files 本身不會因為任何原因產(chǎn)生內(nèi)部重定向。 例如nginx不解析PHP文件,以文本代碼返回 try_files $uri /cache.php @fallback; 因為這個指令設(shè)置內(nèi)部文件指向到 $document_root/cache.php 并返回,但沒有發(fā)生內(nèi)部重定向,因而沒有進(jìn)行l(wèi)ocation段處理而返回文本 。 (如果加上index指令可以解析PHP是因為index會觸發(fā)一個內(nèi)部重定向) 轉(zhuǎn)載: http://www.2cto.com/os/201210/164157.html .htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php [QSA,L] 在 Nginx 下, 添加以下聲明:location / {try_files $uri $uri/ /index.php;} https://git.oschina.net/os621/ROCBOSS轉(zhuǎn)載于:https://www.cnblogs.com/wawahaha/p/4641691.html
總結(jié)
以上是生活随笔為你收集整理的nginx中的try_files指令解释的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ORACLE经常使用的命令
- 下一篇: [HAOI2008][BZOJ1042]