rewrit的项目心得与总结
一、rewrite跳转场景
1.1、URL看起来更规范、合理
1.2、企业会将动态URL地址伪装成静态地址提供服务
1.3、网站换新域名后,让旧的访问跳转到新的域名上
1.4、服务端某些业务调整
二、rewrite的跳转实现

三、rewrite的实现场景
1、Nginx跳转需求的实现方式
2、使用rewrite进行匹配跳转
3、使用if匹配全局变量后跳转
4、使用location匹配再跳转
5、rewrite放在server{},if{},location{}段中
6、location只对域名后边的除去传递参数外的字符串起作用
7、对域名或参数字符串
8、使用if全局变量匹配
9、使用proxy_pass反向代理
四、rewrite命令
1 rewrite <regex> <replacement> [flag]; 2 <regex>:正则表达式 3 <replacement>:跳转后的内容 4 [flag]rewrite支持的flag标记
flag标记说明
|
标记 |
说明 |
|
last |
相当于Apache的[L]标记,表示完成rewrite |
|
break |
本条规则匹配完成即终止,不再匹配后面的任何规则 |
|
redirect |
返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url |
|
permanent |
返回301永久重定向,浏览器地址会显示跳转后的URL地址,爬虫更新url |
1)last:url重写后,马上发起一个新请求,再次进入server块,重试location匹配,超过10次匹配不到报500错误,地址栏不变
2)break:url重写后,直接使用当前资源,不再使用location余下的语句,完成本次请求,地址栏不变
总结:last和break再重定向后,地址栏都不会发生变化,这是他们的相同点,不同点在于last会写在server和if中,break是写在location中,last不会终止重写后的url匹配,break会终止重写后的url匹配
五、location的分类
1 location = patt{} [精准匹配]
2 location patt {} [一般匹配]
3 location ~ patt {} [正则匹配]
正则匹配的常用表达式
|
标记 |
说明 |
|
~ |
执行一个正则匹配,区分大小写 |
|
~* |
执行一个正则匹配,不区分大小写 |
|
!~ |
执行一个正则匹配,区分大小写不匹配 |
|
!~* |
执行一个正则匹配,不区分大小写不匹配 |
|
^~ |
普通字符匹配,使用前缀匹配,如果匹配成功,则不再匹配其他location |
|
= |
普通字符精确匹配,也就是完全匹配 |
|
@ |
定义一个命名的location,使用再内部定向时 |
location的优先级
相同类型的表达式,字符串长的会优先匹配
按优先级排列
1 = 类型 2 ^~ 类型表达式 3 正则表达式(~和~*)类型 4 常规字符串匹配类型,按前缀匹配 5 通用匹配(/),如果没有其他匹配,热河请求都会匹配到
location优先级的示例
1 location = / { ①精确匹配/,主机名后面不能带任何字符串
2 [configuration A]
3 }
4 location ~ /documents/abc { ②匹配任何以/documents/abc开头的地址,当后面的正则表达式没有匹配到时,才会起作用
5 [configuration B]
6 }
7 location /documents/ { ③匹配任何以/documents/开头的地址,当后面的正则表达式没有匹配到时,才起作用
8 [configuration C]
9 }
10 location / { ④所有的地址都以/开头,这条规则将匹配到所有请求,但正则表达式和最长字符会优先匹配
11 [configuration D]
12 }
13location ^~ /images/ { ①以/images/开头的地址,匹配符合后,停止往下匹配
14[configuration E]
15}
16ocation ~* \.(gif|jpg|jpeg)$ { ②匹配所有以gif,jpg或jpeg结尾的请求,因为^~的优先级最高
17[configuration F]
18}
19location ~ /images/abc { ③以/images/abc开头的,优先级次之
20[configuration G]
21}
22 location /images/abc/1.html { ④如果和正则~ /images/abc/1.html相比,正则优先级最高
23 [configuration H]
24 }
25 location /images/abc { ⑤最长字符匹配到/images/abc,优先级最低
26 [configuration I]
27 }
六、对location和rewrite进行比较
1、相同点:都能实现跳转
2、不同点:rewrite是在同一个域名内更改获取资源的路径
location是对一类路径控制访问或反向代理,还可以proxy_pass到其他机器
3、rewrite会写在location里,执行顺序
①、执行server快里面的rewrite指令
②、执行location匹配
③、执行选定的location中的rewrite指令
4、location优先级规则
匹配某个具体文件
(location = 完整路径)>(location ^~ 完整路径)>(location ~* 完整路径)=(location ~ 完整路径)>(location /)
用目录做匹配访问某个文件
(location = 目录)>(location ^~ 目录)>(location ~* 目录)=(location ~ 目录)>(location /)
六、实例说明
1、基于域名的跳转
公司的旧域名www.old.com 因为业务需求变更,需要使用新域名www.new.com代替
不能废除旧域名
从旧域名跳转到新域名,切保持其参数不变
2、修改新站点配置文件(基于LNMP)
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.old.com;
charset utf-8;
access_log logs/host.access.log main;
if ($host = 'www.old.com')
{
rewrite ^/(.*)$ http://www.new.com/$1 permanent;
}
location / {
root /usr/local/share/html;
index index.html index.htm;
检查nginx配置文件并重启nginx
[root@server3 ~]# 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@server3 ~]# systemctl restart nginx
创建新站点的网页,添加主机映射
vi /etc/hosts 20.0.0.30 www.old.com www.new.com [root@server3 ~]# mkdir -p /usr/local/share/html [root@server3 ~]# cd /usr/local/share/html/ [root@server3 html]# vi index.html [root@server3 html]# vi /usr/local/nginx/conf/nginx.conf <html><body><h1>this is new</h1></body></html>
客户端测试


2、基于客户端IP访问跳转
今天我们公司新业务版本上线。所有ip访问任何内容都会显示一个正在维护的页面,只有公司的ip才能正常访问,
修改配置文件
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.old.com;
set $rewrite true;
if ($remote_addr = "20.0.0.30"){
set $rewrite false;
}
if ($rewrite = true){
rewrite (.+) /maintenance.html;
}
location = /maintenance.html {
root /usr/local/nginx/html;
}
检查配置文件,重启nginx
nginx -t systemctl restart nginx
修改重定向的网页
vi /usr/local/nginx/html/maintenance.html <html><body><h1>website is maintaing,please visit later</h1></body></html>
测试



3、基于新域名,旧域名跳转并加目录
修改配置文件
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.old.com;
charset utf-8;
access_log logs/host.access.log main;
location /post
{
rewrite (.+) http://www.new.com/bbs$1 permanent;
}
末尾添加
server {
listen 80;
server_name www.new.com;
charset utf-8;
access_log logs/host.access.log main;
location /
{
root /usr/local/share/html;
index index.html index.htm;
}
}
检查配置文件 并重启nginx
nginx -t systemctl restart nginx
创建BBS目录
cd /usr/local/share/html/ mkdir bbs mv index.html bbs/ cd bbs/ mkdir post mv index.html post
测试


4、基于参数匹配的跳转
修改配置文件
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.old.com;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$)
{
rewrite (.+) http://www.old.com permanent;
}
#charset koi8-r;
#access_log logs/host.access.log main;
j检查配置文件,重启nginx
nginx -t systemctl restart nginx
进行测试



浙公网安备 33010602011771号