nginx中rewrite和if的用法及配置

一、rewrite应用

rewrite语法

rewrite  <正则表达式>  <跳转后的内容>  [rewrite支持的flag标记]
  • 1

1、rewrite跳转场景

  • URL看起来更规范、合理
  • 企业会将动态URL地址伪装成静态地址提供服务
  • 网址换新域名后,让旧的访问跳转到新的域名上
  • 服务端某些业务调整

2、rewrite实际场景

  • 1.3.1 Nginx跳转需求的实现方式
    使用rewrite进行匹配跳转
    使用if匹配全局变量后跳转
    使用location匹配再跳转
  • 1.3.2 rewrite放在server{},if{},location{}段中
    location只对域名后边的除去传递参数外的字符串起作用
  • 1.3.3 对域名或参数字符串
    使用if全局变量匹配
    使用proxy_pass反向代理

3、常用的nginx正则表达式

^:匹配输入字符串的起始位置
$:匹配输入字符串的结束位置
*****:匹配前面的字符零次或多次
+:匹配前面的字符一次或多次
?:匹配前面的字符零次或一次
.:匹配除\n之外的任何单个字符 使用[.\n]可以匹配包括\n在内的任意字符
****:转义符
\d:匹配纯数字
{n}:重复n次
{n,}:重复n次或更多次
[c]:匹配单个字符c
[a-z]:匹配a-z小写字母的任意一个
[a-zA-Z]:匹配a-z小写字母或A-Z大写字母的任意一个

4、常见的flag

flag 作用
last 基本上都用这个flag,表示当前的匹配结束,继续下一个匹配,最多匹配10个到20个 一旦此rewrite规则重写完成后,就不再被后面其它的rewrite规则进行处理 而是由UserAgent重新对重写后的URL再一次发起请求,并从头开始执行类似的过程
break 中止Rewrite,不再继续匹配 一旦此rewrite规则重写完成后,由UserAgent对新的URL重新发起请求, 且不再会被当前location内的任何rewrite规则所检查
redirect 以临时重定向的HTTP状态302返回新的URL
permanent 以永久重定向的HTTP状态301返回新的URL

last和break在重定向后,地址栏都不会发生变化,这是它们的相同点,不同点在于last会写在server和if中,break是写在location中,last不会终止重写后的url匹配,break会终止重写后的url匹配。

5、案例配置

rewrite重写flag

