当前位置: 首页 > ds >正文

nginx-重定向-正则表达式-路由匹配优先级

nginx-重定向-正则表达式-路由匹配优先级

  • 一、return指令实现重定向
    • 功能1:所有请求重定向
    • 功能2:特定路径重定向
    • 功能3:具体页面迁移
  • 二、正则表达式
  • 三、rewrite指令实现重定向
    • 功能1:请求重定向
    • 功能2: 对特定浏览器重定向
    • 功能3:实现url里携带参数的转发
    • 功能4:防盗链
  • 四、路由匹配优先级


一、return指令实现重定向

模块 ngx_http_rewrite_module

永久重定向 301
临时重定向 302

访问www.feng.com或者192.168.100.157 重定向到 www.jd.com

功能1:所有请求重定向

www.feng.com —>www.jd.com 302
www.feng.com —>www.qq.com 301

修改主配置文件nginx.conf

[root@nginx-1 conf]# vim nginx.conflocation / {root   html;index  index.html index.htm;#return 302 https://www.jd.com;return 301 https://www.qq.com;}[root@nginx-1 conf]# nginx  -t
nginx: the configuration file /usr/local/nginx3/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx3/conf/nginx.conf test is successful
[root@nginx-1 conf]# nginx  -s reload

功能2:特定路径重定向

www.feng.com/jd -->www.jd.com
www.feng.tom/qq -->www.qq.com

[root@nginx-1 conf]# vim nginx.conflocation / {root   html;index  index.html index.htm;}#自己定义路由location /jd {return 301 https://www.jd.com;}location /qq {return 301 https://www.qq.com;}[root@nginx-1 conf]# nginx  -t
nginx: the configuration file /usr/local/nginx3/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx3/conf/nginx.conf test is successful
[root@nginx-1 conf]# nginx  -s reload

功能3:具体页面迁移

www.feng.com/rain/a/20250814A01GIY00 -->news.qq.com/rain/a/20250814A01GIY00

[root@nginx-1 conf]# vim nginx.conflocation / {root   html;index  index.html index.htm;return  301 https://news.qq.com$request_uri; #添加}[root@nginx-1 conf]# nginx  -t
nginx: the configuration file /usr/local/nginx3/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx3/conf/nginx.conf test is successful
[root@nginx-1 conf]# nginx  -s reload

二、正则表达式

regular expression: 由字符串+特殊符号+数字组合而成的一个公式,用来表达某个具体的意思
是一套规则(方法),很多命令都想使用这套规则
最开始这套方法是perl语言里出现的 --》用来查找文本的时候发明
特别是从海量的文本里,搜索出需要的内容

特殊符号(元字符)^ $ + ? * . \ ( ) [ ] { } |
^代表以什么开头
^root 以root开头的行
$代表以什么结尾
bash$ 以bash结尾的行
.代表单个任意字符
*表示前面一个字符可以出现0到n次
.*表示任意个字符出现任意次
?代表前面的字符出现0或者1次
+代表前面的字符出现1或者n次
\转义字符
|逻辑或,匹配左边或右边的表达式
{n}匹配前一个元素 恰好 n 次
{n,}匹配前一个元素 至少 n 次
{n,m}匹配前一个元素 n 到 m 次
^huang{3,5}代表以huang开头 g字符出现3到5次
[a-z]代表所有的小写字母
[A-Z]代表所有的大写字母
[abc]匹配 a、b、c 中的任意一个字符
[^abc]匹配 除了 a、b、c 之外的任意字符
[0-9]代表0到9任意一个字符
[0-9]+代表匹配一个或多个连续的数字
[a-zA-Z0-9]匹配任意字母或数字

nginx 支持正则表达式,结合不同的修饰符可以实现精准匹配、模糊匹配、正则匹配等功能

修饰符含义
=精准匹配,完全一致时才生效
!=不精准匹配
~匹配文本区分大小写 --》模糊匹配
~*匹配文本不区分大小写
$1第一个位置变量(标签)
$2第2个位置变量(标签)
!~不匹配,区分大小写
!~*不匹配,不区分大小写

rewrite 的使用

if ($http_user_agent ~ MSIE) {rewrite ^(.*)$ /msie/$1 break;
}location /download/ {rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  break;return  403;
}rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  last;return  403;uri  -->/download/sc/media/huang.mp3www.feng.com/download/sc/media/huang.mp5   
www.feng.com/download/cs/audio/zhang.mp4 --》www.feng.com/download/sc/mp3/huang.mp3

三、rewrite指令实现重定向

功能1:请求重定向

www.feng.com --》www.taobao.com
www.feng.com/zhang --》 https://www.taobao.com/zhang

[root@nginx-1 conf]# vim nginx.conflocation / {root   html;index  index.html index.htm;#return 302 https://www.jd.com;#return 301 https://news.qq.com$request_uri;#rewrite ^/(.*)  https://www.taobao.com/$1 permanent;   #永久重定向  301rewrite ^/(.*)  https://www.tmall.com/$1 redirect;   #临时重定向 302}[root@nginx-1 conf]# nginx -t
nginx: the configuration file /usr/local/nginx3/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx3/conf/nginx.conf test is successful
[root@nginx-1 conf]# nginx  -s reload

