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;
}
}
常見錯誤
常見錯誤一
try_files 按順序檢查文件是否存在,返回第一個找到的文件,至少需要兩個參數,但最后一個是內部重定向也就是說和rewrite效果一致,前面的值是相對$document_root的文件路徑。也就是說參數的意義不同,甚至可以用一個狀態碼 (404)作為最后一個參數。如果不注意會有死循環造成500錯誤。
location ~.*\.(gif|jpg|jpeg|png)$ {
root /web/wwwroot;
try_files /static/$uri $uri;
}
原意圖是訪問http://example.com/test.jpg時先去檢查/web/wwwroot/static/test.jpg是否存在,不存在就取/web/wwwroot/test.jpg
但由于最后一個參數是一個內部重定向,所以并不會檢查/web/wwwroot/test.jpg是否存在,只要第一個路徑不存在就會重新向然后再進入這個location造成死循環。結果出現500 Internal Server Error
location ~.*\.(gif|jpg|jpeg|png)$ {
root /web/wwwroot;
try_files /static/$uri $uri 404;
}
這樣才會先檢查/web/wwwroot/static/test.jpg是否存在,不存在就取/web/wwwroot/test.jpg再不存在則返回404 not found
常見錯誤二
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;
}
}
發現PHP無法獲取$_GET信息
try_files $uri $uri/ /index.php;
改為
try_files $uri $uri/ /index.php?$query_string;
即可解決,以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。