当前位置: 代码网 > 服务器>服务器>Linux > nginx配置选项try_files的用法及说明

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

2025年10月10日 Linux 我要评论
一、try_files是nginx中http_core核心模块所带的指令主要是能替代一些rewrite的指令,提高解析效率。官网的文档为module ngx_http_core_module二、try

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

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

官网的文档为module ngx_http_core_module

二、try_files的语法规则

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

可应用的上下文: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

  • 关键点1:按指定的file顺序查找存在的文件,并使用第一个找到的文件进行请求处理
  • 关键点2:查找路径是按照给定的root或alias为根路径来查找的
  • 关键点3:如果给出的file都没有匹配到,则重新请求最后一个参数给定的uri,就是新的location匹配
  • 关键点4:如果是格式2,如果最后一个参数是 = 404 ,若给出的file都没有匹配到,则最后返回404的响应码

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来匹配)

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com