文章目录
Rewrite
Rewrite跳转场景
URL看起来更规范,合理
企业会将动态URL地址伪装成静态地址提供服务
网址换新域名,让旧的访问跳转到新的域名上
服务端某些业务调整
Rewrite跳转实现
Rewrite实际场景
●Nginx跳转需求的实现方式
- 使用rewrite进行匹配跳转
- 使用if匹配全局变量后跳转
- 使用location匹配再跳转
●rewrite放在server{},if{},location{}段中
- Location只对域名后边的出去传递参数外的字符串起作用
●对域名或参数字符串
- 使用if全局变量匹配
- 使用proxy_pass反向代理
Nginx正则表达式
\d:匹配数字[0-9]
\d+ :0-无穷大
Rewrite命令
命令语法
flag标记说明
last和break比较
Location
Location分类
Location = patt {} [精准匹配]
Location patt {} [一般匹配]
Location ~ patt {} [正则匹配]
正则匹配的常用表达式
Location优先级
相同类型的表达式,字符串长的会优先匹配
按优先级列
- = 类型
- ^~ 类型表达式
- 正则表达式
(~和~*)
类型 - 常规字符串匹配类型,按前缀匹配
- 通用匹配 (/),如果没有其他匹配,任何请求都会匹配到
比较rewrite和location
相同点
- 都能实现跳转
不同点
- rewrite是在同一域名内更改获取资源的路径
- location是一类路径做控制访问或反向代理,还可以proxy_pass到其他机器
rewrite会写在location里,执行顺序
- 执行server块里面的rewrite指令
- 执行location匹配
- 执行选定的location中的rewrite指令
location优先级示例
location = / { '//精确匹配 /,主机名后面不能带任何字符串'
[configuraion A ]
}
location / { '//所有的地址都以/开头,这条规则将匹配到所有请求,但正则和最长字符串会优先匹配'
[configuraion B ]
}
location /documents/ { '//匹配任何以/documents/开头的地址,当后面的正则表达式没有匹配到时,才起作用'
[configuraion C ]
}
location ~ /documents/abc { '//匹配任何以/documents/abc开头的地址,当后面的正则表达式没有匹配到时,才会起作用'
[configuraion D ]
}
location ^~ /images/ { '//以/images/开头的地址,匹配符合后,停止往下匹配'
[configuraion E ]
}
location ~*\.(gif|jpg|gpeg)$ { '//匹配所有以 gif, jpg或jpeg结尾的请求, Images/下的图片会被 [configuration E]处理,因为^~的优先级更高'
[configuraion F ]
}
location /images/abc { '//最长字符匹配到 /images/abc,优先级最低'
[configuraion G ]
}
location ~ /images/abc { '//以/ Images/abc开头的,优先级次之'
[configuraion H ]
}
location /images/abc/1.html { '//如果和正则 ~ images/abc/1.htm相比,正则优先级更高'
[configuraion I ]
}
location的优先级规则
匹配某个具体文件
- (location = 完整路径) > (location ^~完整路径) > (location ~* 完整路径) > (location ~ 完整路径) > (location /)
用目录做匹配访问某个文件
- (location = 目录)> (location ^~目录)> (location ~ 目录)> (location ~* 目录)> (location 目录)> (location /)
实验应用
环境准备
一台centos7服务器
一台Windows主机
实验一:基于域名的跳转
需求:公司旧域名www.benet.com,因业务需求有变更,需要使用新域名www.newbenet.com代替
推荐步骤:
1.先关闭临时防护系统,清空防火墙规则
[root@promote ~]# setenforce 0
[root@promote ~]# iptables -F
2.用rpm安装nginx的依赖包
[root@promote~]#rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@promote ~]# yum -y install nginx ##安装nginx服务
3.安装DNS服务工具,并启动服务
[root@promote ~]# yum -y install bind
[root@promote ~]# vim /etc/named.conf ##修改主配置文件内容
[root@promote ~]# vim /etc/named.rfc1912.zones ##配置区域配置文件
[root@promote named]# cp -p named.localhost benet.com.zone
[root@promote named]# vim benet.com.zone ##修改区域数据配置文件
[root@promote named]# cp -p benet.com.zone newbenet.com.zone [root@promote named]# systemctl start named
4.配置nginx的配置文件,写入需求
location / {
if ($host = 'www.benet.com') {
rewrite ^/(.*)$ http://www.newbenet.com/$1 permanent;
}
5.配置win系统的虚拟机的DNS地址为centos的地址
6.这时输入www.benet.com的地址时域名变为newbenet.com
实验二:基于客户端IP访问跳转
需求:今天公司业务版本上线,所有IP访问任何内容都显示一个固定维护页面,只有公司地址访问正常
1.修改配置文件,写入脚本
#判断标志$rewrite
set $rewrite ture;
#允许公司内部访问,更改标志为false
if ($remote_addr = "192.168.148.200") {
set $rewrite false;
}
#如果不是公司IP,加上后缀地址作为标识
if ($rewrite = ture){
rewrite (.+) /error.html;
}
location = /error.html {
root /usr/share/nginx/html;
}
2.到站点目录下写入error.html内容
[root@localhost html]# cd /usr/share/nginx/html/
3.重启服务,回到win10验证功能
4.将地址改为148.150查看访问情况
实验三:基于旧,新域名跳转并加目录
需求:将域名http://bbs.benet.com下面的发帖都跳转到http://www.benet.com/bbs,且域名跳转后保持参数不变
1.修改配置文件,写入脚本
location /post {
rewrite (.+) http://www.benet.com/bbs$1 permanent;
}
[root@localhost html]# vim /var/named/benet.com.zone ##修改区域数据配置文件内容
[root@localhost html]# systemctl restart nginx ##重启服务
[root@localhost html]# echo "nameserver 192.168.148.135" > /etc/resolv.conf
2.打开火狐浏览器验证结果
实验四:基于参数匹配的跳转
需求:例如现在访问http://www.old.com/100-(100|200)-100.html跳转到http://www.new.com页面
1.修改配置文件,添加以下脚本
#charset koi8-r;
access_log /var/log/nginx/host.access.log main;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.*) http://www.benet.com permanent;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
2.重启服务,回到win10上输入地址验证
实验五:基于目录下所有php文件跳转
需求:访问任意一个页面跳转到首页,即http://www.old.com/abc/任意.php
1.修改配置文件
access_log /var/log/nginx/host.access.log main;
location ~* ^/abc/.*\.php$ {
rewrite (.+) http://www.benet.com permanent;
}
2.重启服务,回到win中访问查看
实验六:基于最普通url请求的跳转
需求:访问一个具体的页面跳转到首页
1.修改配置文件,写入脚本
#charset koi8-r;
access_log /var/log/nginx/host.access.log main;
location ~* ^/abc/123.html {
rewrite (.+) http://www.benet.com permanent;
}
2.重启服务回到win10上访问,验证结果