目录
- 一、Nginx Rewrite概述
- 1.1、 Nginx Rewrite概述
- 二、Nginx Rewrite基本操作
- 2.1、Rewrite命令
- 2.2、location分类和优先级
- 2.2.1、location分类
- 2.2.2、location优先级
- 2.2.3、location优先级规则
- 三、Rewrite使用场景实验
- 搭建基础环境
一、Nginx Rewrite概述
1.1、 Nginx Rewrite概述
概述:
现在Nginx已经成为很多公司作为前端反向代理服务器的首选,在实际工作中往往会
遇到很多跳转(重写URL)的需求。比如更换域名后需要保持旧的域名能跳转到新的域名上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求。如果在后端使用的Apache服务器,虽然也能做跳转,规则库也很强大,但是用Nginx跳转效率会更高。
-
跳转场景
1、可以调整用户浏览的URL,看起来更规范,合乎开发及产品人员的需求。
2、为了让搜索引擎搜录网站内容及用户体验更好,企业会将动态URL地址伪装成静态地址提供服务。
3、网址换新域名后,让旧的访问跳转到新的域名上。例如,访问京东的360buy.com会跳转到jd.com。
4、根据特殊变量、目录、客户端的信息进行URL调整等。 -
跳转实现
Nginx是通过ngx_http_rewrite_module模块支持url重写、支持if条件判断,但不支持else。另外该模块需要 PCRE支持,应在编译Nginx时指定PCRE 支持,默认已经安装。根据相关变量重定向和选择不同的配置,从一个location跳转到另一个location,不过这样的循环最多可以执行10次,超过后Nginx将返回500错误。同时,重写模块包含set指令,来创建新的变量并设其值,这在有些情景下非常有用的,如记录条件标识、传递参数到其他location、记录做了什么等等。rewrite功能就是,使用Nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。
- Rewrite实际场景
■Nginx跳转需求的实现方式
①使用rewrite进行匹配跳转
②使用if匹配全局变量后跳转
③使用location匹配在跳转
二、Nginx Rewrite基本操作
2.1、Rewrite命令
- Rewrite命令语法
rewrite < regex > < replacement > [flag]
regex:正则表达式
replacement :跳转后的内容
flag:rewrite支持的flag标记 - flag标记说明
标记 | 说明 |
---|---|
last | 相当于Apache的【L】标记,表示完成rewrite |
break | 本条规则匹配完成即终止,不在匹配后面的任何规则 |
redirect | 返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url |
permanent | 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新url |
- last 和 break 比较
last | break | |
---|---|---|
使用场景 | 一般写在server和if中 | 一般使用在location中 |
URL匹配 | 不终止重写后的url匹配 | 终止重写后的url匹配 |
2.2、location分类和优先级
2.2.1、location分类
- location= patt {} [精准匹配]
- location patt {} [一般匹配]
- location ~ patt {} [一般匹配]
标记 | 说明 |
---|---|
~ | 执行一个正则匹配,区分大小写 |
~* | 执行一个正则匹配,不区分大小写 |
!~ | 执行一个正则匹配,区分大小写不匹配 |
!~* | 执行一个正则匹配,不区分大小写不匹配 |
^~ | 普通字符匹配:使用前缀匹配,如果匹配成功,则不再匹配其他location |
= | 普通字符精确匹配,也就是完全匹配 |
@ | 定义一个命名的location,使用在内部定向时 |
2.2.2、location优先级
- 相同类型的表达式,字符串长的会优先匹配
- 按优先级排列
① = 类型
② ^~类型表达式
③ 正则表达式 (~和 ~*) 类型
④常规字符串匹配类型,按前缀匹配
⑤ 通用匹配(/),如果没有其他匹配,任何请求都会匹配到
2.2.3、location优先级规则
- 匹配某个具体文件
(location=完整路径) > (location ~ 完整路径) > (location ~ *完整路径) >(location~完整路径) > (location完整路径) > (location /) - 用目录做匹配访问某个文件
(location=目录) > (location ^ ~ 目录/) > (location ~ 目录 ) > (location~*目录) > (location目录) > (location /)
三、Rewrite使用场景实验
搭建基础环境
(1)、先把Nginx搭建好!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43[root@localhost opt]# tar xzvf nginx-1.15.9.tar.gz [root@localhost nginx-1.15.9]# useradd -M -s /bin/nologin nginx [root@localhost opt]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel [root@localhost opt]# cd nginx-1.15.9/ ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module make -j3 && make install #########优化路径######### [root@localhost nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ [root@localhost nginx-1.15.9]# ls -l /usr/local/sbin/nginx [root@localhost nginx-1.15.9]# nginx -t ### 检查配置文件 #########启动、重新配置、停止Nginx############ [root@localhost nginx-1.15.9]# nginx [root@localhost nginx-1.15.9]# netstat -anpt |grep nginx [root@localhost ~]# killall -s HUP nginx [root@localhost ~]# killall -s QUIT nginx #########添加Nginx系统服务########## [root@localhost ~]# vi /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/bin/kill -s HUP $MAINPID ExecStop=/usr/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target [root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service [root@localhost ~]# systemctl enable nginx.service [root@localhost ~]# systemctl start nginx
(2)、环境搭建
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.51xit.top; ### 更改域名 #charset koi8-r; access_log /var/log/nginx/www.51xit.top.access.log; ###改访问日志地址 location / { root html; index index.html index.htm; } [root@localhost ~]# mkdir -p /var/log/nginx [root@localhost ~]# touch /var/log/nginx/www.51xit.top.access.log
测试:打开网页www.51xit.top 显示正常
(3)、基于域名的跳转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.51xit.top; charset koi8-r; access_log /var/log/nginx/www.51xit.top.access.log; location / { root html; index index.html index.htm; if ($host = 'www.51xit.top') ###这边改一下本机域名 { rewrite ^/(.*)$ http://www.52xit.top/$1 permanent; ###这边改下跳转后的域名 } }
测试:打开浏览器输入www.51xit.top它会跳转到www.51xit.top中去
(3)、基于客户端IP地址的跳转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.51xit.top; listen 80; server_name www.51xit.top; charset utf-8; access_log /var/log/nginx/www.51xit.top.access.log; set $rewrite true; if($remote_addr = "20.0.0.6") { set $rewrite false; access_log /var/log/nginx/www.51xit.top.access.log; set $rewrite true; if($remote_addr = "20.0.0.6"){ set $rewrite false; } if ($rewrite = true){ rewrite (.+) /wh.html; } location = /wh.html { root /usr/local/nginx/html/; } location / { root html; index index.html index.htm; } [root@localhost ~]# cd /usr/local/nginx/html/ [root@localhost html]# cp index.html wh.html [root@localhost html]# vi wh.html ###里面稍微修改一下,可以改成维护中。 [root@localhost html]# systemctl restart nginx.service ###重启nginx服务
测试:客户端浏览器输入www.51xit.top自动跳转到维护页面
(4)、基于旧域名跳转到新域名后面加目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.51xit.top; ###改域名 charset utf-8; ###改字符集 access_log /var/log/nginx/www.51xit.top.access.log; ###改日志存储路径,去掉main location /bbs { rewrite (.+) http://www.52xit.top/new$1 permanent; } [root@localhost html]# systemctl restart nginx.service ###重启nginx服务
测试:客户端浏览器输入www.51xit.top/bbs自动跳转新域名www.52xit.top/bbs
(5)、基于参数匹配的跳转
1
2
3
4
5
6
7
8
9
10
11
12
13
14[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.51xit.top; charset utf-8; access_log /var/log/nginx/www.51xit.top.access.log; if ($request_uri ~ ^/100-(100|200)-(d+).html$) { rewrite (.*) http://www.51xit.top permanent; } [root@localhost html]# systemctl restart nginx.service ###重启nginx服务
测试:客户端浏览器输入www.51xit.top/100-100-100.html 页面显示www.51xit.top
客户端浏览器输入www.51xit.top/100-200-100.html 页面显示www.51xit.top
客户端浏览器输入www.51xit.top/100-200-10dsa0.html 页面不能跳转,因为有英文字符了,不是数字
(5)、基于目录下所有php结尾的文件跳转,访问www.51xit.top/upload/1.php跳转到首页
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.51xit.top; charset utf-8; access_log /var/log/nginx/www.51xit.top.access.log; location ~* /upload/.*.php$ { rewrite (.+) http://www.51xit.top permanent; } [root@localhost html]# systemctl restart nginx.service ###重启nginx服务
测试:访问www.51xit.top/upload/1.php跳转到www.51xit.top
(6)、基于最普通一条url请求的跳转,访问一个具体的页面跳转到首页
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.51xit.top; charset utf-8; access_log /var/log/nginx/www.51xit.top.access.log; location ~* ^/1/test.html { rewrite (.+) http://www.51xit.top permanent; } [root@localhost html]# systemctl restart nginx.service ###重启nginx服务
测试:访问www.51xit.top/1/test.html跳转到www.51xit.top首页上
(7)、基于报错代码来跳转的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf server { listen 80; server_name www.51xit.top; charset utf-8; access_log /var/log/nginx/www.51xit.top.access.log; error_page 500 502 503 504 /index.html; location = /index.html { root html; } [root@localhost html]# systemctl restart nginx.service ###重启nginx服务
最后
以上就是悲凉丝袜最近收集整理的关于理论+实验:Nginx Rewrite(基于域名跳转、ip地址跳转、旧域名跳新域名、参数匹配跳转、基于目录php结尾文件跳转、基于url跳转、报错代码跳转)一、Nginx Rewrite概述二、Nginx Rewrite基本操作三、Rewrite使用场景实验的全部内容,更多相关理论+实验:Nginx内容请搜索靠谱客的其他文章。
发表评论 取消回复