Nginx rewrite (下) 示例

rewrite 示例

(一)基于域名的跳转

现在公司旧域名www.kgc.com有业务需求变更,需要使用新域名www.accp.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变。
例如:http://www.kgc.com/test/1.html ——> http://www.accp.com/test/1.html

vim /usr/local/nginx/conf/nginx.conf
server {
 listen       80;
 server_name  www.kgc.com;  //域名修改 
 charset utf-8;
 access_log  /var/log/nginx/kgc.com.access.log;  //日志修改
 location / {
 
  ##——添加域名重定向——##
        if ($host = 'www.kgc.com'){                  //$host为rewrite全局变量,代表请求主机头字段或主机名
   rewrite ^/(.*)$ http://www.accp.com/$1 permanent;  //$1为正则匹配的内容,即域名后边的字符串
        }
        root   html;
        index  index.html index.htm;
    }
}

(二)基于客户端 IP 访问跳转

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

vim /usr/local/nginx/conf/nginx.conf
server {
 listen       80;
 server_name  www.kgc.com;      //域名修改 
 charset utf-8;
 access_log  /log/host.access.log;

    //设置是否合法的IP标记
    set $gt true;              //设置变量$gt,变量值为boole值true
     #判断是否为合法IP
    if ($remote_addr = "192.168.176.100"){     //当客户端IP为192.168.71.12时,将变量值设为false,不进行重写
        set $gt false;
    }
    //除了合法IP,其它都是非法IP,进行重写跳转维护页面
    if ($gt = true){                //当变量值为true时,进行重写
        rewrite (.+) /weihu.html;    //重写在访问IP后边插入/weihu.html,例如        192.168.176.100/weihu.html  //注意不能添加 permenant,否则会一直重定向,不能访问页面
    }
    location = /weihu.html {
        root /var/www/html;      //网页返回/var/www/html/weihu.html的内容
    }

(三)基于旧域名跳转到新域名后面加目录

现在访问的是 http://gt.kgc.com,现在需要将这个域名下面的访问都跳转到http://www.kgc.com/gt
http://gt.kgc.com/post/1.html ——> http://www.kgc.com/gt/post/1.html

vim /usr/local/nginx/conf/nginx.conf
server {
 listen       80;
 server_name  gt.kgc.com;     //域名修改 
 charset utf-8;
 access_log  /log/host.access.log;
 location /post {
        rewrite (.+) http://www.kgc.com/gt$1 permanent;    //这里的$1为位置变量,代表/post
    }

location / {
        root   html;
        index  index.html index.htm;
    }
}


mkdir -p /usr/local/nginx/html/gt/post
systemctl restart nginx.service   
使用浏览器访问 http://gt.kgc.com/post/1.html 跳转到 http://www.kgc.com/gt/post/1.html 
echo 'this is 1.html' > /usr/local/nginx/html/gt/post/1.html
systemctl restart nginx.service 

(四)基于参数匹配的跳转

现在访问http://www.gt.com/100-(100|200)-100.html 跳转到http://www.kgc.com页面。

vim /usr/local/nginx/conf/nginx.conf
server {
 listen       80;
 server_name  www.kgc.com;  #域名修改 
 charset utf-8;
 access_log  /log/host.access.log;

if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
        rewrite (.+) http://www.kgc.com permanent;
    }

location / {
        root   html;
        index  index.html index.htm;
    }
}

使用浏览器访问http://www.kgc.com/100-200-100.html 或者 http://www.kgc.com/100-100-100.html 跳转到http://www.kgc.com页面

(五)基于目录下所有 php 结尾的文件跳转

要求访问 http://www.gt.com/upload/123.php 跳转到首页。

vim /usr/local/nginx/conf/nginx.conf
server {
 listen       80;
 server_name  www.kgc.com;  #域名修改 
 charset utf-8;
  access_log  /log/host.access.log;
 location ~* /upload/.*\.php$ {
    rewrite (.+) http://www.kgc.com permanent;
}

 location / {
    root   html;
    index  index.html index.htm;
}
}

(六)基于最普通一条 url 请求的跳转

要求访问一个具体的页面如 http://www.kgc.com/abc/123.html 跳转到首页
vim /usr/local/nginx/conf/nginx.conf

server {
 listen       80;
 server_name  www.kgc.com;  #域名修改 
 charset utf-8;
 access_log  /log/host.access.log;
 location ~* ^/abc/123.html {             //也可以用if ($request_uri ~* ^/abc/123.html//
        rewrite (.+) http://www.kgc.com permanent;
    }
location / {
        root   html;
        index  index.html index.htm;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值