Nginx的try_files指令使用实例
Nginx的配置语法灵活,可控制度非常高。在0.7以后的版本中加入了一个try_files指令,配合命名location,可以部分替代原本常用的rewrite配置方式,提高解析效率。
try_files指令说明
try_files指令
语法:try_filesfile...uri或try_filesfile...=code
默认值:无
作用域:serverlocation
其作用是按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有的文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。
需要注意的是,只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部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; indexindex.phpindex.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{
listen8000;
server_name192.168.119.100;
roothtml;
indexindex.htmlindex.php;
location/abc{
try_files/4.html/5.html@qwe;#检测文件4.html和5.html,如果存在正常显示,不存在就去查找@qwe值
}
location@qwe{
rewrite^/(.*)$http://www.baidu.com;#跳转到百度页面
}
示例四
跳转指定文件
server{
listen8000;
server_name192.168.119.100;
roothtml;
indexindex.phpindex.html;
location/abc{
try_files/4.html/5.html/6.html;
}
示例五
将请求跳转到后端
upstreamtornado{
server127.0.0.1:8001;
}
server{
server_nameimike.me;
return301$scheme://www.imike.me$request_uri;
}
server{
listen80;
server_namewww.imike.me;
root/var/www/www.imike.me/V0.3/www;
indexindex.htmlindex.htm;
try_files$uri@tornado;
location@tornado{
proxy_pass_headerServer;
proxy_set_headerHost$http_host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Scheme$scheme;
proxy_passhttp://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造成死循环。结果出现500InternalServerError
location~.*\.(gif|jpg|jpeg|png)${
root/web/wwwroot;
try_files/static/$uri$uri404;
}
这样才会先检查/web/wwwroot/static/test.jpg是否存在,不存在就取/web/wwwroot/test.jpg再不存在则返回404notfound
常见错误二
Nginxtry_files$query_string为空的解决办法
server{
listen80;
server_namelocalhost.dev;
indexindex.phpindex.htmlindex.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_pass127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;includefastcgi_params;
}
location~*^/(css|img|js|flv|swf|download)/(.+)${
root$root_path;
}
location~/\.ht{
denyall;
}
}
发现PHP无法获取$_GET信息
try_files$uri$uri//index.php;
改为
try_files$uri$uri//index.php?$query_string;
即可解决,以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。