配置Nginx服務中Rewrite的應用

Rewrite跳轉場景

  • URL看起來更規範、合理
  • 企業會將動態URL地址僞裝成靜態地址提供服務
  • 網址換新域名後,讓舊的訪問跳轉到新的域名上
  • 服務端某些業務調整

Rewrite跳轉實現

配置Nginx服務中Rewrite的應用

Rewrite實用場景

Nginx跳轉需求的實現方式

  • 使用rewrite進行匹配跳轉
  • 使用if匹配全局變量後跳轉
  • 使用location匹配再跳轉

rewrite放在server{},if{},location{} 段中

對域名或參數字符串

  • 使用if全局變量匹配
  • 使用proxy_ pass反向代理

Nginx正則表達式

常用的正則表達式元字符

字符 說明
^ 匹配輸入字符串的起始位置
$ 匹配輸入字符串的結束位置
* 匹配前面的字符零次或多次
+ 匹配前面的字符一次或多次
? 匹配前面的字符零次或一次
. 匹配除“\n”之外的任何單個字符。使用諸如"[.\n]"之 類的模式,可匹配包括“n”在內的任意字符
\ 將後面接着的字符標記爲一個特殊字符或一個原義字符或一個向後引用
\d 匹配純數字
{n} 重複n次
{n,} 重複n次或更多次
[c] 匹配單個字符c
[a-z] 匹配a-z小寫字母的任意一個
[a-zA-Z] 匹配a-z小寫字母或A-Z大寫字母的任意一 個

Rewrite命令

語法

rewrite    <regex>   <replacement>   [flag];
<regex>  //正則     <replacement>   //跳轉後的內容    [flag]   //rewrite支持flag標記

flag標記說明

標記 說明
last 相當於Apache[L]標記,表示完成rewrite
break 本條規則匹配完成即終止,不再匹配後面的任何規則
redirect 返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址,爬蟲不會更新url
permanent 返回301永久重定向,瀏覽器地址欄會顯示跳轉後的URL地址,爬蟲更新url

last和break比較

last break
使用場景 一般寫在serverif 一般使用在location
URL匹配 不終止重寫後的url匹配 終止重寫後的url匹配

location分類

分類

location = patt {}       [精準匹配]
location patt {}         [一般匹配]
location ~ patt {}       [正則匹配]

正則匹配的常用表達式

標記 說明
~ 執行一個正則匹配,區分大小寫
~* 執行一個正則匹配,不區分大小寫
!~ 執行一個正則匹配,區分大小寫不匹配
!~* 執行一個正則匹配,不區分大小寫不匹配
^~ 普通字符匹配;使用前綴匹配。如果匹配成功,則不再匹配其他location
= 普通字符精確匹配。也就是完全匹配
@ 定義一個命名的location,使用在內部定向時

location優先級

相同類型的表達式,字符串長的會優先匹配

按優先級排列

  • =類型
  • ^~類型表達式
  • 正則表達式(~~*)類型
  • 常規字符串匹配類型,按前綴匹配
  • 通用匹配(/),如果沒有其它匹配,任何請求都會匹配到

比較rewrite和location

相同點

  • 都能實現跳轉

不同點

  • rewrite是在同一域名內更改獲取資源的路徑
  • location是對一-類路徑做控制訪問或反向代理,還可以proxy_pass到其他機器

rewrite會寫在location裏, 執行順序

  • 執行server塊裏面的rewrite指令
  • 執行location匹配
  • 執行選定的location中的rewrite指令

Location優先級的示例

location =/ {
[ configuration A ]           //精確匹配/,主機名後面不能帶任何字符串
}

location/ {
[ configuration B ]           //所有的地址都以/開頭,這條規則將匹配到所有請求,但正則和最長字符串會優先匹配
}

location /documents/ {
[ configuration C ]           //匹配任何以/documents/開頭的地址,當後面的正則表達式沒有匹配到時,才起作用
}

location ~ /documents/abc {
[ configuration D ]            //匹配任何以/documents/abc開頭的地址當後面的正則表達式沒有匹配到時,纔會起作用
}

location ^~ /images/ {
[ configuration E ]             //以/images/開頭的地址,匹配符合後,停止往下匹配
}

