這篇文章將為大家詳細(xì)講解有關(guān)Nginx中如何進(jìn)行rewrite模塊應(yīng)用,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
平陰網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)于2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。
Linux CentOS7虛擬機(jī)(IP:192.168.52.132)
win10虛擬機(jī)(作為測試機(jī))
[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 準(zhǔn)備中... ################################# [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 { //添加一個(gè)區(qū)域信息 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 //復(fù)制模板并命名 [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服務(wù) [root@localhost named]# systemctl stop firewalld.service //關(guān)閉防火墻 [root@localhost named]# setenforce 0 //關(guān)閉增強(qiáng)性安全功能 [root@localhost named]# systemctl start nginx //開啟Nginx服務(wù) [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]#
現(xiàn)在公司舊域名 www.old.com 有業(yè)務(wù)需求有變更;
需要使用新 域名 www.new.com 代替,但是舊域名不能廢除;
需要跳轉(zhuǎn)到新域名上,而且后面的參 數(shù)保持不變。
[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; #則永久設(shè)置跳轉(zhuǎn)新域名 } 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區(qū)域數(shù)據(jù)配置文件 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 #復(fù)制區(qū)域數(shù)據(jù)配置文件為kgc的數(shù)據(jù)配置文件 [root@localhost named]# systemctl restart named #重啟解析服務(wù) [root@localhost named]# systemctl restart nginx #重啟nginx服務(wù) [root@localhost named]#
所有IP 訪問任何內(nèi)容都顯示一個(gè)固定維護(hù)頁面,只有公司 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; #設(shè)置是否合法的IP標(biāo)志 set $rewrite true; #判斷是否為合法IP if ($remote_addr = "192.168.52.128"){ #win10主機(jī)ip為192.168.52.129,此時(shí)129為不合法ip set $rewrite false; } #非法IP進(jìn)行判斷打上標(biāo)記 if ($rewrite = true){ rewrite (.+) /main.html; } #匹配標(biāo)記進(jìn)行跳轉(zhuǎn)站點(diǎn) 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/ #切換到站點(diǎn)中 [root@localhost html]# ls 50x.html index.html [root@localhost html]# vim main.html #編輯非法IP訪問網(wǎng)頁內(nèi)容 <h2>this is error page</h2> [root@localhost html]# systemctl restart nginx #重啟Nginx服務(wù) [root@localhost html]#
[root@localhost html]# vim /etc/nginx/conf.d/default.conf #判斷是否為合法IP if ($remote_addr = "192.168.52.129"){ #將合法ip改為win10主機(jī)IP地址 set $rewrite false; } [root@localhost html]# systemctl restart nginx #重啟服務(wù) [root@localhost html]#
例如現(xiàn)在訪問的是 http://bbs.accp.com,現(xiàn)在需要將這個(gè)域名下面的發(fā)帖都跳轉(zhuǎn)到 http://www.accp.com/bbs,注意保持域名跳轉(zhuǎn)后的參數(shù)不變。
[root@localhost html]# vim /etc/nginx/conf.d/default.conf #修改默認(rèn)配置文件 server { listen 80; server_name bbs.old.com; #修改服務(wù)名稱 #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; #永久重定向跳轉(zhuǎn) } 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 #修改區(qū)域數(shù)據(jù)配置文件 $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 #重啟解析服務(wù) [root@localhost named]# systemctl restart nginx #重啟Nginx服務(wù) [root@localhost named]#
http://www.accp.com/100-(100|200)-100.html 跳轉(zhuǎn)到 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)-一次多次的整數(shù)html為結(jié)尾的 rewrite (.*) http://www.old.com permanent; #永久重定向跳轉(zhuǎn)到主頁 }
[root@localhost named]# vim /var/named/old.com.zone #修改區(qū)域數(shù)據(jù)配置文件 $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 #重啟解析服務(wù) [root@localhost named]# systemctl restart nginx #重啟解析服務(wù) [root@localhost named]#
[root@localhost named]# vim /etc/nginx/conf.d/default.conf #修改默認(rèn)配置文件 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為結(jié)尾的 rewrite (.+) http://www.old.com permanent; #跳轉(zhuǎn)到首頁 } [root@localhost named]# systemctl restart nginx #重啟Nginx服務(wù) [root@localhost named]#
[root@localhost named]# vim /etc/nginx/conf.d/default.conf #修改Nginx默認(rèn)配置文件 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 { #匹配某一個(gè)特定的網(wǎng)頁 rewrite (.+) http://www.old.com permanent; #跳轉(zhuǎn)到首頁 } location / { root /usr/share/nginx/html; index index.html index.htm; } [root@localhost named]# systemctl restart nginx #重啟Nginx服務(wù) [root@localhost named]#
關(guān)于Nginx中如何進(jìn)行rewrite模塊應(yīng)用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁標(biāo)題:Nginx中如何進(jìn)行rewrite模塊應(yīng)用
本文網(wǎng)址:http://vcdvsql.cn/article30/peihso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、網(wǎng)站改版、用戶體驗(yàn)、外貿(mào)建站、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)