Nginx Rewrite规则的break和last示例
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示例的更多相关文章
- [转】 nginx rewrite规则
http://www.cnblogs.com/cgli/archive/2011/05/16/2047920.html 最近在VPS上尝试配置安装一个网站,VPS安装了LNMP(Linux+Nginx ...
- Nginx Rewrite规则记录
Rewrite 是一种服务器的重写脉冲技术,它可以使得服务器可以支持 URL 重写,是一种最新流行的服务器技术.它还可以实现限制特定IP访问网站的功能.很多情况下,某个 IP 的访问很容易造成 CPU ...
- Nginx rewrite 规则 与 proxy_pass 实现
Nginx rewrite 规则 与 proxy_pass 实现 -------------------------------------------------------------- ...
- Nginx Rewrite规则详解
Rewrite规则含义就是某个URL重写成特定的URL,从某种意义上说为了美观或者对搜索引擎友好,提高收录量及排名等. Rewrite规则的最后一项参数为flag标记,支持的flag标记主要有以下几种 ...
- Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)
一.Nginx Rewrite 规则 1. Nginx rewrite规则 Rewrite规则含义就是某个URL重写成特定的URL(类似于Redirect),从某种意义上说为了美观或者对搜索引擎友好, ...
- [转帖]Nginx rewrite 规则 与 proxy_pass 实现
Nginx rewrite 规则 与 proxy_pass 实现 https://www.cnblogs.com/jicki/p/5546916.html Nginx rewrite 规则 与 pr ...
- Nginx Rewrite规则
location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配 ...
- Nginx Rewrite规则初探(转)
Nginx rewrite(nginx url地址重写)Rewrite 主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Ng ...
- nginx rewrite规则笔记
优先级 在nginx的location和配置中location的顺序没有太大关系.正location表达式的类型有关.相同类型的表达式,字符串长的会优先匹配. 第一优先级:等号类型(=)的优先级最高. ...
随机推荐
- iptables学习02-nat表应用
nat表应用实验 第一步 准备工作 A机器两块网卡ens33(192.168.2.106).再添加一块自定义网卡ens37(192.168.100.1),添加到LAN内网区段(自定义名字,写什么无所谓 ...
- 初识Spring JdbcTemplate
JdbcTemplate 概述 JdbcTemplate是Spring提供的一个模板类,它是对jdbc的封装.用于支持持久层的操作.具有简单,方便等特点. pom.xml <!--依赖版本--& ...
- Python——1变量和数据类型(内含其他知识点链接)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- ZOJ 4109 Welcome Party
题目链接:(https://zoj.pintia.cn/problem-sets/91827364500/problems/91827370504)(https://vjudge.net/proble ...
- Arthas 实战,助你解决同名类依赖冲突问题
上篇文章中,小黑哥分析 Maven 依赖冲突分为两类: 项目同一依赖应用,存在多版本,每个版本同一个类,可能存在差异. 项目不同依赖应用,存在包名,类名完全一样的类. 第二种情况,往往是这个场景,本地 ...
- No CPU/ABI system image available for this target
在创建AVD设备的时候无法正常创建虚拟设备,CPU选项不能选择. 下面报错:No CPU/ABI system image available for this target 是因为SDK里面缺少了s ...
- 【tomcat系列】配置tomcat远程访问
当程序部署在tomcat上后,需要监测tomcat的性能和监测tomcat的各项指标,如内存使用情况,cpu使用情况,jvm实际情况等,对于这些指标的监控,tomcat提供了访问入口,然而tomcat ...
- RIP实验
实验要求 1. 理解 RIP 协议的工作原理2. 理解 RIPv1.RIPv2 的特性3. 掌握 RIP 协议的基本配置方法4. 掌握 RIP 自动汇总和手动汇总的方法5. 掌握 RIP 配 ...
- python sqlite3操作类扩展,包含数据库分页
一.原因 最近在使用python3和sqlite3编辑一些小程序,由于要使用数据库,就离不开增.删.改.查,sqlite3的操作同java里的jdbc很像,于是就想找现成的操作类,找来找去,发现一个 ...
- Deeplink推广,打开率很低怎么办?
但凡做TOC业务的多多少少都会用到Deeplink,这是一个重要的运营手段.但用了Deeplink却没有达到预期的目标,打开率不尽人意,你有没有想过到底是什么原因? 在Deeplink这条路上,我们当 ...