功能2: 对特定浏览器重定向

[root@nginx-1 conf]# vim nginx.conflocation / {root   html;index  index.html index.htm;# if语句可以去检查我们的http的请求报文头部信息的user-agent的字符串内容,进行文本匹配if ($http_user_agent ~* Safari) {rewrite ^(.*)$ https://www.jd.com$1 break;}}
[root@nginx-1 conf]# nginx  -t
nginx: the configuration file /usr/local/nginx3/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx3/conf/nginx.conf test is successful
[root@nginx-1 conf]# nginx  -s reload

如果用户使用的是requests库过来访问的(默认没有修改user-agent),直接返回404状态码

   if ($http_user_agent ~* python-requests) {return 404 ;}

http://192.168.100.158/sc?name=feng

功能3:实现url里携带参数的转发

rewrite ^ /new-path permanent;
rewrite ^/old-path(.*)$ /new-path$1 permanent;

    location /163 {rewrite ^ https://www.163.com/163 permanent;#rewrite ^/163(.*)$ https://www.163.com/163$1 permanent;}

功能4:防盗链

盗链 --》偷偷链接到别人的网站上,自己的网站上没有,直接使用别人的东西 --》图片、视频、音频、文件

https://p2.img.cctvpic.com/photoworkspace/contentimg/2025/08/12/2025081120352134793.jpg

https://p2.img.cctvpic.com -->请求报文里的host字段
uri --》nginx内部的变量–》request_uri
/photoworkspace/contentimg/2025/08/12/2025081120352134793.jpg -->uri

别人盗链我们网站的图片,对我们的服务器有什么危害?
需要连接到我们的服务器 --》消耗服务器的cpu、内存、网络带宽

location ~* \.(gif|jpg|png|swf|flv)$ {valid_referers none blocked www.sanchuangedu.cn www.huang.com; #定义有效的引用if ($invalid_referer) {return 404;} //防盗链

四、路由匹配优先级

路由匹配的优先级问题
= --> ^~ --> ~* --> ~ 最后才是 /

location = / {[ configuration A ]
}location / {[ configuration B ]
}location  /documents/ {[ configuration C ]
}location ^~ /images/ {[ configuration D ]
}location ~* \.(gif|jpg|jpeg)$ {[ configuration E ]
}http://192.168.100.158  -->Ahttp://192.168.100.158/index.html -->Bhttp://192.168.100.158/documents/document.html  -->Chttp://192.168.100.158/images/1.gif  -->Dhttp://192.168.100.158/documents/1.jpg -->E

正则匹配的优先级比前缀匹配优先级高

#前缀匹配
location  /sc { return  301 https://www.jd.com;
}
#正则匹配
location ~ /sc { return  301 https://www.cctv.com;
}
http://www.xdnf.cn/news/18550.html

相关文章:

  • 最新react,vue 解决无法使用js触发点击,解决方案
  • SamOutVXP: 轻量级高效语言模型
  • 通信工程学习:什么是Camera Calibration相机标定
  • WaitForSingleObject函数详解
  • python测试开发django-1.开始hello world!
  • 机器学习--聚类算法、集成算法
  • 集中式负载均衡 vs. 分布式负载均衡
  • 银河麒麟V10防火墙下访问NFS共享:端口开放全攻略
  • WindowsAPI|每天了解几个winAPI接口之网络配置相关文档Iphlpapi.h详细分析七
  • uniapp实现分页,效果如图
  • C++——C++重点知识点复习2(详细复习模板,继承)
  • 大模型部署
  • 基于随机森林的红酒分类与特征重要性分析
  • DeepSeek辅助编写的将xlsx格式文件中sheet1.xml按需分别保留或去掉标签的程序
  • 服务器间大文件迁移
  • 领域专用AI模型训练指南:医疗、法律、金融三大垂直领域微调效果对比
  • 电商项目_微服务_架构
  • 2025年国内AI大模型现状浅析
  • Shell 脚本条件测试
  • 一款更适合 SpringBoot 的API文档新选择(Spring Boot 应用 API 文档)
  • Rancher 管理的 K8S 集群中部署常见应用(MySQL、Redis、RabbitMQ)并支持扩缩容的操作
  • SpringBoot4发布!新特性解析
  • 2025.8.21总结
  • 【Bug】CentOS 7 使用vim命令报错vim: command not found
  • 37、需求预测与库存优化 (快消品) - /供应链管理组件/fmcg-inventory-optimization
  • AP状态管理中提到的两种“业务逻辑”
  • Java实现一个简单的LRU缓存对象
  • 50 C++ STL模板库-算法库 algorithm
  • python的校园研招网系统
  • RHCA10NUMA