公司旧域名www.accp.com ,因业务需求有变更,需要使用新域名www.kgc.com 代替
1.不能废除旧域名
2.从旧域名跳转到新域名,且保持其参数不变
一台Linux服务器(192.168.142.130)
一台测试主机windows 7
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
#安装nginx官方源
[root@localhost ~]# yum install nginx -y
#yum安装nginx
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf ##修改默认配置文件
server {
listen 80;
server_name www.accp.com; ##修改主机名
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main; ##开启日志服务
[root@localhost ~]# yum install bind -y
[root@localhost ~]# vim /etc/named.conf
options {
listen-on port 53 { any; }; ##监听所有
...
allow-query { any; }; ##允许所有
[root@localhost ~]# vim /etc/named.rfc1912.zones ##配置区域配置文件
zone "accp.com" IN {
type master;
file "accp.com.zone"; ##accp区域数据配置文件
allow-update { none; };
};
[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost accp.com.zone ##复制模板
[root@localhost named]# vim accp.com.zone ##修改区域配置文件
$TTL 1D
@ IN SOA @ rname.invalid. (
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.142.130 ##本机地址
[root@localhost named]# systemctl start named ##开启dns服务
[root@localhost named]# systemctl stop firewalld.service ##关闭防火墙
[root@localhost named]# setenforce 0
[root@localhost named]# systemctl start nginx ##开启nginx服务
[root@localhost named]# vim /etc/nginx/conf.d/default.conf ##修改配置文件
server {
listen 80;
server_name www.accp.com;
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
location / {
if ($host = "www.accp.com"){ ##匹配如果域名是老域名
rewrite ^/(.*)$ http://www.kgc.com/$1 permanent; ##则永久设置跳转新域名
}
root /usr/share/nginx/html;
index index.html index.htm;
}
[root@localhost named]# vim /etc/named.rfc1912.zones
zone "kgc.com" IN {
type master;
file "kgc.com.zone"; ##accp区域数据配置文件
allow-update { none; };
};
[root@localhost named]# cp -p /var/named/accp.com.zone /var/named/kgc.com.zone
##复制区域数据配置文件为kgc的数据配置文件
[root@localhost named]# systemctl restart named ##重启解析服务
[root@localhost named]# systemctl restart nginx ##重启nginx服务
公司业务版本上线,所有IP访问任何内容都显示一个固定维护页面,只有公司IP访问正常
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf
server {
listen 80;
server_name www.accp.com;
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
#设置是否合法的IP标志
set $rewrite true; ##设置变量为真
#判断是否为合法的IP
if ($remote_addr = "192.168.142.120"){
set $rewrite false; ##匹配合法IP,将变量设置为假,正常跳转页面
}
#非法IP进行判断打上标记
if ($rewrite = true){ ##匹配非法IP,跳转到main的网页
rewrite (.+) /main.html;
}
#匹配标记进行跳转站点
location = /main.html { ##精确匹配
root /usr/share/nginx/html; ##站点路径
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
[root@localhost conf.d]# cd /usr/share/nginx/html/ ##切换到站点中
[root@localhost html]# vim main.html ##编辑非法IP访问网页内容
<h2>this is test web</h2>
[root@localhost html]# systemctl restart nginx ##重启Nginx服务
将域名http://bbs.accp.com 下面的发帖都跳转到http://www.accp.com/bbs 且域名跳转后保持参数不变
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf ##修改默认配置文件
server {
listen 80;
server_name bbs.accp.com; ##修改服务名称
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
location /post { ##用location匹配post目录
rewrite (.+) http://www.accp.com/bbs$1 permanent; ##永久重定向跳转
}
[root@localhost conf.d]# cd /var/named/
[root@localhost named]# vim accp.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 192.168.142.130
[root@localhost named]# systemctl restart named ##重启解析服务
[root@localhost named]# systemctl restart nginx ##重启Nginx服务
[root@localhost named]# echo "nameserver 192.168.142.130" > /etc/resolv.conf
##将解析服务器地址放到本地解析配置文件中
浏览器访问:http://www.accp.com/100-(100|200)-100.html 跳转到http://www.accp.com 页面
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf
server {
listen 80;
server_name www.accp.com;
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$){
##匹配正则开头为100-(100|200)-一次多次的整数html为结尾的
rewrite (.*) http://www.accp.com permanent; ##永久重定向跳转到主页
}
[root@localhost conf.d]# vim /var/named/accp.com.zone ##修改区域数据配置文件
www IN A 192.168.142.130
[root@localhost conf.d]# systemctl restart named ##重启解析服务
[root@localhost conf.d]# systemctl restart nginx ##重启Nginx服务
访问http://www.accp.com/upload/1.php 跳转到首页
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf ##修改默认配置文件
server {
listen 80;
server_name www.accp.com;
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
location ~* /upload/.*\.php$ { ##匹配不分大小写,匹配upload后零次或多次以.php为结尾的
rewrite (.+) http://www.accp.com permanent; ##跳转到首页
}
[root@localhost conf.d]# systemctl restart nginx ##重启Nginx服务
[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf ##修改Nginx默认配置文件
server {
listen 80;
server_name www.accp.com;
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
location ~* ^/abc/123.html { ##匹配某一个特定的网页
rewrite (.+) http://www.accp.com permanent; ##跳转到首页
}
[root@localhost conf.d]# systemctl restart nginx ##重启Nginx服务
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。