nginx里的rewrite跳转的实现
一. 新旧域名跳转
作用场景:基于域名的跳转,现在公司旧域名: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 ($host = 'www.peihua.com') { rewrite ^/(.*)$ 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 $rewrite ture; #判断是否为合法ip if ($remote_addr = "12.0.0.100") { set $rewrite false; } #非法ip进行判断打上标记 if ($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 80; server_name bbs.peihua.com; #charset koi8-r; charset 'utf-8'; access_log /var/log/nginx/peihua.com-access.log main; location /post { rewrite (.+) http://www.peihua.com/bbs$1 permanent; }
注意: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-r; charset 'utf-8'; access_log /var/log/nginx/peihua.com-access.log main; if ($request_uri ~ ^/100-(100|200)-(\d+).html$) { rewrite (.*) http://www.peihua.com permanent; }
注意:\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.peihua.com; #charset koi8-r; charset 'utf-8'; access_log /var/log/nginx/peihua.com-access.log main; location ~* /upload/.*\.php$ { rewrite (.+) http://www.peihua.com permanent; } [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-r; charset 'utf-8'; access_log /var/log/nginx/peihua.com-access.log main; location ~* ^/abc/123.html { rewrite (.+) http://www.peihua.com permanent; } [root@localhost named]# systemctl restart nginx
浏览器访问
浏览器访问 http://www.peihua.com/abc/123.html,被跳转称为 www.peihua.com
到此这篇关于nginx里的rewrite跳转的实现的文章就介绍到这了,更多相关nginx rewrite跳转内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!