break 将重写的URI作为一个新的URI,在本块中继续处理,将重写后 的地址在当前location块中处理,不会将新的URI转向到其他location块中

last,终止继续在本location块中处理接收到的URI,并将此处重写的URI作位一个新的URI,使用各location进行处理,该标志将重写后的URI重新在server块中执行,为重写后的URI提供转入到其他

location的机会。

Example 1: No (break or last) flags:

server {
server_name example.com;
root 'path/to/somewhere'; location / {
echo 'finally matched location /';
} location /notes {
echo 'finally matched location /notes';
} location /documents {
echo 'finally matched location /documents';
} rewrite ^/([^/]+.txt)$ /notes/$1;
rewrite ^/notes/([^/]+.txt)$ /documents/$1;
}

Result:

url example.com/test.txt
finally matched location /documents

Explanation:

For rewrite, the flags are optional!

Example 2: Outside location block (break or last):

server {
server_name example.com;
root 'path/to/somewhere'; location / {
echo 'finally matched location /';
} location /notes {
echo 'finally matched location /notes';
} location /documents {
echo 'finally matched location /documents';
} rewrite ^/([^/]+.txt)$ /notes/$1 break; # or last
rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed
}

Result:

#curl example.com/test.txt
finally matched location /notes

Explanation:

Outside the location block, both break and last behave in the exact manner...

no more parsing of rewrite conditions
Nginx internal engine goes to the next phase (searching for location match)

Example 3: Inside location block - "break":

server {
server_name example.com;
root 'path/to/somewhere'; location / {
echo 'finally matched location /';
rewrite ^/([^/]+.txt)$ /notes/$1 break;
rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed
} location /notes {
echo 'finally matched location /notes';
} location /documents {
echo 'finally matched location /documents';
}
}

Result:

# curl example.com/test.txt
finally matched location /

Explanation:

Inside a location block, break flag would do the following...

no more parsing of rewrite conditions
Nginx internal engine continues to parse the current location block

Example 4: Inside location block - "last":

server {
server_name example.com;
root 'path/to/somewhere'; location / {
echo 'finally matched location /';
rewrite ^/([^/]+.txt)$ /notes/$1 last;
rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed
} location /notes {
echo 'finally matched location /notes';
rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed, either!
} location /documents {
echo 'finally matched location /documents';
}
}

Result:

# curl example.com/test.txt
finally matched location /notes

Explanation:

Inside a location block, last flag would do the following...

no more parsing of rewrite conditions
Nginx internal engine starts to look for another location match based on the result of the rewrite result.
no more parsing of rewrite conditions, even on the next location match!

Summary:

When a rewrite condition with the flag break or last matches, Nginx stops parsing any more rewrites!
Outside a location block, with break or last, Nginx does the same job (stops processing anymore rewrite conditions).
Inside a location block, with break, Nginx only stops processing anymore rewrite conditions
Inside a location block, with last, Nginx stops processing anymore rewrite conditions and then starts to look for a new matching of location block! Nginx also ignores any rewrites in the new location block!

Final Note:

missed to include some more edge cases (actually common problem with rewrites, such as 500 internal error). But, that'd be out of scope of this question. Probably, example 1 is out of scope, too!

