时间:2020-11-09来源:www.pcxitongcheng.com作者:电脑系统城
一. 新旧域名跳转
作用场景:基于域名的跳转,现在公司旧域名:www.peihua.com
有业务需求要变更,需要使用新域名www.zhenguo.com代替,但是旧域名不能废除。需要跳转到新域名上,而且后面的参数保持不变
配置dns,分别配置www.peihua.com(old)和www.zhenguo.com(new)解析
?| 1 | rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm |
(必须要有官方源才能yum安装nginx)
?| 1 2 |
yum install nginx -yrpm -qc nginx //查找配置文件 |
修改nginx的配置文件
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[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 |
配置域名解析
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
yum -y install bind#修改主配置文件[root@localhost conf.d]# vim /etc/named.confoptions { 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配置文件
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[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添加自定义页面内容
?| 1 2 |
[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配置文件
?| 1 2 3 4 5 6 7 8 9 10 |
[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
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[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.1bbs 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配置文件
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
[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
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[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.1www 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配置文件
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[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配置文件
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[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 |
浏览器访问
2024-07-07
myeclipse怎么导入tomcat教程2024-07-07
myeclipse如何启动tomcat2024-07-07
myeclipse如何绑定tomcat上线了一个小的预约程序,配置通过Nginx进行访问入口,默认的日志是没有请求时间的,因此需要配置一下,将每一次的请求的访问响应时间记录出来,备查与优化使用....
2023-03-17