服务器 > 服务器 > Linux

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

26人参与 2025-10-10 Linux

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

总结

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

(0)

您想发表意见!!点此发布评论

推荐阅读

Linux磁盘清理管理的实用命令和自动脚本详解

10-10

Nginx使用try_files遇到的问题及解决

10-10

Linux查看和修改MAC地址的方法大全

10-10

Nginx负载均衡通用方案详解

10-10

Linux服务器SSH客户端连接目标服务器失败的解决方法

10-10

解决linux系统删除文件后,可用率没变,磁盘还是满的问题

10-10

猜你喜欢

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

发表评论