nginx url rewrite break和last的区别的更多相关文章

  1. nginx中的break与last指令区别

    很多人资料说,last与break的区别在于,last并不会停止对下面location的匹配.我理解上模模糊糊.今天自己来测验了一下. rewrite 指令末尾的break应该与单独写break作用是 ...

  2. Nginx 笔记与总结(12)Nginx URL Rewrite 实例(ecshop)

    访问项目地址:http://192.168.254.100/ecshop 某个商品的 URL:http://192.168.254.100/ecshop/goods.php?id=3 现在需要实现把以 ...

  3. thinkphp nginx php-fpm url rewrite 导致 404 错误

    ## thinkphp nginx php-fpm url rewrite 导致 404 错误 之前thinkphp的系统部署在apache上,考虑到在并发性能nginx比apache强悍得多,所以在 ...

  4. nginx url重写 rewrite实例

    本文介绍下,在nginx中实现Url重写,学习rewrite的具体用法,有需要的朋友参考下吧. 原文地址:http://www.360doc.com/content/14/0202/20/142341 ...

  5. Nginx下支持ThinkPHP的Pathinfo和URl Rewrite模式

    下面介绍如何使Nginx支持ThinkPHP的Pathinfo和URL Rewrite模式. 1.ThinkPHP给出了ThinkPHP的官方解决方案,如下: 打开Nginx的配置文件 /etc/ng ...

  6. [转]nginx下的url rewrite

    转:http://zhengdl126.iteye.com/blog/698206 if (!-e $request_filename){rewrite "^/index\.html&quo ...

  7. (Nginx) URL REWRITE

    URL重写的基础介绍 把URI地址用作参数传递:URL REWRITE 最简单的是基于各种WEB服务器中的URL重写转向(Rewrite)模块的URL转换: 这样几乎可以不修改程序的实现将 news. ...

  8. Nginx URL重写(rewrite)配置及信息详解

    URL重写有利于网站首选域的确定,对于同一资源页面多条路径的301重定向有助于URL权重的集中 Nginx URL重写(rewrite)介绍 和apache等web服务软件一样,rewrite的组要功 ...

  9. nginx中break和last的区别

    一.环境准备 资源文件创建 mkdir -p /opt/tmp/wqy/test/aaa mkdir -p /opt/tmp/wqy/test/bbb echo "aaa" > ...

随机推荐

  1. Educational Codeforces Round 66 (Rated for Div. 2) A. From Hero to Zero

    链接:https://codeforces.com/contest/1175/problem/A 题意: You are given an integer nn and an integer kk. ...

  2. 关于EasyML的使用

    一.安装IntelliJ Idea 具体安装过程比较简单.但是遇到一个问题,如今LInux版本的IntelliJ的安装需要jdk1.8及以上版本的支持,但是EasyML目前仅支持jdk1.7的环境. ...

  3. IIS中的 Asp.Net Core 和 dotnet watch

    在基于传统的.NET Framework的Asp.Net Mvc的时候,本地开发环境中可以在IIS中建立一个站点,可以直接把站点的目录指向asp.net mvc的项目的根目录.然后build一下就可以 ...

  4. LinkedList源码及原理

    简介 内部结构分析 LinkedList源码分析 构造方法 添加(add)方法 根据位置取数据的方法 根据对象得到索引的方法 检查链表是否包含某对象的方法: 删除(remove/pop)方法 Link ...

  5. Fedora12下yum安装低版本gcc

    1.Fedora12下gcc位置及其版本如下: 2.根据需要,要安装低版本的gcc,直接用"yum install gcc"安装时默认是安装最新版本的gcc,如下: 3.可先通过& ...

  6. Godaddy虚拟主机新建mysql数据库 2019最新

    第一次用狗爹,完全摸不着路子. 网站本地已搭建,不知道数据库是在哪里上传. 百度搜索结果都是四五年前的旧内容,耽误时间. 还是问客服,Godaddy的客服确实不赖 godaddy虚拟主机如何新建数据库 ...

  7. 【深度精讲】JFinal中的Ret和Kv工具类的区别,你用对了吗?

    在JFinal中有两个类Map的工具类,一个是有状态的Ret,一个是无状态的Kv,各种自己的应用场景,你用对了吗? 下面我们从多个方面来探究一下,JFinal针对这两个类的设计: 一.位置-com.j ...

  8. iOS Programming Autorotation, Popover Controllers, and Modal View Controllers

    iOS Programming Autorotation, Popover Controllers, and Modal View Controllers  自动旋转,Popover 控制器,Moda ...

  9. Linux 安装Memcache扩展支持

    查看相关软件包 yum search memcached 安装memcache yum -y install memcachedMemcache关联php yum -y install php-pec ...

  10. node执行cmd命令方法

    var cmd='tasklist';//获取 子进程模块的exec方法,用于执行cmd命令var exec = require('child_process').exec; //运行 定义的cmd命 ...