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

转载于:https://www.cnblogs.com/wawahaha/p/4641691.html

nginx中的try_files指令解释相关推荐

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

    转载自:Nginx中的rewrite指令(break,last,redirect,permanent) rewite 在server块下,会优先执行rewrite部分,然后才会去匹配location块 ...

  2. nginx中的location指令

    1.location 介绍 location是Nginx中的块级指令(block directive),location指令的功能是用来匹配不同的url请求,进而对请求做不同的处理和响应,这其中较难理 ...

  3. Nginx中常用的指令配置详解

    rewrite指令的作用:是做一些访问匹配规则的判断,从而实现相应的操作. location / { proxy_pass http://192.168.1.5 if (-d $request_fil ...

  4. nginx php permanent,Nginx中的rewrite指令详解(break,last,redirect,permanent)

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

  5. Nginx中server_name 参数详解

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

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

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

  7. Nginx try_files 指令

    官方文档 http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files try_files 用于指定文件的查找规则,可以配置多个规 ...

  8. [Linux] nginx的try_files指令实现隐藏index.php的重写

    1.nginx的try_files指令 ,核心功能是替代rewrite,并且比rewrite更强大的是可以按顺序查找文件是否存在,如果文件都找不到才会执行最后的重定向 解决的问题是,如果一个网站的部署 ...

  9. Nginx核心模块——HTTP中的配置指令location和rewrite介绍

    文章目录 一.正则表达式在学习 1.1 常用的Nginx 正则表达式 二.访问路由location 2.1 location的分类 2.2 location 常用的匹配规则: 2.3 location ...

最新文章

  1. 【PHPExcel】生成Excel文件
  2. 怎样才能快速批量绑定MAC与IP地址
  3. android安装python opencv_MacLinux环境在Android Studio中安装OpenCV
  4. qt带小数点的数字串显示,Qt-自定义小数点和千位分隔符
  5. highcharts图表高级入门之polar:极地图的基本配置以及一些关键配置说明
  6. leetcode 1. 两数之和 思考分析
  7. linux设备模型之kset/kobj/ktype分析
  8. 【Git】Git处理新Repository无法push的问题
  9. Hadoop 系列之 1.0 和2.0 架构
  10. 苹果AirPods大受欢迎:与iPod鼎盛时期相当
  11. 电脑黑屏的原因有哪些
  12. Nginx+Tomcat负载均衡群集
  13. Guthrie QA-CAD2019(CAD绘图修订管理工具)A.04中文版
  14. 新颖的计算机科学与技术毕业设计题目
  15. 某些型号的Comba和D-Link路由器存在管理员密码泄露漏洞
  16. 全能程序员系列(十二)--开发人员该怎么做PPT?
  17. CSS3左右滑动开关按钮
  18. 这12张数据治理内涵图,你看懂了吗
  19. ios 系统状态栏样式修改_iOS_状态栏字体颜色修改(完美解决)
  20. iphone浏览器Safari中的数字被识别为电话号码

热门文章

  1. ARM Exploitation
  2. mysql与ftp连接过慢的原因
  3. php 使用 go函数指针,golang 什么时候使用指针
  4. python jupyter安装_python之jupyter的安装
  5. 华为5720设置静态路由不通_如何设置静态路由与网关?一文了解清楚
  6. JZOJ 5678. 【GDOI2018Day2模拟4.21】果树
  7. centos安装llvm_CentOS 7.0 编译安装LLVM7.0.0版本
  8. python语音命名规则_python语音变量命名规则
  9. android联网程序,android 联网 HttpClient
  10. java cookie p3p_P3P解决cookie存取的跨域问题