nginx里的rewrite跳转的实现
一.新旧域名跳转
作用场景:基于域名的跳转,现在公司旧域名:www.peihua.com
有业务需求要变更,需要使用新域名www.zhenguo.com代替,但是旧域名不能废除。需要跳转到新域名上,而且后面的参数保持不变
配置dns,分别配置www.peihua.com(old)和www.zhenguo.com(new)解析
rpm-ivhhttp://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
(必须要有官方源才能yum安装nginx)
yuminstallnginx-y rpm-qcnginx//查找配置文件
修改nginx的配置文件
[root@localhost~]#vim/etc/nginx/conf.d/default.conf server{ listen80; server_namewww.peihua.com;#域名修改 #charsetkoi8-r; access_log/var/log/nginx/peihua.com-access.logmain;#日志修改 location/{ #域名重定向 if($host='www.peihua.com'){ rewrite^/(.*)$http://www.zhenguo.com/$1permanent; } root/usr/share/nginx/html; indexindex.htmlindex.htm; } [root@localhostnamed]#systemctlrestartnginx
配置域名解析
yum-yinstallbind #修改主配置文件 [root@localhostconf.d]#vim/etc/named.conf options{ listen-onport53{any;};//修改成any listen-on-v6port53{::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@localhostconf.d]#vim/etc/named.rfc1912.zones zone"peihua.com"IN{ typemaster; file"peihua.com.zone"; allow-update{none;}; }; zone"zhenguo.com"IN{ typemaster; file"zhenguo.com.zone"; allow-update{none;}; }; #修改区域数据文件 [root@localhostconf.d]#cd/var/named/ [root@localhostnamed]#cp-pnamed.localhostpeihua.com.zone [root@localhostnamed]#cp-ppeihua.com.zonezhenguo.com.zone [root@localhostnamed]#systemctlstartnamed
浏览器测试
浏览器输入模拟访问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@localhosthtml]#vim/etc/nginx/conf.d/default.conf server{ listen80; server_namewww.peihua.com; #charsetkoi8-r; charset'utf-8';//识别中文字符 access_log/var/log/nginx/peihua.com-access.logmain; #设置是否合法的IP标志 set$rewriteture; #判断是否为合法IP if($remote_addr="12.0.0.100"){ set$rewritefalse; } #非法IP进行判断打上标记 if($rewrite=ture){ rewrite(.+)/maintenance.html; } #匹配标记进行跳转站点 location=/maintenance.html{ root/usr/share/nginx/html; }
给maintenance.html添加自定义页面内容
[root@localhosthtml]#cat/usr/share/nginx/html/maintenance.html网站正在维护
浏览器测试
使用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@localhostnamed]#vim/etc/nginx/conf.d/default.conf listen80; server_namebbs.peihua.com; #charsetkoi8-r; charset'utf-8'; access_log/var/log/nginx/peihua.com-access.logmain; location/post{ rewrite(.+)http://www.peihua.com/bbs$1permanent; }
注意:accp.com.zone需要更改主机域名解析,把www改成bbs
[root@localhostnamed]#cd/var/named [root@localhostnamed]#vimpeihua.com.zone $TTL1D @INSOA@rname.invalid.( 0;serial 1D;refresh 1H;retry 1W;expire 3H);minimum NS@ A127.0.0.1 bbsINA12.0.0.25 [root@localhostnamed]#systemctlrestartnamed [root@localhostnamed]#systemctlrestartnginx
浏览器访问
浏览器访问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@localhostnamed]#vim/etc/nginx/conf.d/default.conf server{ listen80; server_namewww.peihua.com; #charsetkoi8-r; charset'utf-8'; access_log/var/log/nginx/peihua.com-access.logmain; if($request_uri~^/100-(100|200)-(\d+).html$){ rewrite(.*)http://www.peihua.compermanent; }
注意:\d匹配一个数字,0~9之间
修改会dns,把bbs改成www
[root@localhostnamed]#vimpeihua.com.zone $TTL1D @INSOA@rname.invalid.( 0;serial 1D;refresh 1H;retry 1W;expire 3H);minimum NS@ A127.0.0.1 wwwINA12.0.0.25 [root@localhostnamed]#systemctlrestartnamed [root@localhostnamed]#systemctlrestartnginx
浏览器测试
浏览器访问http://www.peihua.com/100-200-26.html,被跳转称为www.peihua.com首页
五.基于所有以php结尾及基于某一个页面的跳转
修改nginx配置文件
[root@localhostnamed]#vim/etc/nginx/conf.d/default.conf server{ listen80; server_namewww.peihua.com; #charsetkoi8-r; charset'utf-8'; access_log/var/log/nginx/peihua.com-access.logmain; location~*/upload/.*\.php${ rewrite(.+)http://www.peihua.compermanent; } [root@localhostnamed]#systemctlrestartnginx
浏览器访问
浏览器访问http://www.peihua.com/upload/a.php,被跳转称为www.peihua.com
六.基于具体的某一个页面进行跳转/abc/123.html
修改nginx配置文件
[root@localhostnamed]#vim/etc/nginx/conf.d/default.conf server{ listen80; server_namewww.peihua.com; #charsetkoi8-r; charset'utf-8'; access_log/var/log/nginx/peihua.com-access.logmain; location~*^/abc/123.html{ rewrite(.+)http://www.peihua.compermanent; } [root@localhostnamed]#systemctlrestartnginx
浏览器访问
浏览器访问http://www.peihua.com/abc/123.html,被跳转称为www.peihua.com
到此这篇关于nginx里的rewrite跳转的实现的文章就介绍到这了,更多相关nginxrewrite跳转内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!