location ~* \.(gif|jipg|jpeg)$ {
[ configuration F ]             
}
//匹配所有以gif,jpg或jpeg結尾的請求,/images/下的圖片會被[ configuration E ]處理,因爲^~的優先級更高

location /images/abc {
[ configuration G ]               //最長字符匹配到/images/abc,優先級最低
}

location ~ /images/abc {
[ configuration H ]                //以/images/abc開頭的,優先級次之
}

location /images/abc/1.html {
[ configuration I ]                 //如果和正則~ /images/abc/1.html相比,正則優先級更高
}

location優先級規則

匹配某個具體文件

  • (location =完整路徑) > (location ^~完整路徑) > (location ~*完整路徑) > (location ~完整路徑) > (location 完整路徑) > (location /)

用目錄做匹配訪問某個文件

  • (location= 目錄) > (location ^~目錄/) > (location ~ 目錄) > (location ~*目錄) > (location 目錄) > (location /)

配置實例

首先安裝Nginx服務,安裝並配置DNS服務

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm     //安裝nginx官方源碼包
獲取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
警告:/var/tmp/rpm-tmp.ymMjSa: 頭V4 RSA/SHA1 Signature, 密鑰 ID 7bd9bf62: NOKEY
準備中...                          ################################# [100%]
正在升級/安裝...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@localhost ~]# yum install nginx -y     //安裝nginx服務
已加載插件:fastestmirror, langpacks
已加載插件:fastestmirror, langpacks
base                                                                       | 3.6 kB  00:00:00     
extras                                                                     | 2.9 kB  00:00:00     
...//省略部分內容...
已安裝:
  nginx.x86_64 1:1.16.1-1.el7.ngx                                                                   
完畢!
[root@localhost ~]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf                 //配置文件路徑
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf     //編輯配置文件
server {
    listen       80;
    server_name  www.aacp.com;           //更改域名

    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;   //更改日誌文件名稱,並開啓功能

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
...//省略部分內容...
:wq
[root@localhost ~]# yum install bind -y          //安裝DNS功能
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
...//省略部分內容...
已安裝:
  bind.x86_64 32:9.11.4-9.P2.el7 
  ...//省略部分內容...
完畢!
[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; };
...//省略部分內容...
:wq
[root@localhost ~]# vim /etc/named.rfc1912.zones
...//省略部分內容...
zone "accp.com" IN {
        type master;
        file "accp.com.zone";
        allow-update { none; };
};
...//省略部分內容...
:wq
[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. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.144.133
:wq
[root@localhost named]# systemctl start named               //啓動DNS服務
[root@localhost named]# systemctl start nginx               //啓動nginx服務
[root@localhost named]# systemctl stop firewalld.service    //關閉防火牆
[root@localhost named]# setenforce 0                        //關閉增強性安全功能
[root@localhost named]# netstat -ntap | grep nginx       //查看nginx服務是否開啓
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2769/nginx: master 

在客戶機中測試nginx服務是否成功搭建

配置Nginx服務中Rewrite的應用配置Nginx服務中Rewrite的應用

配置基於域名的跳轉

公司舊域名www.accp.com, 因業務需求有變更,需要使用新域名www.kgc.com代替

  • 不能廢除舊域名
  • 從舊域名跳轉到新域名,且保持其參數不變

配置實例

[root@localhost named]# vim /etc/nginx/conf.d/default.conf     //編輯Nginx配置文件
...//省略部分內容...
 #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;
           //在location模塊中添加if判斷語句,當輸入"www.accp.com"訪問網頁時跳轉到www.kgc.com中
         }
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }   

    #error_page  404              /404.html;
...//省略部分內容...
:wq
[root@localhost named]# vim /etc/named.rfc1912.zones
...//省略部分內容...
zone "accp.com" IN {
        type master;
        file "accp.com.zone";
        allow-update { none; };
};

zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};      
...//省略部分內容...
:wq
[root@localhost named]# cp -p accp.com.zone kgc.com.zone
[root@localhost named]# systemctl restart nginx
[root@localhost named]# systemctl restart named

在客戶機中測試訪問

配置Nginx服務中Rewrite的應用配置Nginx服務中Rewrite的應用配置Nginx服務中Rewrite的應用配置Nginx服務中Rewrite的應用

配置基於客戶端IP訪問跳轉

今天公司業務版本上線,所有IP訪問任何內容都顯示一個固定維護頁面,只有公司IP訪問正常

