公司舊域名www.accp.com ,因業務需求有變動,須要使用新域名www.kgc.com 代替 不能廢除舊域名 從舊域名跳轉到新域名,且保持其參數不變
Linux服務器(192.168.13.144) 測試機win7
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm ##安裝nginx官方源 警告:/var/tmp/rpm-tmp.vS0k20: 頭V4 RSA/SHA1 Signature, 密鑰 ID 7bd9bf62: NOKEY 準備中... ################################# [100%] 正在升級/安裝... 1:nginx-release-centos-7-0.el7.ngx ################################# [100%] [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; }; ##監聽全部 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; }; ##容許全部
[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.13.144 ##本機地址 [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]# netstat -ntap | grep nginx ##查看端口 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4093/nginx: master
[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.13.140"){ 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訪問網頁內容 <h1>this is test web</h1> [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.13.144 [root@localhost named]# systemctl restart named ##重啓解析服務 [root@localhost named]# systemctl restart nginx ##重啓Nginx服務 [root@localhost named]# echo "nameserver 192.168.13.144" > /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.13.144 [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服務