系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > server > anz > 详细页面

Nginx中break与last的区别详析

时间:2021-01-17来源:www.pcxitongcheng.com作者:电脑系统城

先说区别

  • last,重写后的规则,会继续用重写后的值去匹配下面的location。
  • break,重写后的规则,不会去匹配下面的location。使用新的规则,直接发起一次http请求了。

Nginx 配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
  listen 88;
  server_name _;
  location /break { # location 1
   rewrite ^/break/(.*)$ /bak/$1 break;
  }
  location /last { # location 2
   rewrite ^/last/(.*)$ /bak/$1 last;
  }
 
  location /bak { # location 3
   default_type text/html;
   return 200 $uri;
  }
 
}

访问 http://rumenz.com:88/break/one

命中location1,浏览器地址栏没有变,直接去找 /nginx/html/bak/one 文件,由于没有这个文件所以返回404。

浏览器

Nginx错误(error.log)日志

1 /nginx/html/bak/one failed (2: No such file or directory)

break 表示重写后停止不再匹配 location 块。

访问 http://rumenz.com:88/last/one

命中location2,浏览器地址栏没有变,重新匹配到 location3 

 

last表示重写后跳到location块再次用重写后的地址匹配

break 和 last 的使用场景

break

文件下载,隐藏保护真实文件服务器。

1
2
3
location /down {
 rewrite ^/down/(.*)$ https://rumenz.com/file/$1 break;
}

last

接口地址改写,将 https://rumenz.com/api/list 改写成 https://rumenz.com/newapi/list

1
2
3
4
5
6
7
8
location /api {
 rewrite ^/api/(.*)$ /newapi/$1 last;
}
 
location /newapi {
 default_type Application/json;
 return 200 '{"code":200,"msg":"ok","data":["JSON.IM","json格式化"]}';
}

总结

到此这篇关于Nginx中break与last区别的文章就介绍到这了,更多相关Nginx中break与last区别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

分享到:

相关信息

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载