break和last各自的作用

官方解释

last:stops processing the current set of ngx_http_rewrite_module directives followed by a search for a new location matching the changed URI;

break:stops processing the current set of ngx_http_rewrite_module directives;

last: 停止当前这个请求,匹配成功后会跳出本条location,会执行其他location,会并根据rewrite匹配的规则重新发起一个请求。

break:相对last,break并不会重新发起一个请求,跳出所有location, 其他location也都不会去执行了,只是跳过当前的rewrite阶段,并执行本请求后续的执行阶段,比如在匹配成功后访问指定的目录文件

举例说明

last情况

server {
listen 80;
server_name wqy.test.com;
access_log logs/wqy-test-com/access.log;
location ^~ /aaa {
rewrite ^/aaa/(.*) /bbb/$1 last;
root /opt/tmp/wqy/test;
}
location ^~ /bbb {
return 200 "hello world\n";
}
}

测试

#curl -s http://wqy.test.com/aaa/index.html -x 127.0.0.1:80
hello world
#curl -s http://wqy.test.com/bbb/index.html -x 127.0.0.1:80
hello world

重点说明

当访问域名wqy.test.com/aaa的时候,后面跟的是/bbb/$1,再往后走发现是last规则,就停止当前规则,跳出loaction再重新执行location匹配一遍,这个时候的域名变成wqy.test.com/bbb/,重新匹配到/bbb,然后返回hello world

当访问域名wqy.test.com/bbb的时候,后面跟的是直接匹配的/bbb然后返回hello world

break情况

server {
listen 80;
server_name wqy.test.com;
access_log logs/wqy-test-com/access.log;
location ^~ /aaa {
rewrite ^/aaa/(.*) /bbb/$1 break;
root /opt/tmp/wqy/test;
}
location ^~ /bbb {
return 200 "hello world\n";
}
}

测试

#curl -s http://wqy.test.com/aaa/index.html -x 127.0.0.1:80
bbb
#curl -s http://wqy.test.com/bbb/index.html -x 127.0.0.1:80
hello world

重点说明

当访问域名wqy.test.com/aaa的时候,后面跟的是/bbb/$1,再往后走发现是break规则,不在跳出location直接就是匹配成功,读取root /opt/tmp/wqy/test/bbb文件内容bbb

当访问域名wqy.test.com/bbb的时候,后面跟的是直接匹配的/bbb然后返回hello world

如果以上还是不明白那么看下面这个例子

location /break/ {
rewrite ^/break/(.*) /test/$1 break;
return 402;
}
location /last/ {
rewrite ^/last/(.*) /test/$1 last;
return 403;
}
location /test/ {
return 508;
}

请求break:如果请求开头为/break/index.html 因为后面是break,不再进行重新匹配,就直接访问/test下的index.html文件内容,如果/test下没有文件就返回404找不到报错

请求last:如果是请求开头为/last/index.html,因为后面是last,重新跳出本location进行匹配,/test/ 直接返回508(访问last相当于直接访问/test/)

Nginx Rewrite规则的break和last示例的更多相关文章

  1. [转】 nginx rewrite规则

    http://www.cnblogs.com/cgli/archive/2011/05/16/2047920.html 最近在VPS上尝试配置安装一个网站,VPS安装了LNMP(Linux+Nginx ...

  2. Nginx Rewrite规则记录

    Rewrite 是一种服务器的重写脉冲技术,它可以使得服务器可以支持 URL 重写,是一种最新流行的服务器技术.它还可以实现限制特定IP访问网站的功能.很多情况下,某个 IP 的访问很容易造成 CPU ...

  3. Nginx rewrite 规则 与 proxy_pass 实现

    Nginx rewrite 规则  与 proxy_pass 实现     -------------------------------------------------------------- ...

  4. Nginx Rewrite规则详解

    Rewrite规则含义就是某个URL重写成特定的URL,从某种意义上说为了美观或者对搜索引擎友好,提高收录量及排名等. Rewrite规则的最后一项参数为flag标记,支持的flag标记主要有以下几种 ...

  5. Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)

    一.Nginx Rewrite 规则 1. Nginx rewrite规则 Rewrite规则含义就是某个URL重写成特定的URL(类似于Redirect),从某种意义上说为了美观或者对搜索引擎友好, ...

  6. [转帖]Nginx rewrite 规则 与 proxy_pass 实现

    Nginx rewrite 规则 与 proxy_pass 实现 https://www.cnblogs.com/jicki/p/5546916.html Nginx rewrite 规则  与 pr ...

  7. Nginx Rewrite规则

    location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配 ...

  8. Nginx Rewrite规则初探(转)

    Nginx  rewrite(nginx url地址重写)Rewrite 主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Ng ...

  9. nginx rewrite规则笔记

    优先级 在nginx的location和配置中location的顺序没有太大关系.正location表达式的类型有关.相同类型的表达式,字符串长的会优先匹配. 第一优先级:等号类型(=)的优先级最高. ...

随机推荐

  1. 能源科技,苹果和Google的新圣战?

    细心的果粉可能会注意到,最新版本的IOS软体中,增加了一个不起眼的按钮,它是一款署名为"家庭"的App,之所以说它不起眼,是因为它好像真得没什么用,活跃率恐怕不及Wechat的万分 ...

  2. 【最简单的vim教程】vim学习笔记-基础操作

    说明 C-字母 = Ctrl + 字母 char = 任意字符 开始编辑 insert 按键 功能 说明 i(I) insert 当前位置插入(当前行前) a(A) append 当前字符后面插入(当 ...

  3. 事务Transaction

    目录 为什么写这系列的文章 事务概念 ACID 并发事务导致的问题 脏读(Dirty Read) 非重复读(Nonrepeatable Read) 幻读(Phantom Reads) 丢失修改(Los ...

  4. C++走向远洋——52(十三周阅读程序)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  5. golang 统计系统测试覆盖率

    golang 统计系统测试覆盖率 参考资料 https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests https:/ ...

  6. C语言程序设计100例之(31):全排列问题

    例31   全排列问题 题目描述 输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数字. 输入格式 n(1≤n≤9) 输出格式 由1-n组成的所有不重复的数字 ...

  7. Asp.net Core MVC(四)

    上一篇说的是asp.net mvc核心UseMvc的过程,末尾想捋一下asp.net核心的路由流转过程,现在看来还是要准备下一个代码,熟悉了代码,那么整个流转过程就通了〜 不多说,今儿先看下,Rout ...

  8. czC#01

    1. .net简介: .net分为.net平台及.net Framework 2..NET作用 2.转义与@ 3.类型转换 1) 隐式转换 2)显式类型转换 (待转换的目标类型)原始值

  9. 【技巧】歪脑筋优化flexbox瀑布流布局方案

    效果先行 需求 在大量"不定宽"元素并排的布局模式下,上图是我们想要的最佳布局但是FlexBox布局虽然枪弹但并不能完全呈现以上布局,于是我们需要结合FlexBox作下小的改动即可 ...

  10. RuntimeError: No application found. Either work inside a view function or push an application context.

    记录: 遇到这种报错信息: 在create_all()生成数据表的时候,添加app=app,指明app对象即可-----> create_all(app=app)