nginx

关注公众号 jb51net

关闭
首页 > 网站技巧 > 服务器 > nginx > nginx配置选项try_files

nginx配置选项try_files的用法及说明

作者:flyingju

这篇文章主要介绍了nginx配置选项try_files的用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

一、try_files是nginx中http_core核心模块所带的指令

主要是能替代一些rewrite的指令,提高解析效率。

官网的文档为Module ngx_http_core_module

二、try_files的语法规则

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

1.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

2.举例说明

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

3.其他注意事项

try-files 如果不写上 $uri/,当直接访问一个目录路径时,并不会去匹配目录下的索引页  即 访问127.0.0.1/images/ 不会去访问  127.0.0.1/images/index.html 

三、其他用法

location / {
    try_files /system/maintenance.html
              $uri $uri/index.html $uri.html
              @mongrel;
}
location @mongrel {
    proxy_pass http://mongrel;
}

以上中若未找到给定顺序的文件,则将会交给location @mongrel处理(相当于匹配到了@mongrel来匹配)

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文