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;
}