目录
一、环境准备
一台nginx服务器提供 www.test.com 的网页。
1、安装rpm源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2、直接用yum安装nginx和bind
yum install nginx bind -y
3、DNS域名解析
vim /etc/named.conf
vim /etc/named.rfc1912.zones
#复制修改
zone "test.com" IN {
type master;
file "test.com.zone";
allow-update { none; };
};
cd /var/named/
cp -p named.localhost test.com.zone
4、修改nginx配置文件
vim /etc/nginx/conf.d/default.conf
5、启动服务,关闭防火墙
systemctl start named
systemctl start nginx
systemctl stop firewalld
setenforce 0
二、Rewrite 介绍
2.1、Rewrite跳转场景
- URL 看起来更规范、合理;
- 企业会将动态URL地址伪装成静态地址提供服务;
- 网址换域名后,让旧的访问跳转到新的域名上;
- 服务端某些业务调整
2.2、Rewrite 实用场景
1、Nginx跳转需求的实现方式:
- 使用rewrite进行匹配跳转;
- 使用 if 匹配全局变量后跳转;
- 使用 location 匹配再跳转
2、rewrite放在 server { },if { };location{ }段中;
3、对域名或参数字符串:
- 使用 if 全局变量匹配;
- 使用 proxy_pass 反向代理
2.3、常用的正则表达式元字符
字符 | 说明 |
---|---|
A | 匹配输入字符串的起始位置 |
$ | 匹配输入字符串的结束位置 |
* | 匹配前面的字符零次或多次 |
+ | 匹配前面的字符一次或多次 |
? | 匹配前面的字符零次或一-次 |
. | 匹配除"\n"之外的任何单个字符。使用诸如“[.\n]“之类的模式,可匹配包括”\n"在内的任意字符 |
\d | 将后面接着的字符标记为一一个特殊字符或一个原义字符或一个向后引用匹配纯数言 |
{n} | 重复n次 |
{n,} | 重复n次或更多次 |
[c] | 匹配单个字符c |
[a-z] | 匹配a-z小写字母的任意一个 |
[a-zA-Z] | 匹配a-z小写字母或A-Z大写字母的任意一个 |
2.4、Rewrite 命令
语法:
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.5、location 分类
分类:
- location = patt { } [精准匹配]
- location patt { } [一般匹配]
- location ~ patt { } [正则匹配]
正则匹配的常用表达式:
标记 | 说明 |
---|---|
~ | 执行一个正则匹配,区分大小写 |
~* | 执行一个正则匹配,不区分大小写 |
!~ | 执行一个正则匹配,区分大小写不匹配 |
!~* | 执行一个正则匹配,不区分大小写不匹配 |
A~ | 普通字符匹配;使用前缀匹配。如果匹配成功,则不再匹配其他location |
= | 普通字符精确匹配。也就是完全匹配 |
@ | 定义一个命名的location,使用在内部定向时 |
2.6、location 优先级
相同类型的表达式,字符串长的会优先匹配;
按优先级排列:
- = 类型
- ^~ 类型表达式
- 正则表达式(~ 和 ~*)类型
- 常规字符串匹配类型,按前缀匹配
- 通用匹配(/),如果没有其他匹配,任何请求都会匹配到
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 /)
2.7、比较rewrite和location
相同点
- 都能实现跳转
不同点
- rewrite是在同一域名内更改获取资源的路径
- location是对一类路径做控制访问或反向代理,还可以proxy_pass到其他机器
rewrite会写在location里,执行顺序
- 执行server块里面的rewrite指令
- 执行location匹配
- 执行选定的location中的rewrite指令
三、具体场景
3.1、场景一:基于域名的跳转
实验环境:公司旧域名www.test.com,因业务需求有变更,需要使用新域名www.newtest.com代替.
需求:
不能废除旧域名
从旧域名跳转到新域名,且保持其参数不变
1、修改nginx的配置文件
vim /etc/nginx/conf.d/default.conf
if ($host = "www.test.com"){
rewrite ^/(.*)$ http://www.newtest.com/$1 permanent;
}
#域名重定向:表示当访问域名www.test.com时,将自动跳转到www.newtest.com域名,permanent表示永久的意思
2、DNS服务提供新域名的解析
vim /etc/named.rfc1912.zones
#复制之前的test域名声明段修改
zone "newtest.com" IN {
type master;
file "newtest.com.zone";
allow-update { none; };
};
cd /var/named/
cp -p test.com.zone newtest.com.zone
3、重启服务
systemctl stop nginx
systemctl start nginx
systemctl restart named
验证:在浏览器中输入新域名www.newtest.com
3.2、场景二:基于客户端IP地址访问跳转
实验要求:今天公司业务版本上线,所有IP访问任何内容都显示一个固定维护页面,只有公司的IP才能访问正常。
公司IP地址:10.0.0.40
PC客户端:10.0.0.60
把上一个实验的nginx配置部分删除,以防影响下面的实验。
1、修改nginx的配置文件,重启服务
vim /etc/nginx/conf.d/default.conf
#设置是否合法的IP标志
set $rewrite true;
#判断是否为合法IP
if ($remote_addr = "10.0.0.40“){
set $rewrite false;
}
#非法IP进行判断打上标记
if ($rewrite = true){
rewrite (.+) /main.html;
}
#匹配标记进行跳转站点
location = /main.html {
root /usr/share/nginx/html;
}
systemctl stop nginx
systemctl start nginx
2、给 main.html 添加自定义页而内容
cd /usr/share/nginx/html
vim main.html
<html>
<head>
<meta charset="utf-8">
<title>test网站</title>
</head>
<body>
<h1>网站维护中,请稍等~~~</h1>
</body>
</html>
用公司的IP地址访问:
通过客户端IP地址访问:
3.3、场景三:基于旧、新域名跳转并加目录
例如:现在访问的是 http://ftp.test.com ,现在需要将这个域名下面的发帖都跳转到 http://www.test.com/ftp,注意保持域名跳转后的参数不变。
1、在nginx配置文件中添加以下代码(删除之前的配置)
vim /etc/nginx/conf.d/default.conf
server_name ftp.test.com;
...省略内容
location /post {
rewrite (.+) http://www.test.com/ftp$1 permanent;
}
修改DNS区域数据配置文件
[root@localhost named]# vim test.com.zone
#将www修改为ftp,否则客户机则无法解析地址
ftp IN A 10.0.0.40
指定DNS
echo "nameserver 10.0.0.40" > /etc/resolv.conf
重启DNS、Nginx服务
[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl restart nginx
在centos7上访问 http://ftp.test.com/post/a.html ,会帮我们自动跳转 http://www.test.com/ftp/post/a.html , 此时域名跳转后的参数并没有变还是ftp
3.4、场景四:基于参数匹配跳转
例如:浏览器访问http://www.test.com/100-(100|200)-100.html,会自动跳转到 http://www.test.com 的页面。
1、修改nginx的配置文件,添加以下代码
vim /etc/nginx/conf.d/default.conf
server_name www.test.com;
...省略内容
if ($request_uri ~ ^/100-(100|200)-(\d+).html$){
rewrite (.*) http://www.test.com permanent;
}
2、DNS解析www。并重启服务
vim /var/named/test.com.zone
#最后一行改为:
www IN A 10.0.0.40
systemctl stop nginx
systemctl start nginx
systemctl restart named
3、在浏览器访问 http://www.test.com/100-100-100.html,就会帮我们自动跳转到www.test.com网站
3.5、场景五:基于目录下所有php文件跳转
例如,我们访问 http://www.test.com/upload/1.php,会自动跳转到首页www.test.com。
1、修改nginx的配置文件,添加以下代码
vim /etc/nginx/conf.d/default.conf
server_name www.test.com;
...省略内容
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.test.com permanent;
}
2、重启服务
systemctl stop nginx
systemctl start nginx
3、在浏览器上访问 http://www.test.com/upload/1.php ,就会帮我们自动跳转到 www.test.com网页。
3.6、场景六:基于最普通 url 请求的跳转
场景说明:我们访问一个具体的页面都会帮我们跳转到首页www.test.com
例如:我们在网页上访问一个具体网址 http://www.test.com/abc/1.html ,就会帮我们跳转到首页 www.test.com。
1、在nginx配置文件中添加以下代码
vim /etc/nginx/conf.d/default.conf
server_name www.test.com;
...省略内容
location ~* /abc/1.html{
rewrite (.+) http://www.test.com permanent;
}
2、重启服务
systemctl stop nginx
systemctl start nginx
3、验证:浏览器访问 http://www.test.com/abc/1.html,会自动跳转到 www.test.com