且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Nginx配置指令try_files

更新时间:2021-12-04 04:55:59

    try_files指令是按顺序检测文件的存在性,并且返回第一个找到文件的内容,如果第一个找不到就会自动找第二个,依次查找.其实现的是内部跳转.以下举例说明:

案例1(跳转到变量):

  server {
   listen 8000;
   server_name 121.10.143.66;
   root html;
   index index.html index.php;

 
   location /abc {

       try_files /4.html /5.html @qwe;      --检测文件4.html和5.html,如果存在正常显示,不存在就去查找@qwe值
  }

   location @qwe  {

      rewrite ^/(.*)$   http://www.baidu.com;     --跳转到百度页面

   }


 

测试:

Nginx配置指令try_files

Nginx配置指令try_files

 

 

案例2(跳转指定文件):

server {
   listen 8000;
   server_name 121.10.143.66;
   root html;
   index index.php index.html;


   location /abc {

       try_files /4.html /5.html /6.html;
  }


 

 vim ../../html/6.html
      66666666666666           --写入内容方便测试


测试:

Nginx配置指令try_files

 

 

案例3(如果文件存在):

server {
   listen 8000;
   server_name 121.10.143.66;
   root html;
   index index.php index.html;


   location /abc {

       try_files /4.html /5.html /6.html;
  }


 

vim ../../html/4.html
      4444444444           --如果文件存在会显示第一个文件


测试:

Nginx配置指令try_files










本文转自 z597011036 51CTO博客,原文链接:http://blog.51cto.com/tongcheng/1427194,如需转载请自行联系原作者