try_files 指令的官方介绍比较让人摸不着头脑,经网上一番总结查看,try_files最核心的功能是可以替代rewrite。

 
try_files
 
语法: try_files file ... uri    或  try_files  file ... = code
 
默认值: 无
 
作用域: 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;
按顺序检查文件是否存在,返回第一个找到的文件。结尾的斜线表示为文件夹 -$uri/。如果所有的文件都找不到,会进行一个内部重定向到最后一个参数。
 
务必确认只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部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)作为最后一个参数,如果是最后一个参数是文件,那么这个文件必须存在。
 
需要明确的是出最后一个参数外 try_files 本身不会因为任何原因产生内部重定向。
 
 
 
 
 
例如nginx不解析PHP文件,以文本代码返回
 
try_files $uri /cache.php @fallback;
因为这个指令设置内部文件指向到 $document_root/cache.php 并返回,但没有发生内部重定向,因而没有进行location段处理而返回文本 。
 
(如果加上index指令可以解析PHP是因为index会触发一个内部重定向)
转载:
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

nginx中的try_files指令解释的更多相关文章

  1. 干货 | 一文彻底读懂nginx中的location指令

    一个执着于技术的公众号 Nginx系列导读 给小白的 Nginx 10分钟入门指南 Nginx编译安装及常用命令 完全卸载nginx的详细步骤 Nginx 配置文件详解 一文带你读懂Nginx反向代理 ...

  2. Nginx中的rewrite指令

    转自:http://www.76ku.cn/articles/archives/317 rewite.在server块下,会优先执行rewrite部分,然后才会去匹配location块server中的 ...

  3. Nginx中的rewrite指令(break,last,redirect,permanent)

    rewite 在server块下,会优先执行rewrite部分,然后才会去匹配location块 server中的rewrite break和last没什么区别,都会去匹配location,所以没必要 ...

  4. Nginx中的一些匹配顺序

    Nginx中经常需要做各种配置,总结如下: 1.server_name配置 nginx中的server_name指令主要用于配置基于名称虚拟主机,同一个Nginx虚拟主机中,可以绑定多个server_ ...

  5. 在nginx中配置如何防止直接用ip访问服务器web server及server_name特性讲解

    看了很多nginx的配置,好像都忽略了ip直接访问web的问题,不利于SEO优化,所以我们希望可以避免直接用IP访问网站,而是域名访问,具体怎么做呢,看下面. 官方文档中提供的方法: If you d ...

  6. Nginx中server_name 参数详解

    Nginx中的server_name指令主要用于配置基于名称的虚拟主机,server_name指令在接到请求后的匹配顺序分别为: 1.准确的server_name匹配,例如: server { lis ...

  7. Nginx的try_files指令和命名location使用实例

    Nginx的配置语法灵活,可控制度非常高.在0.7以后的版本中加入了一个try_files指令,配合命名location,可以部分替代原本常用的rewrite配置方式,提高解析效率. 下面是一个使用实 ...

  8. Nginx try_files 指令

    Nginx try_files 指令 按顺序检查文件是否存在,返回第一个找到的文件.结尾的斜线表示为文件夹 -$uri/.如果所有的文件都找不到,会进行一个内部重定向到最后一个参数. 务必确认只有最后 ...

  9. Nginx之 try_files 指令

    location / { try_files $uri $uri/ /index.php; } 当用户请求 http://localhost/example 时,这里的 $uri 就是 /exampl ...

随机推荐

  1. 字符编码的故事(ASCII,ANSI,Unicode,Utf-8区别)转载

    http://www.imkevinyang.com/2009/02/字符编解码的故事(ascii,ansi,unicode,utf-8区别).html 很久很久以前,有一群人,他们决定用8个可以开合 ...

  2. 5.6 太多分区引起OOM

    一个月之前,Scott和同事们发现公司有一个MySQL MHA集群的master(假设master机器名为hostA)每隔一周左右就会挂一次(指MySQL挂掉),在几周内,MHA来回切了好几次. 按照 ...

  3. Linux MySQL-Workbench安装

    yum install pcre-devel libglade2-devel gtkmm24-devel libgnome-devel lua-devel libzip-devel mysql-dev ...

  4. VB.NET中使用代表对方法异步调用

    按照我们常规的思维方式,计算机应该是干完一件事,然后再干下一件.用术语来说,这种执行任务的方式叫做同步执行(Synchronous Execution).既然这样,那么为什么要引入异步执行的概念呢? ...

  5. CentOS 6.x安装gcc 4.8/4.9/5.2

    1.gcc 4.8 cd /etc/yum.repos.d wget http://people.centos.org/tru/devtools-2/devtools-2.repo -gcc -bin ...

  6. UVA 11021 - Tribles(概率)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=481&page=s ...

  7. scala学习笔记:理解并行集合par

    scala> (1 to 5).foreach(println(_)) 1 2 3 4 5 scala> (1 to 5).par.foreach(println(_)) 3 1 4 2 ...

  8. Android调用.net的webservice服务器接收参数为空的情况

    问题描述:安卓开发中,用Android调用.net开发的wenService时候,从Android客户端传递参数到服务器端,服务器端接收为空 解决方法: 1.设置envelope.dotNet = t ...

  9. DEVMODE 结构体

    typedef struct _devicemode { TCHAR dmDeviceName[CCHDEVICENAME]; //打印机(显示设备)名称 WORD dmSpecVersion; WO ...

  10. Oracle 递归函数与拼接

    ) name FROM table tb START ) CONNECT BY PRIOR ID=mt.parentid ; 在Oracle中,SYS_CONNECT_BY_PATH函数主要作用是可以 ...