Nginx的配置語法靈活,可控制度非常高。在0.7以后的版本中加入了一個try_files指令,配合命名location,可以部分替代原本常用的rewrite配置方式,提高解析效率。
try_files指令說明
try_files指令
語法:try_files file ... uri 或 try_files file ... = code
默認值:無
作用域:server location
其作用是按順序檢查文件是否存在,返回第一個找到的文件或文件夾(結尾加斜線表示為文件夾),如果所有的文件或文件夾都找不到,會進行一個內部重定向到最后一個參數。
需要注意的是,只有最后一個參數可以引起一個內部重定向,之前的參數只設置內部URI的指向。最后一個參數是回退URI且必須存在,否則會出現內部500錯誤。命名的location也可以使用在最后一個參數中。與rewrite指令不同,如果回退URI不是命名的location那么$args不會自動保留,如果你想保留$args,則必須明確聲明。
try_files $uri $uri/ /index.php?q=$uri&$args;
實例分析
示例一
try_files 將嘗試你列出的文件并設置內部文件指向。
例如:
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是否存在,如果不存在著內部重定向到@fallback(@表示配置文件中預定義標記點) 。
你也可以使用一個文件或者狀態碼(=404)作為最后一個參數,如果是最后一個參數是文件,那么這個文件必須存在。
示例二
例如nginx不解析PHP文件,以文本代碼返回
try_files $uri /cache.php @fallback;
因為這個指令設置內部文件指向到 $document_root/cache.php 并返回,但沒有發生內部重定向,因而沒有進行location段處理而返回文本 。
(如果加上index指令可以解析PHP是因為index會觸發一個內部重定向)
示例三
跳轉到變量
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; #檢測文件4.html和5.html,如果存在正常顯示,不存在就去查找@qwe值} location @qwe { rewrite ^/(.*)$ http://www.baidu.com; #跳轉到百度頁面 }
示例四
跳轉指定文件
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; }
示例五
將請求跳轉到后端
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; }}
新聞熱點
疑難解答