您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Nginx中如何進行rewrite模塊應用,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
Linux CentOS7虛擬機(IP:192.168.52.132)
win10虛擬機(作為測試機)
[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 獲取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 警告:/var/tmp/rpm-tmp.PtKluI: 頭V4 RSA/SHA1 Signature, 密鑰 ID 7bd9bf62: NOKEY 準備中... ################################# [100%] 正在升級/安裝... 1:nginx-release-centos-7-0.el7.ngx ################################# [100%] [root@localhost ~]# yum install nginx bind -y ..........//省略安裝過程
[root@localhost sbin]# vim /etc/named.conf options { listen-on port 53 { any; }; //127.0.0.1改為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; }; //localhost改為any [root@localhost sbin]#
[root@localhost sbin]# vim /etc/named.rfc1912.zones zone "old.com" IN { //添加一個區域信息 type master; file "old.com.zone"; allow-update { none; }; }; [root@localhost sbin]#
[root@localhost sbin]# cd /var/named/ [root@localhost named]# ls data dynamic named.ca named.empty named.localhost named.loopback slaves [root@localhost named]# cp -p named.localhost old.com.zone //復制模板并命名 [root@localhost named]# vim old.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 192.168.52.132 //添加解析地址 [root@localhost named]#
[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 80 //查看端口 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 51197/nginx: master [root@localhost named]#
現在公司舊域名 www.old.com 有業務需求有變更;
需要使用新 域名 www.new.com 代替,但是舊域名不能廢除;
需要跳轉到新域名上,而且后面的參 數保持不變。
[root@localhost named]# cd /etc/nginx/ [root@localhost nginx]# vim conf.d/default.conf #修改配置文件 server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; location / { if ($host = "www.old.com") { #匹配如果域名是老域名 rewrite ^/(.*)$ http://www.new.com/$1 permanent; #則永久設置跳轉新域名 } root /usr/share/nginx/html; index index.html index.htm; }
[root@localhost nginx]# vim /etc/named.rfc1912.zones zone "new.com" IN { type master; file "new.com.zone"; #new區域數據配置文件 allow-update { none; }; }; [root@localhost nginx]# cd /var/named/ [root@localhost named]# ls data dynamic named.ca named.empty named.localhost named.loopback old.com.zone slaves [root@localhost named]# cp -p old.com.zone new.com.zone #復制區域數據配置文件為kgc的數據配置文件 [root@localhost named]# systemctl restart named #重啟解析服務 [root@localhost named]# systemctl restart nginx #重啟nginx服務 [root@localhost named]#
所有IP 訪問任何內容都顯示一個固定維護頁面,只有公司 IP :192.168.52.129訪問正常。
[root@localhost named]# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; #設置是否合法的IP標志 set $rewrite true; #判斷是否為合法IP if ($remote_addr = "192.168.52.128"){ #win10主機ip為192.168.52.129,此時129為不合法ip set $rewrite false; } #非法IP進行判斷打上標記 if ($rewrite = true){ rewrite (.+) /main.html; } #匹配標記進行跳轉站點 location = /main.html { root /usr/share/nginx/html; } location / { root /usr/share/nginx/html; index index.html index.htm; }
[root@localhost named]# cd /usr/share/nginx/html/ #切換到站點中 [root@localhost html]# ls 50x.html index.html [root@localhost html]# vim main.html #編輯非法IP訪問網頁內容 <h2>this is error page</h2> [root@localhost html]# systemctl restart nginx #重啟Nginx服務 [root@localhost html]#
[root@localhost html]# vim /etc/nginx/conf.d/default.conf #判斷是否為合法IP if ($remote_addr = "192.168.52.129"){ #將合法ip改為win10主機IP地址 set $rewrite false; } [root@localhost html]# systemctl restart nginx #重啟服務 [root@localhost html]#
例如現在訪問的是 http://bbs.accp.com,現在需要將這個域名下面的發帖都跳轉到 http://www.accp.com/bbs,注意保持域名跳轉后的參數不變。
[root@localhost html]# vim /etc/nginx/conf.d/default.conf #修改默認配置文件 server { listen 80; server_name bbs.old.com; #修改服務名稱 #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; location /post { #用location匹配post目錄 rewrite (.+) http://www.old.com/bbs$1 permanent; #永久重定向跳轉 } location / { root /usr/share/nginx/html; index index.html index.htm; }
[root@localhost html]# cd /var/named/ [root@localhost named]# ls data dynamic named.ca named.empty named.localhost named.loopback new.com.zone old.com.zone slaves [root@localhost named]# vim old.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.52.132 #www改為bbs [root@localhost named]# systemctl restart named #重啟解析服務 [root@localhost named]# systemctl restart nginx #重啟Nginx服務 [root@localhost named]#
http://www.accp.com/100-(100|200)-100.html 跳轉到 http://www.accp.com 頁面。
[root@localhost named]# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; if ($request_uri ~ ^/100-(100|200)-(\d+).html$) { #匹配正則開頭為100-(100|200)-一次多次的整數html為結尾的 rewrite (.*) http://www.old.com permanent; #永久重定向跳轉到主頁 }
[root@localhost named]# vim /var/named/old.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 192.168.52.132 [root@localhost named]# systemctl restart named #重啟解析服務 [root@localhost named]# systemctl restart nginx #重啟解析服務 [root@localhost named]#
[root@localhost named]# vim /etc/nginx/conf.d/default.conf #修改默認配置文件 server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; location ~* /upload/.*\.php$ { #匹配不分大小寫,匹配upload后零次或多次以.php為結尾的 rewrite (.+) http://www.old.com permanent; #跳轉到首頁 } [root@localhost named]# systemctl restart nginx #重啟Nginx服務 [root@localhost named]#
[root@localhost named]# vim /etc/nginx/conf.d/default.conf #修改Nginx默認配置文件 server { listen 80; server_name www.old.com; #charset koi8-r; access_log /var/log/nginx/www.old.com-access.log main; location ~* ^/abc/123.html { #匹配某一個特定的網頁 rewrite (.+) http://www.old.com permanent; #跳轉到首頁 } location / { root /usr/share/nginx/html; index index.html index.htm; } [root@localhost named]# systemctl restart nginx #重啟Nginx服務 [root@localhost named]#
關于Nginx中如何進行rewrite模塊應用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。