Nginx Rewrite跳转

1

一. 新旧域名跳转

作用场景:基于域名的跳转,现在公司旧域名:www.peihua.com
有业务需求要变更,需要使用新域名www.zhenguo.com代替,但是旧域名不能废除。需要跳转到新域名上,而且后面的参数保持不变

配置dns,分别配置www.peihua.com(old)和www.zhenguo.com(new)解析

rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
(必须要有官方源才能yum安装nginx)

yum install nginx -y

rpm -qc nginx //查找配置文件

修改nginx的配置文件

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf 

server {
listen 80;
server_name www.peihua.com; #域名修改

#charset koi8-r;
access_log /var/log/nginx/peihua.com-access.log main; #日志修改

location / {
#域名重定向
if (KaTeX parse error: Expected '}', got 'EOF' at end of input: …ation">)</span> http://www.zhenguo.com/$1 permanent;
}
root /usr/share/nginx/html;
index index.html index.htm;
}

[root@localhost named]# systemctl restart nginx

配置域名解析

yum -y install bind

#修改主配置文件
[root@localhost conf.d]# vim /etc/named.conf
options {
listen-on port 53 { any; }; //修改成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; }; //修改成any

#修改区域配置文件
[root@localhost conf.d]# vim /etc/named.rfc1912.zones
zone “peihua.com” IN {
type master;
file “peihua.com.zone”;
allow-update { none; };
};

zone “zhenguo.com” IN {
type master;
file “zhenguo.com.zone”;
allow-update { none; };
};

#修改区域数据文件
[root@localhost conf.d]# cd /var/named/
[root@localhost named]# cp -p named.localhost peihua.com.zone
[root@localhost named]# cp -p peihua.com.zone zhenguo.com.zone

[root@localhost named]# systemctl start named

浏览器测试

浏览器输入模拟访问 http://www.peihua.com/test/1/index.php(虽然这个请求内容) 是不存在的)跳转到
http://www.zhneguo.com/test/1/index.php,从headers 里面 可以看到301,实现了永久跳转,而且域名后的参数也正常跳转。

二. 基于IP访问跳转

作用场景:基于客户端IP访问跳转,例如今天公司业务版本上线,所有IP 访问任何内容都显示一个固定维护页面,只有公司IP:12.0.0.100访问正常

修改nginx配置文件

[root@localhost html]# vim /etc/nginx/conf.d/default.conf 

server {
listen 80;
server_name www.peihua.com;

#charset koi8-r;
charset ‘utf-8’; //识别中文字符
access_log /var/log/nginx/peihua.com-access.log main;
#设置是否合法的IP标志
set KaTeX parse error: Expected 'EOF', got '#' at position 57: …n">;</span> #̲判断是否为合法<span cl…remote_addr = “12.0.0.100”) {
set KaTeX parse error: Expected 'EOF', got '}' at position 124: …n punctuation">}̲</span> #非法<s…rewrite = ture) {
rewrite (.+) /maintenance.html;
}
#匹配标记进行跳转站点
location = /maintenance.html {
root /usr/share/nginx/html;
}

给maintenance.html添加自定义页面内容

[root@localhost html]# cat /usr/share/nginx/html/maintenance.html 
<h1>网站正在维护</h1>

浏览器测试

使用ip地址为12.0.0.100 进行访问http://www.peihua.com,可以正常访问
使用IP地址为12.0.0.99进行访问http://www.peihua.com,显示维护页面

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

作用场景:基于旧域名跳转到新域名后面加目录,例如现在访问的是http://bbs.peihua.com.
现在需要将这个域名下面的发帖都跳转到http://www.peihua.com/bbs,注意保持域名跳转后 的参数不变

修改nginx配置文件

[root@localhost named]# vim /etc/nginx/conf.d/default.conf 
listen       <span class="token number">80</span><span class="token punctuation">;</span>
server_name  bbs<span class="token punctuation">.</span>peihua<span class="token punctuation">.</span>com<span class="token punctuation">;</span>
#charset koi8<span class="token operator">-</span>r<span class="token punctuation">;</span>
charset <span class="token string">'utf-8'</span><span class="token punctuation">;</span>
access_log  <span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>log<span class="token operator">/</span>nginx<span class="token operator">/</span>peihua<span class="token punctuation">.</span>com<span class="token operator">-</span>access<span class="token punctuation">.</span>log  main<span class="token punctuation">;</span>
location <span class="token operator">/</span>post <span class="token punctuation">{<!-- --></span>
          <span class="token function">rewrite</span> <span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">+</span><span class="token punctuation">)</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>peihua<span class="token punctuation">.</span>com<span class="token operator">/</span>bbs$<span class="token number">1</span> permanent<span class="token punctuation">;</span>

}

注意:accp.com.zone 需要更改主机域名解析,把www改成 bbs

[root@localhost named]#  cd /var/named
[root@localhost named]# vim peihua.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       12.0.0.25

[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl restart nginx

浏览器访问

浏览器访问 http://bbs.peihua.com/post/a.html,被跳转称为www.peihua.com/bbs/post/a.txt

四. 基于参数匹配的跳转

作用场景:基于参数匹配的跳转,例如现在访问 http://www.peihua.com/100-(100 | 200)-100.html
跳转到http://www.peihua.com页面

修改nginx配置文件

[root@localhost named]# vim /etc/nginx/conf.d/default.conf 

server {
listen 80;
server_name www.peihua.com;

#charset koi8<span class="token operator">-</span>r<span class="token punctuation">;</span>
charset <span class="token string">'utf-8'</span><span class="token punctuation">;</span>
access_log  <span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>log<span class="token operator">/</span>nginx<span class="token operator">/</span>peihua<span class="token punctuation">.</span>com<span class="token operator">-</span>access<span class="token punctuation">.</span>log  main<span class="token punctuation">;</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span>$request_uri <span class="token operator">~</span> <span class="token operator">^</span><span class="token operator">/</span><span class="token number">100</span><span class="token operator">-</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token operator">|</span><span class="token number">200</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token punctuation">(</span>\d<span class="token operator">+</span><span class="token punctuation">)</span><span class="token punctuation">.</span>html$<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
      <span class="token function">rewrite</span> <span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">)</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>peihua<span class="token punctuation">.</span>com permanent<span class="token punctuation">;</span>
<span class="token punctuation">}</span> 

注意:\d匹配一个数字,0~9之间

修改会dns,把bbs改成www

[root@localhost named]# vim peihua.com.zone 
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       12.0.0.25

[root@localhost named]# systemctl restart named
[root@localhost named]# systemctl restart nginx

浏览器测试

浏览器访问 http://www.peihua.com/100-200-26.html,被跳转称为 www.peihua.com首页

五. 基于所有以php结尾及基于某一个页面的跳转

修改nginx配置文件

[root@localhost named]# vim /etc/nginx/conf.d/default.conf 

server {
listen 80;

server_name  www<span class="token punctuation">.</span>peihua<span class="token punctuation">.</span>com<span class="token punctuation">;</span>
#charset koi8<span class="token operator">-</span>r<span class="token punctuation">;</span>
charset <span class="token string">'utf-8'</span><span class="token punctuation">;</span>
access_log  <span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>log<span class="token operator">/</span>nginx<span class="token operator">/</span>peihua<span class="token punctuation">.</span>com<span class="token operator">-</span>access<span class="token punctuation">.</span>log  main<span class="token punctuation">;</span>
location <span class="token operator">~</span><span class="token operator">*</span> <span class="token regex">/upload/</span><span class="token punctuation">.</span><span class="token operator">*</span>\<span class="token punctuation">.</span>php$ <span class="token punctuation">{<!-- --></span>
    <span class="token function">rewrite</span> <span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">+</span><span class="token punctuation">)</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>peihua<span class="token punctuation">.</span>com permanent<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

[root@localhost named]# systemctl restart nginx

浏览器访问

浏览器访问 http://www.peihua.com/upload/a.php,被跳转称为 www.peihua.com

六. 基于具体的某一个页面进行跳转/abc/123.html

修改nginx配置文件

[root@localhost named]# vim /etc/nginx/conf.d/default.conf 

server {
listen 80;
server_name www.peihua.com;

#charset koi8<span class="token operator">-</span>r<span class="token punctuation">;</span>
charset <span class="token string">'utf-8'</span><span class="token punctuation">;</span>
access_log  <span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>log<span class="token operator">/</span>nginx<span class="token operator">/</span>peihua<span class="token punctuation">.</span>com<span class="token operator">-</span>access<span class="token punctuation">.</span>log  main<span class="token punctuation">;</span>

location <span class="token operator">~</span><span class="token operator">*</span> <span class="token operator">^</span><span class="token operator">/</span>abc<span class="token operator">/</span><span class="token number">123.</span>html <span class="token punctuation">{<!-- --></span>
    <span class="token function">rewrite</span> <span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">+</span><span class="token punctuation">)</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>www<span class="token punctuation">.</span>peihua<span class="token punctuation">.</span>com permanent<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

[root@localhost named]# systemctl restart nginx

浏览器访问

浏览器访问 http://www.peihua.com/abc/123.html,被跳转称为 www.peihua.com

上一篇:charles抓包乱码


下一篇:nginx location Rewrite 乱七八糟 农贷