Jump rewrite scenarios

Jump scenarios based domain 1--

公司旧域名www.accp.com ,因业务需求有变更,需要使用新域名www.kgc.com 代替
    不能废除旧域名
    从旧域名跳转到新域名,且保持其参数不变

lab environment

Linux服务器(192.168.13.144)
测试机win7

1, install Nginx Service

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
##安装nginx官方源
警告:/var/tmp/rpm-tmp.vS0k20: 头V4 RSA/SHA1 Signature, 密钥 ID 7bd9bf62: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
     1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@localhost ~]# yum install nginx -y   ##yum安装nginx

2, modify the default configuration file nginx

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf  ##修改默认配置文件
server {
        listen       80;
        server_name  www.accp.com;   ##修改主机名

        #charset koi8-r;
        access_log  /var/log/nginx/www.accp.com-access.log  main;   ##开启日志服务

3, install bind Resolution Service

[root@localhost ~]# yum install bind -y

4, modify the main configuration file (the named.conf)

[root@localhost ~]# vim /etc/named.conf 
options {
                                listen-on port 53 { any; };          ##监听所有
                                listen-on-v6 port 53 { ::1; };
                                directory       "/var/named";
                                dump-file       "/var/named/data/cache_dump.db";
                                statistics-file "/var/named/data/named_stats.txt";
                                memstatistics-file "/var/named/data/named_mem_stats.txt";
                                recursing-file  "/var/named/data/named.recursing";
                                secroots-file   "/var/named/data/named.secroots";
                                allow-query     { any; };           ##允许所有

5, profile modification area (named.rfc1912.zones)

[root@localhost ~]# vim /etc/named.rfc1912.zones    ##配置区域配置文件

zone "accp.com" IN {
                                type master;
                                file "accp.com.zone";             ##accp区域数据配置文件
                                allow-update { none; };
};

6, modify the configuration file data region (accp.com.zone)

[root@localhost ~]# cd /var/named/  
[root@localhost named]# cp -p named.localhost accp.com.zone    ##复制模板
[root@localhost named]# vim accp.com.zone    ##修改区域配置文件

$TTL 1D
@       IN SOA  @ rname.invalid. (
                                                                        1D      ; refresh
                                                                        1H      ; retry
                                                                        1W      ; expire
                                                                        3H )    ; minimum
                                NS      @
                                A       127.0.0.1
www IN  A       192.168.13.144     ##本机地址
[root@localhost named]# systemctl start named      ##开启dns服务
[root@localhost named]# systemctl stop firewalld.service    ##关闭防火墙
[root@localhost named]# setenforce 0
[root@localhost named]# systemctl start nginx   ##开启nginx服务
[root@localhost named]# netstat -ntap | grep nginx  ##查看端口
tcp   0    0 0.0.0.0:80      0.0.0.0:*       LISTEN   4093/nginx: master 

7, with the test machine test page

Jump rewrite scenarios
Jump rewrite scenarios

8, modify the configuration file, set the domain name to jump

[root@localhost named]# vim /etc/nginx/conf.d/default.conf  ##修改配置文件
server {
        listen       80;
        server_name  www.accp.com;

        #charset koi8-r;
        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   /usr/share/nginx/html;
                index  index.html index.htm;
        }

9, add a new DNS

[root@localhost named]# vim /etc/named.rfc1912.zones 

zone "kgc.com" IN {
                                type master;
                                file "kgc.com.zone";             ##accp区域数据配置文件
                                allow-update { none; };
};

[root@localhost named]# cp -p /var/named/accp.com.zone /var/named/kgc.com.zone
##复制区域数据配置文件为kgc的数据配置文件
[root@localhost named]# systemctl restart named   ##重启解析服务
[root@localhost named]# systemctl restart nginx     ##重启nginx服务

10, with the old domain name to access, view Jump

Jump rewrite scenarios
Jump rewrite scenarios

Are there parameters 11, after the old domain name plus the parameters, see the jump a new domain name

Jump rewrite scenarios
Jump rewrite scenarios

2-- scenarios based on the client IP Access Jump

公司业务版本上线,所有IP访问任何内容都显示一个固定维护页面,只有公司IP访问正常

1, modify the default configuration file Nginx

[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;
        #设置是否合法的IP标志
        set $rewrite true;         ##设置变量为真
        #判断是否为合法的IP
        if ($remote_addr = "192.168.13.140"){
                set $rewrite false;    ##匹配合法IP,将变量设置为假,正常跳转页面
        }
        #非法IP进行判断打上标记
        if ($rewrite = true){                ##匹配非法IP,跳转到main的网页
                rewrite (.+) /main.html;
        }
        #匹配标记进行跳转站点
        location = /main.html {              ##精确匹配
                root /usr/share/nginx/html;   ##站点路径
        }

        location / {
                root   /usr/share/nginx/html;
                index  index.html index.htm;
        }  

2, create a web page and main site of illegal IP

[root@localhost conf.d]# cd /usr/share/nginx/html/  ##切换到站点中
[root@localhost html]# vim main.html    ##编辑非法IP访问网页内容
<h1>this is test web</h1>
[root@localhost html]# systemctl restart nginx   ##重启Nginx服务

3, visit the page

Jump rewrite scenarios
Jump rewrite scenarios

3-- scenarios based on the old, the new domain name and add a table of contents to jump

将域名http://bbs.accp.com 下面的发帖都跳转到http://www.accp.com/bbs 且域名跳转后保持参数不变

1, modify the default configuration file Nginx

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf   ##修改默认配置文件
server {
        listen       80;
        server_name  bbs.accp.com;   ##修改服务名称

        #charset koi8-r;
        access_log  /var/log/nginx/www.accp.com-access.log  main;
        location /post {          ##用location匹配post目录
                rewrite (.+) http://www.accp.com/bbs$1 permanent;   ##永久重定向跳转
        }

2, modify dns area data profile (accp.com.zone)

[root@localhost conf.d]# cd /var/named/
[root@localhost named]# vim accp.com.zone   ##修改区域数据配置文件
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                                    0       ; serial
                                                    1D      ; refresh
                                                    1H      ; retry
                                                    1W      ; expire
                                                    3H )    ; minimum
                NS      @
                A       127.0.0.1
bbs IN  A       192.168.13.144
[root@localhost named]# systemctl restart named   ##重启解析服务
[root@localhost named]# systemctl restart nginx     ##重启Nginx服务
[root@localhost named]# echo "nameserver 192.168.13.144" > /etc/resolv.conf 
##将解析服务器地址放到本地解析配置文件中

3, test page

Jump rewrite scenarios
Jump rewrite scenarios

4-- application scenarios based on parameters match Jump

浏览器访问
http://www.accp.com/100-(100|200)-100.html 跳转到http://www.accp.com 页面

1, modify the default configuration file Nginx

[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;
        if ($request_uri ~ ^/100-(100|200)-(\d+).html$){       
        ##匹配正则开头为100-(100|200)-一次多次的整数html为结尾的
                rewrite (.*) http://www.accp.com permanent;       ##永久重定向跳转到主页
        }

2, modify the configuration file data area dns

[root@localhost conf.d]# vim /var/named/accp.com.zone  ##修改区域数据配置文件
www IN  A       192.168.13.144   
[root@localhost conf.d]# systemctl restart named  ##重启解析服务 
[root@localhost conf.d]# systemctl restart nginx     ##重启Nginx服务

3, test page

Jump rewrite scenarios
Jump rewrite scenarios

PHP 5-- scenarios based on all the files in the directory jump

访问http://www.accp.com/upload/1.php 跳转到首页

1, modify the default configuration file Nginx

[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;
        location ~* /upload/.*\.php$ {         ##匹配不分大小写,匹配upload后零次或多次以.php为结尾的
                rewrite (.+) http://www.accp.com permanent;   ##跳转到首页
        }
[root@localhost conf.d]# systemctl restart nginx   ##重启Nginx服务

2, test page

Jump rewrite scenarios
Jump rewrite scenarios

6-- scenarios based on the most common request url Jump

访问一个具体的页面跳转到首页

1, modify the default configuration file Nginx

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf    ##修改Nginx默认配置文件
server {
        listen       80;
        server_name  www.accp.com;
        #charset koi8-r;
        access_log  /var/log/nginx/www.accp.com-access.log  main;
        location ~* ^/abc/123.html {       ##匹配某一个特定的网页
                rewrite (.+) http://www.accp.com permanent;  ##跳转到首页
        }
[root@localhost conf.d]# systemctl restart nginx   ##重启Nginx服务

2, test page

Jump rewrite scenarios
Jump rewrite scenarios

thanks for reading!

Guess you like

Origin blog.51cto.com/14469918/2451315