怎么在Nginx中使用Rewrite模塊

本篇文章給大家分享的是有關(guān)怎么在Nginx中使用Rewrite模塊,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

為北關(guān)等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及北關(guān)網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站制作、成都網(wǎng)站制作、北關(guān)網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

部署環(huán)境

一臺Linux服務(wù)器(192.168.142.130)
一臺測試主機windows 7

1,安裝Nginx服務(wù)

[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

2,修改nginx默認(rèn)配置文件

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf ##修改默認(rèn)配置文件
server {
    listen    80;
    server_name www.accp.com;  ##修改主機名

    #charset koi8-r;
    access_log /var/log/nginx/www.accp.com-access.log main;  ##開啟日志服務(wù)

3,安裝bind解析服務(wù)

[root@localhost ~]# yum install bind -y

4,修改主配置文件(named.conf)

[root@localhost ~]# vim /etc/named.conf 
options {
                listen-on port 53 { any; };     ##監(jiān)聽所有
                ...
                allow-query   { any; };      ##允許所有

5,修改區(qū)域配置文件(named.rfc1912.zones)

[root@localhost ~]# vim /etc/named.rfc1912.zones  ##配置區(qū)域配置文件

zone "accp.com" IN {
                type master;
                file "accp.com.zone";       ##accp區(qū)域數(shù)據(jù)配置文件
                allow-update { none; };
};

6,修改區(qū)域數(shù)據(jù)配置文件(accp.com.zone)

[root@localhost ~]# cd /var/named/ 
[root@localhost named]# cp -p named.localhost accp.com.zone  ##復(fù)制模板
[root@localhost named]# vim accp.com.zone  ##修改區(qū)域配置文件

$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服務(wù)
[root@localhost named]# systemctl stop firewalld.service  ##關(guān)閉防火墻
[root@localhost named]# setenforce 0
[root@localhost named]# systemctl start nginx  ##開啟nginx服務(wù)

7,用測試機測試網(wǎng)頁

怎么在Nginx中使用Rewrite模塊

8,修改配置文件,設(shè)置域名跳轉(zhuǎn)

[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;  ##則永久設(shè)置跳轉(zhuǎn)新域名
          }
          root  /usr/share/nginx/html;
          index index.html index.htm;
      }

9,添加新域名解析

[root@localhost named]# vim /etc/named.rfc1912.zones 

zone "kgc.com" IN {
                type master;
                file "kgc.com.zone";       ##accp區(qū)域數(shù)據(jù)配置文件
                allow-update { none; };
};

[root@localhost named]# cp -p /var/named/accp.com.zone /var/named/kgc.com.zone
##復(fù)制區(qū)域數(shù)據(jù)配置文件為kgc的數(shù)據(jù)配置文件
[root@localhost named]# systemctl restart named  ##重啟解析服務(wù)
[root@localhost named]# systemctl restart nginx   ##重啟nginx服務(wù)

10,用舊域名訪問,查看網(wǎng)頁跳轉(zhuǎn)

怎么在Nginx中使用Rewrite模塊

怎么在Nginx中使用Rewrite模塊

11,舊域名后加上參數(shù),查看跳轉(zhuǎn)新域名時是否有參數(shù)

怎么在Nginx中使用Rewrite模塊

怎么在Nginx中使用Rewrite模塊

應(yīng)用場景2——基于客戶端IP訪問跳轉(zhuǎn)

公司業(yè)務(wù)版本上線,所有IP訪問任何內(nèi)容都顯示一個固定維護頁面,只有公司IP訪問正常

1,修改Nginx默認(rèn)配置文件

[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;
    #設(shè)置是否合法的IP標(biāo)志
    set $rewrite true;     ##設(shè)置變量為真
    #判斷是否為合法的IP
    if ($remote_addr = "192.168.142.120"){
        set $rewrite false;  ##匹配合法IP,將變量設(shè)置為假,正常跳轉(zhuǎn)頁面
    }
    #非法IP進行判斷打上標(biāo)記
    if ($rewrite = true){        ##匹配非法IP,跳轉(zhuǎn)到main的網(wǎng)頁
        rewrite (.+) /main.html;
    }
    #匹配標(biāo)記進行跳轉(zhuǎn)站點
    location = /main.html {       ##精確匹配
        root /usr/share/nginx/html;  ##站點路徑
    }

    location / {
        root  /usr/share/nginx/html;
        index index.html index.htm;
    }

2,創(chuàng)建非法IP站點及main的網(wǎng)頁頁面

[root@localhost conf.d]# cd /usr/share/nginx/html/ ##切換到站點中
[root@localhost html]# vim main.html  ##編輯非法IP訪問網(wǎng)頁內(nèi)容
<h2>this is test web</h2>
[root@localhost html]# systemctl restart nginx  ##重啟Nginx服務(wù)

3,訪問測試網(wǎng)頁

怎么在Nginx中使用Rewrite模塊

怎么在Nginx中使用Rewrite模塊

應(yīng)用場景3——基于舊,新域名跳轉(zhuǎn)并加目錄

將域名http://bbs.accp.com 下面的發(fā)帖都跳轉(zhuǎn)到http://www.accp.com/bbs 且域名跳轉(zhuǎn)后保持參數(shù)不變

1,修改Nginx默認(rèn)配置文件

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# vim default.conf  ##修改默認(rèn)配置文件
server {
    listen    80;
    server_name bbs.accp.com;  ##修改服務(wù)名稱

    #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;  ##永久重定向跳轉(zhuǎn)
    }

2,修改dns的區(qū)域數(shù)據(jù)配置文件(accp.com.zone)

[root@localhost conf.d]# cd /var/named/
[root@localhost named]# vim accp.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.142.130
[root@localhost named]# systemctl restart named  ##重啟解析服務(wù)
[root@localhost named]# systemctl restart nginx   ##重啟Nginx服務(wù)
[root@localhost named]# echo "nameserver 192.168.142.130" > /etc/resolv.conf 
##將解析服務(wù)器地址放到本地解析配置文件中

3,測試網(wǎng)頁

怎么在Nginx中使用Rewrite模塊

怎么在Nginx中使用Rewrite模塊

應(yīng)用場景4——基于參數(shù)匹配的跳轉(zhuǎn)

瀏覽器訪問:http://www.accp.com/100-(100|200)-100.html 跳轉(zhuǎn)到http://www.accp.com 頁面

1,修改Nginx默認(rèn)配置文件

[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)-一次多次的整數(shù)html為結(jié)尾的
        rewrite (.*) http://www.accp.com permanent;    ##永久重定向跳轉(zhuǎn)到主頁
    }

2,修改dns區(qū)域數(shù)據(jù)配置文件

  [root@localhost conf.d]# vim /var/named/accp.com.zone ##修改區(qū)域數(shù)據(jù)配置文件
  www IN A    192.168.142.130  
  [root@localhost conf.d]# systemctl restart named ##重啟解析服務(wù) 
  [root@localhost conf.d]# systemctl restart nginx   ##重啟Nginx服務(wù)

3,測試網(wǎng)頁

怎么在Nginx中使用Rewrite模塊

怎么在Nginx中使用Rewrite模塊

應(yīng)用場景5——基于目錄下所有PHP文件跳轉(zhuǎn)

訪問http://www.accp.com/upload/1.php 跳轉(zhuǎn)到首頁

1,修改Nginx默認(rèn)配置文件

[root@localhost ~]# cd /etc/nginx/conf.d/
  [root@localhost conf.d]# vim default.conf  ##修改默認(rèn)配置文件
  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為結(jié)尾的
          rewrite (.+) http://www.accp.com permanent;  ##跳轉(zhuǎn)到首頁
      }
  [root@localhost conf.d]# systemctl restart nginx  ##重啟Nginx服務(wù)

2,測試網(wǎng)頁

怎么在Nginx中使用Rewrite模塊

怎么在Nginx中使用Rewrite模塊

應(yīng)用場景6——基于最普通url請求的跳轉(zhuǎn),訪問一個具體的頁面跳轉(zhuǎn)到首頁

1,修改Nginx默認(rèn)配置文件

[root@localhost ~]# cd /etc/nginx/conf.d/
  [root@localhost conf.d]# vim default.conf  ##修改Nginx默認(rèn)配置文件
  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 {    ##匹配某一個特定的網(wǎng)頁
          rewrite (.+) http://www.accp.com permanent; ##跳轉(zhuǎn)到首頁
      }
  [root@localhost conf.d]# systemctl restart nginx  ##重啟Nginx服務(wù)

2,測試網(wǎng)頁

怎么在Nginx中使用Rewrite模塊

怎么在Nginx中使用Rewrite模塊

以上就是怎么在Nginx中使用Rewrite模塊,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前文章:怎么在Nginx中使用Rewrite模塊
標(biāo)題網(wǎng)址:http://muchs.cn/article44/geecee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、App設(shè)計、標(biāo)簽優(yōu)化用戶體驗、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計