Rewrite跳转场景
- URL看起来更规范、合理
- 企业会将动态URL地址伪装成静态地址提供服务
- 网址换新域名后,让旧的访问跳转到新的域名上
- 服务端某些业务调整
注:在前面的Nginx防盗链中使用过
Rewrite跳转实现
Rewrite实际场景
1、Nginx跳转需求的实现方式
- 使用rewrite进行匹配跳转
- 使用if匹配全局变量后跳转
- 使用location匹配再跳转
2、rewrite放在server{},if{},location{}段中
- location只对域名后边的除去传递参数外的字符串起作用
3、对域名或参数字符串
- 使用if全局变量匹配
- 使用proxy_pass反向代理
Nginx正则表达式
定义
为了匹配某些条件而产生的字符,对条件的一种描述与匹配,表达方式为字符、数字、各种字符等
字符 | 说明 |
---|---|
^ | 匹配输入字符串的起始位置 |
$ | 匹配输入字符串的结束位置 |
* | 匹配前面的字符零次或多次 |
+ | 匹配前面的字符一次或多次 |
? | 匹配前面的字符零次或一次 |
. | 匹配除“In”之外的任何单个字符 |
\ | 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用 |
\d | 匹配纯数字 |
{n} | 重复n次 |
{n,} | 重复n次或更多次 |
[c] | 匹配单个字符c |
[a-z] | 匹配a-z小写字母的任意一个 |
[a-zA-Z] | 匹配a-z小写字母或A-Z大写字母的任意一个 |
Rewrite命令
Rewrite命令语法
rewrite <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匹配 |
last:会循环匹配条件直至10次
break:执行完一次便终止
location
location分类
- location = patt {}[精准匹配]
- location patt {}[一般匹配]
- location ~ patt {}[正则匹配]
正则匹配的常用表达式
标记 | 说明 |
---|---|
~ | 执行一个正则匹配,区分大小写 |
~* | 执行一个正则匹配,不区分大小写 |
!~ | 执行一个正则匹配,区分大小写不匹配,相当于取反 |
!~* | 执行一个正则匹配,不区分大小写不匹配,相当于取反 |
^~ | 普通字符匹配;使用前缀匹配。如果匹配成功,则不再匹配其他location |
= | 普通字符精确匹配。也就是完全匹配 |
@ | 定义一个命名的 location,使用在内部定向时 |
location优先级
1、相同类型的表达式,字符串长的会优先匹配
2、按优先级排列
=类型 //精确匹配
^~类型表达式 //普通匹配,前缀
正则表达式(~和~*)类型
常规字符串匹配类型,按前缀匹配
通用匹配(/),如果没有其它匹配,任何请求都会匹配到
location优先级规则
匹配某个具体文件
- (location =完整路径)>(location ^~ 完整路径)>(location ~* 完整路径)=(location ~完整路径)>(location完整路径)>(location /)
用目录做匹配访问某个文件
- (location = 目录)>(location ^~ 目录/)>(location ~ 目录)=(location ~*
目录)>(location目录)>(location /)
比较rewrite和location
相同点
- 都能实现跳转
不同点
- rewrite是在同一域名内更改获取资源的路径
- location是对一类路径做控制访问或友向代理,还可以proxy_pass到其他机器
rewrite会写在location里,执行顺序执行
- server块里面的rewrite指令
- 执行location匹配
- 执行选定的location中的rewrite指令
安装Nginx服务
步骤
- 安装nginx源
- 安装nginx软件包
- 修改默认站点配置文 件:etc/nginx/conf.d/default.conf
- 启动nginx
注意
- 确定域名可以正常解析
- 做下一个场景前,要删除上一个场景的配置
- 及时清除浏览器缓存
项目
基于域名跳转
公司旧域名www.domain.com,因业务需求有变更,需要使用新域名www.domain.com代替
- 不能废除旧域名
- 从旧域名跳转到新域名,且保持其参数不变
修改配置文件
[root@localhost ~]# vi /etc/nginx.conf
server { //修改
listen 80;
server_name www.domain.com;
if ($host = 'www.domain.com')
{
rewrite ^/(.*)$ http://www.newdomain.com/$1 permanent;
}
charset utf-8;
access_log logs/www.domain.access.log main;
server { //添加
listen 80;
server_name www.newdomain.com;
charset utf-8;
access_log /var/log/nginx/www.newdomain.com-access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
[root@localhost ~]# mkdir /var/log/nginx //创建目录
[root@localhost ~]# mkdir -p /usr/share/nginx/html //创建文件
[root@localhost ~]# echo "this is a rewrite." > /usr/share/nginx/html/index.html
[root@localhost ~]# cat /usr/share/nginx/html/index.html
this is a rewrite. //查看内容
修改本机映射
在上图目录下hosts文件中添加映射
验证
输入:www.domain.com,自动将域名跳转为www.newdomain.com
抓包验证
基于客户端IP访问跳转
方法一
今天公司业务版本上线,所有IP访问任何内容都显示一个固定维护页面,只有公司IP访问正常
配置文件
[root@localhost ~]# vi /etc/nginx.conf
server { //修改一下内容
listen 80;
server_name www.domain.com;
set $rewrite true;
if ($remote_addr = "192.168.100.10") {
set $rewrite false;
}
if ($rewrite = true) {
rewrite (.+) /maintenance.html;
}
location = /maintenance.html {
root /usr/share/nginx/html;
}
charset utf-8;
access_log logs/www.domain.access.log main;
修改需要重定向的网页
[root@localhost ~]# vi /usr/local/nginx/html/maintenance.html
Website is Maintaining,Please visit later. //添加内容
验证配置准确性并重启服务
[root@localhost ~]# 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@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
验证网页
方法二
临时修改IP地址
[root@client1 ~]# ifconfig ens33 192.168.100.20
添加映射
[root@client1 ~]# vi /etc/hosts
192.168.100.20 www.domain.com //添加
本地访问验证
基于旧、新域名跳转并加目录
将域名http://bbs.domain.com下面的发帖都跳转到
http://www.domain.com/bbs且域名跳转后保持参数不变
配置文件
[root@localhost ~]# vi /etc/nginx.conf
server { //修改
listen 80;
server_name bbs.domain.com;
charset utf-8;
access_log logs/bbs.domain.access.log main;
location /post {
rewrite (.+) http://www.domain.com/bbs$1 permanent;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
......
server { //添加
listen 80;
server_name www.domain.com;
charset utf-8;
access_log /var/log/nginx/www.domain.com-access.log main;
location / {
root html;
index index.html index.htm;
}
}
验证配置
[root@localhost ~]# 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
创建html文件
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# mkdir -p bbs/post //创建目录
[root@localhost html]# cd bbs/post/ //进入目录
[root@localhost post]# vi 1.html
tiangou //添加内容
添加映射
[root@localhost ~]# vi /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.100.10 www.domain.com bbs.domain.com
重启服务
[root@localhost post]# cd
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
验证配置
基于参数匹配的跳转
配置文件
[root@localhost ~]# vi /etc/nginx.conf
server {
listen 80;
server_name www.domain.com;
charset utf-8;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) { //添加正则表达式
rewrite (.*) http://www.domain.com permanent;
}
access_log logs/www.domain.access.log main;
location /post {
rewrite (.+) http://www.domain.com/bbs$1 permanent;
}
location / {
root html;
index index.html index.htm;
}
验证配置
[root@localhost ~]# 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
访问验证
基于目录下所有PHP文件跳转
访问http://www.domain.com/upload/1.php跳转到首页
配置文件
server {
listen 80;
server_name www.domain.com;
charset utf-8;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.domain.com permanent;
}
access_log logs/www.domain.access.log main;
location / {
root html;
index index.html index.htm;
}
验证配置
[root@localhost ~]# 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
访问验证
基于最普通URL请求的跳转
要求:访问一个具体的页面跳转到首页
验证:浏览器访问http://www.kgc.com/1/test.html跳转到首页
配置文件
server {
listen 80;
server_name www.domain.com;
charset utf-8;
location ~* ^/1/test.html {
rewrite (.+) http://www.domain.com permanent;
}
access_log logs/www.domain.access.log main;
location / {
root html;
index index.html index.htm;
}
验证配置
[root@localhost ~]# 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
访问验证