zoukankan      html  css  js  c++  java
  • nginx 指令之 try_files

    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,则必须明确声明。

     
    location / {
                try_files $uri $uri/ /index.php?$query_string;
    }

     (

    $query_string   请求URI中的参数,与$args相同,$query_string是只读的不会改变的。

    当用户请求 http://localhost/example 时,这里的 $uri 就是 /example。 
    try_files 会到硬盘里尝试找这个文件。如果存在名为 /$root/example(其中 $root 是项目代码安装目录)的文件,就直接把这个文件的内容发送给用户。 
    显然,目录中没有叫 example 的文件。然后就看 $uri/,增加了一个 /,也就是看有没有名为 /$root/example/ 的目录。 
    又找不到,就会 fall back 到 try_files 的最后一个选项 /index.php,发起一个内部 “子请求”,也就是相当于 nginx 发起一个 HTTP 请求到 http://localhost/index.php。 

    例子 2

    复制代码
    loaction / {
    
    try_files $uri @apache
    
    }
    
    loaction @apache{
    
    proxy_pass http://127.0.0.1:88
    
    include aproxy.conf
    
    }
    复制代码

    try_files方法让Ngxin尝试访问后面得$uri链接,并进根据@apache配置进行内部重定向。

    当然try_files也可以以错误代码赋值,如try_files /index.php = 404 @apache,则表示当尝试访问得文件返回404时,根据@apache配置项进行重定向。

    try_files是nginx中http_core核心模块所带的指令,主要是能替代一些rewrite的指令,提高解析效率。官网的文档为http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

    二.

      1.try_files的语法规则:

        格式1:try_files file ... uri;  格式2:try_files file ... =code;

        可应用的上下文:server,location段

      2.try_files的语法解释:(先贴出官方的解释,楼主再解释下)

    Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the fileparameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made. 

      关键点1:按指定的file顺序查找存在的文件,并使用第一个找到的文件进行请求处理

      关键点2:查找路径是按照给定的root或alias为根路径来查找的

      关键点3:如果给出的file都没有匹配到,则重新请求最后一个参数给定的uri,就是新的location匹配

      关键点4:如果是格式2,如果最后一个参数是 = 404 ,若给出的file都没有匹配到,则最后返回404的响应码

     3.举例说明:

    location /images/ {
        root /opt/html/;
        try_files $uri   $uri/  /images/default.gif; 
    }
    比如 请求 127.0.0.1/images/test.gif 会依次查找 1.文件/opt/html/images/test.gif   2.文件夹 /opt/html/images/test.gif/下的index文件  3. 请求127.0.0.1/images/default.gif
    
    4.其他注意事项
    1.try-files 如果不写上 $uri/,当直接访问一个目录路径时,并不会去匹配目录下的索引页  即 访问127.0.0.1/images/ 不会去访问  127.0.0.1/images/index.html .


  • 相关阅读:
    Oracle中字符串连接的实现方法
    Could not Open Install.Log File解决方法
    重新注册IIS
    Sqlserver 语法总结
    无法向会话状态服务器发出会话状态请求
    Windows Live Writer发布测试
    IIS不支持apk文件下载
    “未能加载文件或程序集“×××”或它的某一个依赖项。试图加载格式不正确的程序”问题的解决
    IIS安装
    Eclipse运行内存溢出
  • 原文地址:https://www.cnblogs.com/youxin/p/12293430.html
Copyright © 2011-2022 走看看