配置實例

  • 開啓兩臺客戶機,分別查看兩臺客戶端IP地址,並在服務器配置可以訪問的IP地址

配置Nginx服務中Rewrite的應用配置Nginx服務中Rewrite的應用

[root@localhost conf.d]#  vim default.conf        //編輯配置文件
...//省略部分內容...
#charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;

    set $rewrite true;              //設置是否合法的IP標記
    if ($remote_addr = "192.168.144.128"){    //判斷是否爲合法地址,如果爲合法地址執行操作
            set $rewrite false;
    }
    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;
    }
...//省略部分內容...
:wq
[root@localhost conf.d]# cd /usr/share/nginx/html/      //進nginx服務站點
[root@localhost html]# ls         //查看信息
50x.html  index.html
[root@localhost html]# vim main.html      //編輯跳轉的網頁內容
<h1>this is test web</h1>
:wq
[root@localhost html]# systemctl restart nginx    //重啓網站

分別在客戶機中訪問測試

配置Nginx服務中Rewrite的應用配置Nginx服務中Rewrite的應用

配置基於舊、新域名跳轉並加目錄

  • 將域名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.aacp.com;          //更改域名

    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    location /post {               //設置匹配字段,字段匹配執行跳轉操作
          rewrite (.+) http://www.accp.com/bbs$1 permanent;
    }
                             //注意刪除上面設置的基於客戶端IP訪問的跳轉的配置條目
    location / {     
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
...//省略部分內容...
:wq
[root@localhost html]# vim /var/named/accp.com.zone        //編輯DNS服務區域數據文件
$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.144.133          //更改主機頭解析
:wq
[root@localhost html]# systemctl restart nginx      //重啓nginx服務
[root@localhost html]# systemctl restart named       //重啓DNS服務
  • 在客戶機中測試訪問

配置Nginx服務中Rewrite的應用配置Nginx服務中Rewrite的應用

配置基於匹配參數訪問的跳轉

  • 瀏覽器訪問http://www.accp.com/100-(100|200)-100.html跳轉到http://www.accp.com頁面
[root@localhost html]# vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  www.aacp.com;       //將服務器域名更改會www

    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    if ($request_uri ~ ^/100-(100|200)-(\d+).html$){    //刪除此處上面設置的基於新、舊域名跳轉並加目錄跳轉的配置條目
          rewrite (.*) http://www.accp.com permanent;     並設置基於參數訪問時跳轉回主網頁
    }

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

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
...//省略部分內容...
:wq
[root@localhost html]# vim /var/named/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
www IN  A       192.168.144.133      //更改主機頭解析
:wq
[root@localhost html]# systemctl restart nginx        //重啓nginx服務
[root@localhost html]# systemctl restart named        //重啓DNS服務
  • 在客戶機中訪問測試

配置Nginx服務中Rewrite的應用配置Nginx服務中Rewrite的應用

配置基於目錄下所有php文件跳轉

  • 訪問http://www.accp.com/upload/1.php跳轉到首頁
[root@localhost html]# vim /etc/nginx/conf.d/default.conf     //編輯配置文件
server {
    listen       80;
    server_name  www.aacp.com;

    #charset koi8-r;
    access_log  /var/log/nginx/www.accp.com-access.log  main;
    location ~* /upload/.*\.php$ {        //刪除上面設置的匹配參數跳轉訪問,配置匹配所有php結尾訪問跳轉回主頁
          rewrite (.+) http://www.accp.com permanent;
    } 

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

    #error_page  404              /404.html;
...//省略部分內容...
:wq
[root@localhost html]# systemctl restart nginx        //重啓nginx服務
  • 在客戶機中訪問測試

配置Nginx服務中Rewrite的應用配置Nginx服務中Rewrite的應用

配置基於最普通url請求的跳轉

  • 訪問一個具體的頁面跳轉到首頁
[root@localhost html]# vim /etc/nginx/conf.d/default.conf    //編輯配置文件
server {
    listen       80;
    server_name  www.aacp.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;
    }

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

    #error_page  404              /404.html;
...//省略部分內容...
:wq
[root@localhost html]# systemctl restart nginx        //重啓nginx服務
  • 在測試機中測試訪問

配置Nginx服務中Rewrite的應用配置Nginx服務中Rewrite的應用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章