0
点赞
收藏
分享

微信扫一扫

使用rewrite规则实现将所有到a域名的访问rewrite到b域名

小猪肥 2022-04-23 阅读 58

目录

rewrite flag 使用介绍

1、301永久重定向

2、302 临时重定向


rewrite flag 使用介绍


利用nginx的rewrite的指令,可以实现url的重新跳转,rewrite有四种不同的flag,分别是redirect(临时重定向302)、permanent(永久重定向301)、break和last。其中前两种是跳转型的flag,后两种是代理型的flag

  • 跳转型指由客户端浏览器重新对新地址进行请求
  • 代理型是在WEB服务器内部实现跳转

1、301永久重定向


域名永久型调整,即域名永远跳转至另外一个新的域名,之前的域名再也不使用,跳转记录可以缓存到客户端浏览器

永久重定向会缓存DNS解析记录,浏览器中有 from disk cache 信息,即使nginx服务器无法访问,浏览器也会利用缓存进行重定向

#实现单页301重定向
[root@centos7 conf.d]#pwd
/apps/nginx/conf/conf.d
[root@centos7 conf.d]#vim pc.conf
server{
    listen 80;
    server_name www.linux2022.com;
    location / {
        root /data/nginx/html/pc;
    }
    location /gz {
        rewrite ^/gz/(.*) /guangzhou/$1 permanent;
    }

}
[root@centos7 conf.d]#nginx -t
[root@centos7 conf.d]#nginx -s reload

[root@centos7 conf.d]#cd /data/nginx/html/pc/
[root@centos7 pc]#mkdir guangzhou/
[root@centos7 pc]#pwd
/data/nginx/html/pc
[root@centos7 pc]#pwd > guangzhou/index.html
[root@centos7 pc]#vim guangzhou/index.html
/data/nginx/html/pc/guangzhou/index.html

#测试验证
[root@ubuntu1804 ~]#curl www.linux2022.com/gz/
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@ubuntu1804 ~]#curl www.linux2022.com/gz/ -L
/data/nginx/html/pc/guangzhou/index.html
[root@ubuntu1804 ~]#curl www.linux2022.com/guangzhou/ -L
/data/nginx/html/pc/guangzhou/index.html

#实现域名301重定向
[root@centos7 conf.d]#pwd
/apps/nginx/conf/conf.d
[root@centos7 conf.d]#vim pc.conf
server{
    listen 80;
    server_name www.linux2022.com;
    location / {
        root /data/nginx/html/pc;
        rewrite / http://www.linux2022.cn permanent;
    }
    location /gz {
        rewrite ^/gz/(.*) /guangzhou/$1 permanent;
    }

}
server{
    listen 80;
    server_name www.linux2022.cn;
    location / {
        root /data/nginx/html/product/;
    }
}
[root@centos7 conf.d]#nginx -t
[root@centos7 conf.d]#nginx -s reload

[root@centos7 conf.d]#cd /data/nginx/html/pc/
[root@centos7 pc]#mkdir product
[root@centos7 pc]#vim product/index.html
www.linux2022.cn/product

#测试验证
[root@ubuntu1804 ~]#vim /etc/hosts
10.0.0.7 www.linux2022.com m.linux2022.com www.linux2022.cn
[root@ubuntu1804 ~]#curl www.linux2022.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 05 Apr 2022 04:28:45 GMT
Content-Type: text/html
Content-Length: 162
Connection: keep-alive
Location: http://www.linux2022.cn

2、302 临时重定向


域名临时重定向,告诉浏览器域名不是固定重定向到当前目标域名,后期可能随时会更改,因此浏览器不会缓存当前域名的解析记录,而浏览器会缓存永久重定向的DNS解析记录,这也是临时重定向与永久重定向最大的本质区别 。
 

[root@centos7 conf.d]#pwd
/apps/nginx/conf/conf.d
[root@centos7 conf.d]#vim pc.conf
server{
    listen 80;
    server_name www.linux2022.com;
    location / {
        root /data/nginx/html/pc;
        rewrite / http://www.linux2022.cn redirect;
    }
}

[root@centos7 conf.d]#nginx -t
[root@centos7 conf.d]#nginx -s reload

#测试验证
[root@ubuntu1804 ~]#curl www.linux2022.com -I
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Tue, 05 Apr 2022 05:00:12 GMT
Content-Type: text/html
Content-Length: 138
Connection: keep-alive
Location: http://www.linux2022.cn
举报

相关推荐

0 条评论