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

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.dtcms.com/a/342396.html

相关文章:

  • Qt截图工具项目开发教程 - 从零开始构建系统截图工具
  • 【ARM】Keil MDK如何指定单文件的优化等级
  • 牛津大学xDeepMind 自然语言处理(5)
  • 基于 Kubernetes 的 WordPress 网站部署(使用 ConfigMap)
  • Spring两个核心IoCDI(一)
  • javaweb开发笔记—— 前端工程化
  • 当安全遇上资源瓶颈:轻量级加密为何成为 IoT 时代的刚需?
  • 基于 FPGA 的电磁超声脉冲压缩检测系统
  • 家里Windows,公司Linux?通过cpolar,WSL开发环境无缝切换
  • Python数据可视化利器:Matplotlib从入门到实战全解析
  • 今天我们继续学习计算机网络技术,Cisco软件,三层交换机以及RIP动态协议
  • 从零开始:JDK 在 Windows、macOS 和 Linux 上的下载、安装与环境变量配置
  • DeepSeek R2难产:近期 DeepSeek-V3.1 发布,迈向 Agent 时代的第一步
  • 《杠杆》电视剧分析学习
  • 【python与生活】如何从视频中提取关键帧?
  • JAVA-15 (2025.08.20学习记录)
  • 数据库面试常见问题
  • 【OpenGL】LearnOpenGL学习笔记13 - 深度测试、模板测试
  • 05 ODS层(Operation Data Store)
  • LeetCode算法日记 - Day 18: 只出现一次的数字、只出现一次的数字III
  • 通信工程学习:什么是Template Matching模版匹配
  • iOS 文件管理全景实战 多工具协同提升开发与调试效率
  • Python笔记 第三方库之Pandas的数据组合与缺失数据处理篇
  • 通信工程学习:什么是Camera Calibration相机标定
  • 1000qps怎么保证系统的高可用
  • abc Reachable Set
  • 基于Nodejs作为服务端,React作为前端框架,axios作为通讯框架,实现滑块验证
  • C++ 学习与 CLion 使用:(四)常量和变量,包括字面常量和符号常量
  • 计算机视觉--opencv(代码详细教程)(三)--图像形态学
  • 【框架篇二】FastAPI路由与请求处理