rewrite主要是用来重写URL或者跳转URL的指令

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
root /hhh/xxx;
location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
    location /2.html <span class="token punctuation">{<!-- --></span>
rewrite /2.html /a.html<span class="token punctuation">;</span>
<span class="token punctuation">}</span> location /3.html <span class="token punctuation">{<!-- --></span>
rewrite /3.html /b.html<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

[root@nginx ~]# nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@nginx ~]# systemctl restart nginx

[root@nginx ~]# mkdir -p /hhh/xxx

[root@nginx ~]# echo "web1" > /hhh/xxx/1.html

[root@nginx ~]# echo "web2" > /hhh/xxx/2.html

[root@nginx ~]# echo "web3" > /hhh/xxx/3.html

[root@nginx ~]# echo "web a" > /hhh/xxx/a.html

[root@nginx ~]# echo "web b" > /hhh/xxx/b.html

[root@nginx ~]# curl 192.168.183.138/1.html

web b

当请求1.html时,最终会访问到web b

location{}内部,遇到break,本location{}内以及后面的所有location{}内的所有指令都不再执行,所以至跳转到2.html访问到web2

		location / {
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
    location /3.html <span class="token punctuation">{<!-- --></span>
rewrite /3.html /b.html<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

[root@nginx ~]# curl 192.168.183.138/1.html

web2

在location{}内部,遇到last,本location{}内后续指令不再执行,而重写后的url会对所在的server{…}标签重新发起请求,从头到尾匹配一遍规则,哪个匹配则执行哪个。

		location / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
    location /2.html <span class="token punctuation">{<!-- --></span>
rewrite /2.html /a.html<span class="token punctuation">;</span>
<span class="token punctuation">}</span> location /3.html <span class="token punctuation">{<!-- --></span>
rewrite /3.html /b.html<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

[root@nginx ~]# curl 192.168.183.138/1.html web a

二、if应用

1、if语句中的判断条件

正则表达式匹配

  • ==:等值比较;
  • ~:与指定正则表达式模式匹配时返回“真”,判断匹配与否时区分字符大小写;
  • ~*:与指定正则表达式模式匹配时返回“真”,判断匹配与否时不区分字符大小写;
  • !~:与指定正则表达式模式不匹配时返回“真”,判断匹配与否时区分字符大小写;
  • !~*:与指定正则表达式模式不匹配时返回“真”,判断匹配与否时不区分字符大小写

2、文件及目录匹配判断

  • -f, !-f:判断指定的路径是否为存在且为文件;
  • -d, !-d:判断指定的路径是否为存在且为目录;
  • -e, !-e:判断指定的路径是否存在,文件或目录均可;
  • -x, !-x:判断指定路径的文件是否存在且可执行;

3、基于浏览器实现分离案例

if ($http_user_agent ~ Firefox) {
rewrite ^(.*)$ /firefox/$1 break;
} if (\(http_user_agent ~ MSIE) {
rewrite ^(.*)\) /msie/$1 break;

} if (\(http_user_agent ~ Chrome) {
rewrite ^(.*)\) /chrome/$1 break;

}

4、防盗链案例

location ~* \.(jpg|gif|jpeg|png)$ {
valid_referers none blocked www.idfsoft.com;
if ($invalid_referer) {
rewrite ^/ http://www.idfsoft.com/403.html;
}
}

文章知识点与官方知识档案匹配,可进一步学习相关知识
Java技能树控制执行流程if-else137204 人正在系统学习中

[转帖]nginx中rewrite和if的用法及配置的更多相关文章

  1. Nginx中root与alias的用法及区别:

    Nginx中root与alias都是定义location {}块中虚拟目录访问的文件位置: 先看看两者在用法上的区别: location /img/ { alias /var/www/image/; ...

  2. nginx中多ip多域名多端口配置

    1.Nginx中多IP配置: server { listen 80; server_name 192.168.15.7; location / { root /opt/Super_Marie; ind ...

  3. Nginx中rewrite实现二级域名、三级域名、泛域名、路径的重写

    最常见的: 静态地址重定向到带参数的动态地址 rewrite "^(.*)/service/(.*)\.html$" $1/service.php?sid=$2 permanent ...

  4. nginx中rewrite flag

    rewrite  正则表达式  新URI  [flag]; [flag] 选项用于调控重写的行为,它的取值可能是: last:重写完成后,会停止继续处理当前区块所有属于ngx_http_rewrite ...

  5. NGINX中的proxy_pass和rewrite

    文章作者:luxianghao 文章来源:http://www.cnblogs.com/luxianghao/p/6807081.html 转载请注明,谢谢合作. 免责声明:文章内容仅代表个人观点,如 ...

  6. [转帖]Nginx rewrite模块深入浅出详解

    Nginx rewrite模块深入浅出详解 https://www.cnblogs.com/beyang/p/7832460.html rewrite模块(ngx_http_rewrite_modul ...

  7. nginx中location的顺序(优先级)及rewrite规则写法

    一.location正则写法 一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所 ...

  8. Nginx中的 location 匹配和 rewrite 重写跳转

    Nginx中的location匹配和rewrite重写跳转 1.常用的Nginx正则表达式 2.location 3.rewrite 4.rewrite实例 1.常用的Nginx正则表达式: ^ :匹 ...

  9. Nginx中的Location和Rewrite

    Nginx中的Location和Rewrite 目录 Nginx中的Location和Rewrite 一.location 1. location的分类 2. location常用的匹配规则 3. l ...

  10. Nginx中的rewrite指令

    转自:http://www.76ku.cn/articles/archives/317 rewite.在server块下,会优先执行rewrite部分,然后才会去匹配location块server中的 ...

随机推荐

  1. C++中map,multimap和unordered_map的区别

    map.multimap容器 map的所有元素都是pair,同时拥有键值(key)和实值(value) pair的第一元素被视为键值,第二元素被视为实值 性质: 以rb_tree为底层结构,因此元素有 ...

  2. react-native在windows环境搭建并使用脚手架新建工程

    截止到2024-1-11,使用的主要软件的版本如下: 软件实体 版本 react-native 0.73.1 react 18.2.0 react-native-cli 2.0.1 Android S ...

  3. 斯坦福 UE4 C++ ActionRoguelike游戏实例教程 10.控制台变量的用法 & 静态函数库 & 使用对象通道对碰撞进行控制

    斯坦福课程 UE4 C++ ActionRoguelike游戏实例教程 0.绪论 概述 本文对应Lecture 15, 61 - Console Variables for debugging and ...

  4. MySQL系列:索引(B+Tree树、构建过程、回表、基本操作、执行计划、应用)

    介绍 https://dev.mysql.com/doc/refman/5.7/en/optimization-indexes.html 作用 优化查询 算法 索引的算法包括 BTree Hash R ...

  5. 云小课|云小课带你快速掌握云数据迁移CDM

    阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说).深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云.更多精彩内容请单击此处. 摘要:欢迎来到云数据迁移 ...

  6. 10亿数据、查询<10s,论基于OLAP搭建广告系统的正确姿势

    更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群   由于流量红利逐渐消退,越来越多的广告企业和从业者开始探索精细化营销的新路径,取代以往的全流量.粗放式的广告轰炸 ...

  7. SpringBoot Admin OFFLINE

    java.util.concurrent.TimeoutException message Did not observe any item or terminal signal within 100 ...

  8. 第04讲:Flink 常用的 DataSet 和 DataStream API

    Flink系列文章 第01讲:Flink 的应用场景和架构模型 第02讲:Flink 入门程序 WordCount 和 SQL 实现 第03讲:Flink 的编程模型与其他框架比较 第04讲:Flin ...

  9. xv6book阅读 chapter1

    xv6book主要研究了xv6如何实现它的类Unix接口,但是其思想和概念不仅仅适用于Unix.任何操作系统都必须将进程多路复用到底层硬件上,相互隔离进程,并提供受控制的进程间通信机制. 1 了解xv ...

  10. VS Code的C/C++环境配置的傻瓜式教程(看这一篇就够了)

    html: toc: true VS Code的C/C++环境配置的傻瓜式教程(看这一篇就够了) 写在前面的话 作者在学习使用vscode写C代码的时候,根据网上很多参差不齐的教程踩了不少的坑,很多教 ...