一:Rewrite跳转理论知识:
1,Rewrite实用场景
- Nginx跳转需求的实现方式
使用rewrite进行匹配跳转
使用if匹配全局变量后跳转
使用location匹配再跳转
-
rewrite放在server{}, if{}, location{} 段中
-
对域名或参数字符串
使用if全局变量匹配
使用proxy_pass反向代理
2,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匹配 | 终止重写后的ur|匹配 |
3,常用的正则表达式元字符
字符 | 说 明 |
---|---|
^ | 匹配输入字符串的起始位置 |
$ | 匹配输入字符串的结束位置 |
* | 匹配前面的字符零次或多次 |
+ | 匹配前面的字符一次或多次 |
? | 匹配前面的字符零次或一次 |
. | 匹配除"\n"之外的任何单个字符。使用诸如"[.\n]“之类的模式,可匹配包括”\n"在内的任意字符 |
\ | 将后面接着的字符标记为一个特殊字符或一个原义字符或一个向后引用 |
\d | 匹配纯数字 |
{n} | 重复n次 |
{n,} | 重复n次或更多次 |
[c] | 匹配单个字符c |
[a-z] | 匹配a-z小写字母的任意一个 |
[a-zA-Z] | 匹配a-z小写字母或A-Z大写字母的任意一个 |
4,正则匹配的常用表达式
标记 | 说明 |
---|---|
~ | 执行一个正则匹配,区分大小写 |
~* | 执行一个正则匹配,不区分大小写 |
!~ | 执行一个正则匹配,区分大小写 不匹配 |
!~* | 执行一个正则匹配,不区分大小写 不匹配 |
^~ | 普通字符匹配;使用前缀匹配。如果匹配成功,则不再匹配其他location |
= | 普通字符精确匹配。也就是完全匹配 |
@ | 定义一个命名的location,使用在内部定向时 |
5,location优先级:
■相同类型的表达式,字符串长的会优先匹配
■按优先级排列:
● = 类型
● ^~ 类型表达式
●正则表达式(和*)类型
●常规字符串匹配类型,按前缀匹配
●通用匹配(/) ,如果没有其它匹配,任何请求都会匹配到
6,location分类:
location = patt { } [精准匹配]
location patt { } [一般匹配]
location ~ patt { } [正则匹配]
比较rewrite和location:
■相同点
●都能实现跳转
■不同点
●rewrite是在同一域名内更改获取资源的路径
●location是对一类路径做控制访问或反向代理,还可以proxy_ pass到其他主机
■ rewrite会写在location里,执行顺序
●执行server块里面的rewrite指令
●执行location匹配
●执行选定的location中的rewrite指令
7,location优先级规则:
■匹配某个具体文件
●(location = 完整路径) > (location ^~完整路径) > (location ~* 完整路径) > (location ~完整路径) > (location 完整路径) > (location /)
■用目录做匹配访问某个文件
●(location = 目录) > (location ^~ 目录/)> (location ~ 目录) >(location ~* 目录) > (location 目录) > (location /)
一:基于域名访问跳转
公司旧域名为www.accp.com,因业务需求有变更,需要使用新域名www.kgc.com代替
●不能废除旧域名的同时
●需要从旧域名跳转到新域名,且保持其参数不变
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 引入nginx官方源到yum仓库,相当于下载
[root@localhost ~]# yum install nginx -y
[root@localhost ~]# yum install bind -y
[root@localhost ~]# cd /etc/nginx/conf.d
[root@localhost conf.d]# vim default.conf 修改配置文件
把host修改为域名:
把host修改为域名,并把access_log前的#号去掉
server_name www.accp.com; 修改域名
access_log /var/log/nginx/www.accp.com.access.log main; 日志修改
在“location / { ” 后添加:
if ($host = 'www.accp.com'){
rewrite ^/(.*)$ http://www.kgc.com/$1 permanent;
}
[root@localhost conf.d]# vim /etc/named.conf
#/127 查找,并更改:
#/localhost 查找,并更改:
listen-on port 53 { any; };
allow-query { any; };
[root@localhost conf.d]# vim /etc/named.rfc1912.zones
更改区域数据配置文件
zone "accp.com" IN {
type master ;
file "accp.com.zone";
allow-update { none; };
};
zone "kgc.com" IN {
type master ;
file "kgc.com.zone";
allow-update { none; };
};
[root@localhost conf.d]# cd /var/named
[root@localhost named]# cp -p named.localhost accp.com.zone
[root@localhost named]# vim accp.com.zone
把末行4个A一行删掉,末行添加一行,域名对应本机IP地址
www IN A 192.168.17.129
[root@localhost named]# cp -p accp.com.zone kgc.com.zone
[root@localhost named]# systemctl start nginx
[root@localhost named]# systemctl start named
[root@localhost named]# netstat -ntap | grep named
[root@localhost named]# echo "nameserver 192.168.17.129" > /etc/resolv.conf
[root@localhost named]# nslookup www.accp.com
[root@localhost named]# nslookup www.kgc.com
在本机浏览器 输入旧域名 :www.accp.com
二:基于IP访问跳转
公司业务版本上线,所有IP访问任何内容都显示一个固定维护页面,只有在公司允许的IP电脑上才能正常访问。
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]# yum install nginx -y
[root@localhost ~]# yum install bind -y
[root@localhost ~]# cd /etc/nginx/conf.d
[root@localhost conf.d]# vim default.conf
在 server {
listen 80;
server_name www.accp.com;
#charset koi8-r;
access_log /var/log/nginx/www.accp.com.access.log main;
后更改添加跳转设置:
set $rewrite true;
if ($remote_addr = "192.168.17.131"){ 只允许192.168.17.131这台主机可以正常访问
set $rewrite false;
}
if ($rewrite = true){
rewrite (.+) /abc.html;
}
location = /abc.html {
root /usr/share/nginx/html;
}
[root@localhost ~]# cd /usr/share/nginx/html
[root@localhost html]# ls
50x.html index.html
[root@localhost html]# vim abc.html
<h1>正在维护,敬请期待</h1>
[root@localhost html]# systemctl restart nginx
[root@localhost html]# systemctl restart named
浏览器访问 只有公司允许的IP: 192. 168.195. 131可以正常访问,其他IP地址访问本公司域名只显示一个固定维护页面
三,基于旧域名跳转到新域名后面加目录
例如现在访问的是http://bbs.accp.com,现在需要将这个域名下面的发帖都跳转到http://www.accp.com/bbs,注意保持域名跳转后的参数不变。
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]# yum install nginx -y
[root@localhost ~]# yum install bind -y
[root@localhost ~]# cd /etc/nginx/conf.d
[root@localhost conf.d]# vim default.conf
server {
listen 80;
server_name bbs.accp.com;
# charset utf-8;
access_log /var/log/nginx/www.accp.com.access.log main;
跳转设置:
location /post {
rewrite (.+) http://www.accp.com/bbs$1 permanent;
}
[root@localhost conf.d]# vim /etc/named.conf
#/127 查找,并更改:
#/localhost 查找,并更改:
listen-on port 53 { any; };
allow-query { any; };
[root@localhost conf.d]# vim /etc/named.rfc1912.zones
更改区域数据配置文件
zone "accp.com" IN {
type master ;
file "accp.com.zone";
allow-update { none; };
};
[root@localhost conf.d]# cd /var/named
[root@localhost named]# cp -p named.localhost accp.com.zone
[root@localhost named]# vim accp.com.zone
#把末行4个A一行删掉
#末行添加一行,域名对应本机IP地址
bbs IN A 192.168.17.129
[root@localhost html]# systemctl restart nginx
[root@localhost html]# systemctl restart named
浏览器输入 http://bbs.accp.com/post/a.html 自动跳转到 www.accp.com/bbs/post/a.html
四,基于参数匹配的跳转
例如现在访问 http://www.accp.com/100-(100|200)-100.html 跳转到 http://www.accp.com 页面。
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 引入nginx官方源到yum仓库,相当于下载
[root@localhost ~]# yum install nginx -y
[root@localhost ~]# yum install bind -y
[root@localhost ~]# cd /etc/nginx/conf.d
[root@localhost conf.d]# vim default.conf 修改配置文件:
把host修改为域名:
把host修改为域名,并把access_log前的#号去掉
server_name www.accp.com; 修改域名
access_log /var/log/nginx/www.accp.com.access.log main;
跳转设置:
if ($request_uri ~ ^/100-(100|200)-(\d+).html$){
rewrite (.*) http://www.accp.com permanent;
}
[root@localhost conf.d]# vim /etc/named.conf
#/127 查找,并更改:
#/localhost 查找,并更改:
listen-on port 53 { any; };
allow-query { any; };
[root@localhost conf.d]# vim /etc/named.rfc1912.zones 更改区域数据配置文件
zone "accp.com" IN {
type master ;
file "accp.com.zone";
allow-update { none; };
};
[root@localhost conf.d]# cd /var/named
[root@localhost named]# cp -p named.localhost accp.com.zone
[root@localhost named]# vim accp.com.zone
#把末行4个A一行删掉
#末行添加一行,域名对应本机IP地址
www IN A 192.168.17.129
[root@localhost named]# systemctl start nginx
[root@localhost named]# systemctl start named
浏览器输入http://www.accp.com/100-100-1000.html 跳转到www.accp.com首页
五,基于目录下所有 php结尾的文件跳转
访问http://www.accp.com/upload/a.php 跳转到www.accp.com首页。
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 引入nginx官方源到yum仓库,相当于下载
[root@localhost ~]# yum install nginx -y
[root@localhost ~]# yum install bind -y
[root@localhost ~]# cd /etc/nginx/conf.d
[root@localhost conf.d]# vim default.conf
server {
listen 80;
server_name www.accp.com;
# charset utf-8;
access_log /var/log/nginx/www.accp.com.access.log main;
跳转设置:
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.accp.com permanent;
}
[root@localhost conf.d]# vim /etc/named.conf
#/127 查找,并更改:
#/localhost 查找,并更改:
listen-on port 53 { any; };
allow-query { any; };
[root@localhost conf.d]# vim /etc/named.rfc1912.zones 更改区域数据配置文件
zone "accp.com" IN {
type master ;
file "accp.com.zone";
allow-update { none; };
};
[root@localhost conf.d]# cd /var/named
[root@localhost named]# cp -p named.localhost accp.com.zone
[root@localhost named]# vim accp.com.zone
#把末行4个A一行删掉
#末行添加一行,域名对应本机IP地址
www IN A 192.168.17.129
[root@localhost named]# systemctl start nginx
[root@localhost named]# systemctl start named
浏览器输入http://www.accp.com/upload/a.php 会自动跳转到www.accp.com首页
六,基于最普通一条url请求的跳转,访问一个具体的页面跳转到首页。
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 引入nginx官方源到yum仓库,相当于下载
[root@localhost ~]# yum install nginx -y
[root@localhost ~]# yum install bind -y
[root@localhost ~]# cd /etc/nginx/conf.d
[root@localhost conf.d]# vim default.conf
server {
listen 80;
server.name www.accp.com;
#charset koi8-r;
access_1og /var/1og/nginx/www.accp.com.access.1og main;
跳转设置:
location ~* ^/abc/123.html {
rewrite (.+) http://www.accp.com permanent;
}
[root@localhost conf.d]# vim /etc/named.conf
#/127 查找,并更改:
#/localhost 查找,并更改:
listen-on port 53 { any; };
allow-query { any; };
[root@localhost conf.d]# vim /etc/named.rfc1912.zones
更改区域数据配置文件
zone "accp.com" IN {
type master ;
file "accp.com.zone";
allow-update { none; };
};
[root@localhost conf.d]# cd /var/named
[root@localhost named]# cp -p named.localhost accp.com.zone
[root@localhost named]# vim accp.com.zone
#把末行4个A一行删掉
#末行添加一行,域名对应本机IP地址
www IN A 192.168.17.129
[root@localhost named]# systemctl start nginx
[root@localhost named]# systemctl start named
浏览器访问http://www.accp.com/abc/123.html 自动跳转www.accp.com首页
来源:CSDN
作者:LXF@??
链接:https://blog.csdn.net/weixin_45691464/article/details/103843431