ngx_http_rewrite_module
模块用于使用PCRE正则表达式更改请求URI,返回重定向并有条件地选择配置URL看起来更规范、合理
企业会将动态URL地址伪装成静态地址提供服务
网址换新域名后,让旧的访问跳转到新的域名上
服务端某些业务调整
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 ]
}
环境准备
手工编译安装nginx(实验中使用的是此nginx)
或者yum安装nginx
##yum安装nginx
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm '//安装nginx源'
yum install nginx -y
VMware软件
一台centos7服务器(IP地址为192.168.197.141)
一台Windows主机(IP地址为192.168.197.128)
配置Windows主机
配置centos服务器
####配置DNS
[root@localhost local]# yum install bind -y
[root@localhost local]# vim /etc/named.conf
修改监听地址:127.0.0.1修改为any,localhost修改为any
[root@localhost local]# vim /etc/named.rfc1912.zones
zone "old.com" IN { '//添加此段'
type master;
file "old.com.zone";
allow-update { none; };
zone "new.com" IN { '//添加此段'
type master;
file "new.com.zone";
allow-update { none; };
};
[root@localhost local]# cd /var/named
[root@localhost named]# cp -p named.localhost old.com.zone
[root@localhost named]# vim old.com.zone
www IN A 192.168.197.141 '//末行添加'
[root@localhost named]# cp -p old.com.zone new.com.zone
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.old.com; '//修改域名'
....
location / { '//添加此段域名跳转'
if ($host = "www.old.com"){
rewrite ^/(.*)$ http://www.new.com/$1 permanent;
}
}
....
[root@localhost named]# vim /usr/local/nginx/html/index.html
this is old web
'//修改主页内容'
[root@localhost named]# systemctl start nginx.service
[root@localhost named]# systemctl start named
[root@localhost named]# systemctl stop firewalld.service
[root@localhost named]# setenforce 0
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
...
server {
listen 80;
server_name www.old.com;
#charset koi8-r;
#access_log logs/host.access.log main;
#设置是否合法的IP标志
set $rewrite false;
#判断是否为合法IP
if ($remote_addr = "192.168.197.128"){
set $rewrite true;
}
#对非法IP进行判断打上标记
if ($rewrite = false){
rewrite (.+) /main.html;
}
#匹配标记进行跳转站点
location = /main.html {
root /usr/local/nginx/html;
}
....
[root@localhost named]# vim /usr/local/nginx/html/main.html
this is false web
重启nginx服务
#判断是否为合法IP
if ($remote_addr = "192.168.197.129"){ '//变更掉合法ip'
set $rewrite true;
}
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
'//删除掉刚刚设置的基于IP地址访问的跳转配置'
server {
listen 80;
server_name bbs.old.com; '//修改域名'
#charset koi8-r;
#access_log /var/log/nginx/www.old.com.access.log false;
localtion /post { '//添加跳转的网页'
rewrite (.+) http://www.new.com.bbs$1 permanent;
}
[root@localhost named]# vim /var/named/old.com.zone
bbs IN A 192.168.197.141 '//原本的www修改为bbs'
重启两个服务
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
'//删除掉刚刚设置的基于域名添加目录访问的跳转配置'
listen 80;
server_name www.old.com;
#charset koi8-r;
#access_log /var/log/nginx/www.old.com.access.log false;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$){
rewrite (.*) http://www.new.com permanent;
}
...
[root@localhost named]# vim /var/named/old.com.zone
www IN A 192.168.197.141 '//刚刚设置的bbs修改为www'
重启服务
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
'//删除掉刚刚设置的基于参数访问的跳转配置'
location ~* ^/abc/123.html {
rewrite (.+) http://www.old.com permanent;
}
重启服务
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
location ~* ^/abc/.*\.php$ {
rewrite (.+) http://www.old.com permanent;
}
重启服务