原文网址:Nginx系列–rewrite的应用_IT利刃出鞘的博客-CSDN博客
简介
本文介绍Nginx中rewrite的应用。
分享Java技术星球(自学精灵):learn.skyofit.com
语法
rewrite regex URL [flag];
flag标记位
- last:进行解决rewrite,并对配更改后的 URI 从新进行搜寻(再从 server 走一遍匹配流程)。此时对于以后 server 或 location 上下文,不再解决 rewrite 指令。
- break:进行解决rewrite
<!—->
- last 和 break 的区别:last 重写 url 后,会再从 server 走一遍匹配流程,而 break 终止重写后的匹配
- last 和 break 的相同点:都能进行解决,前面的 rewrite 指令不会再执行。
<!—->
- redirect:返回蕴含 302 代码的长期重定向,在替换字符串不以”http://”,”https://“或”$scheme”结尾时应用.
- permanent:返回蕴含 301 代码的永恒重定向。
<!—->
- permanent 是永恒重定向,浏览器会记住它,会始终重定向你设置的地址。能够通过革除浏览器缓存解决。
rewrite 指令只能返回代码 301 或 302。要返回其余代码,须要在 rewrite 指令前面蕴含 return 指令。
查看rewrite日志
关上日志开关:rewrite_log on; 能够配置到http,server,location和if上下文中。
示例
配置:nginx.conf
location /first {
rewrite_log on;
rewrite /first(.*) /second$1 last;
}
拜访
curl test1.com:8080/first/2.txt
日志后果
示例:break和last
break
配置
server {
listen 9000;
server_name localhost;
location /info {
rewrite ^/.* https://www.baidu.com permanent;
}
location /break {
rewrite /.* /info break;
proxy_pass http://127.0.0.1:9000;
# 该 return 不执行
return 200 "ok";
}
}
拜访
http://localhost:9000
后果
重定向到了baidu.com
剖析
首先,匹配到 /break 的 location 块,执行了 rewrite 和 proxy_pass,跳过 return(因为有 break),重定向到 http://127.0.0.1:9000/info;而后,再次进行 server 块,匹配到 /info 的 location 块,最初重定向到了baidu。
last
配置
server {
listen 9000;
server_name localhost;
location /info {
rewrite ^/.* https://www.baidu.com permanent;
}
location /break {
rewrite /.* /info last;
# 该 proxy_pass 不执行
proxy_pass http://127.0.0.1:9000;
# 该 return 不执行
return 200 "ok";
}
}
拜访
http://localhost:9000
后果
重定向到了baidu.com
剖析
首先,匹配到 /break 的 location 块,执行了 rewrite,跳过 return 和 proxy_pass(因为有 last,proxy_pass 须要和 break 一起用);而后持续匹配,匹配到 /info 的 location 块,最初重定向到了baidu。
示例:在location的外部和内部
break和last在location内部
根底示例
server{
listen 80;
server_name test.com;
root /data/wwwroot/test.com;
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
拜访:test.com/1.html
后果:浏览器最终重定向到test.com/3.html
剖析:申请1.html文件时,会被重定向到2.html,而后被重定向到3.html,最初返回的文件为3.html
例1:rewrite后增加break
server{
listen 80;
server_name test.com;
root /data/wwwroot/test.com;
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
}
拜访:test.com/1.html
后果:浏览器最终重定向到test.com/2.html
剖析:申请1.html文件时,会被重定向到2.html,遇到break,不再执行下边的rewrite。
例2:break后还有location
server{
listen 80;
server_name test.com;
root /data/wwwroot/test.com;
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
location /2.html {
return 403;
}
}
拜访:test.com/1.html
后果:浏览器最终重定向到test.com/2.html
剖析:申请1.html文件时,会被重定向到2.html,遇到break,不再执行下边的rewrite。
break和last在location外部
根底示例
server{
listen 80;
server_name test.com;
root /data/wwwroot/test.com;
location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
拜访:test.com/1.html
后果:浏览器最终重定向到test.com/b.html
剖析:申请1.html,会通过两次重定向到3.html,3.html又刚好匹配location /3.html{},所以返回b.html。
拜访:test.com/2.html
后果:浏览器最终重定向到test.com/a.html
剖析:申请2.html,会通过两次重定向到2.html,2.html又刚好匹配location /2.html{},所以返回a.html。
例1:rewrite后增加break
server{
listen 80;
server_name test.com;
root /data/wwwroot/test.com;
location / {
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
拜访:test.com/1.html
后果:浏览器最终重定向到test.com/2.html
剖析:申请1.html文件时,会被重定向到2.html,遇到break,以后location{} 以及前面的location{} 的指令都不再执行。
例2:break后还有location
server{
listen 80;
server_name test.com;
root /data/wwwroot/test.com;
location / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
拜访:test.com/1.html
后果:浏览器最终重定向到test.com/a.html
剖析:申请1.html文件时,会被重定向到2.html,遇到break,不再执行下边的rewrite。2.html会再去匹配一次location,匹配到了location /2.html,于是返回了test.com/a.html。
发表回复