nginx rewrite

從不吃素陳長老發表於2020-12-02

Nginx Rewrite概述

Rewrite跳轉場景

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

Rewrite 實際場景

  1. Nginx跳轉需求的實現方式
    使用rewrite進行匹配跳轉
    使用if匹配全域性變數後跳轉
    使用location匹配再跳轉

  2. rewrite放在server{},if{},location{}段中
    location只對域名後邊的出去傳遞引數外的字串起作用

  3. 對域名或引數字串
    使用if全域性變數匹配
    使用proxy_pass反向代理

Nginx正規表示式

常用的正規表示式說明

字元說明
^匹配輸入字串的其實位置
$匹配輸入字串的結束位置
*匹配前面的字元零次或多次
+匹配前面的字元一次或多次
匹配前面的字元零次或一次
.匹配差除\n之外的任何單個字元
\將後面接著的字元標記為一個特殊字元或一個原義字元或一個向後引用
\d匹配純數字
{n}重複n次
{n,}重複n次或更多次
[c]匹配單個字元c
[a-z]匹配a-z小寫字母的任意一個
[a-zA-Z]匹配a-z小寫字母或A-Z大寫字母的任意一個

Rewrite命令

  1. Rewrite命令語法

     rewrite       <regex>           <replacement>          [flag];
                  正則           跳轉後內容(目標)            rewrite支援flag標記
    
  2. flag標記說明

標記說明
last相當於Apache的[L]標記,標識完成rewrite
break本條規則匹配完成即終止,不再匹配後面的任何規則
redirect返回302臨時重定向,瀏覽器地址會顯示跳轉後的URL地址,爬蟲不會更新url
permanent返回301永久重定向,瀏覽器位址列會顯示跳轉後的URL地址,爬蟲更新url
  1. last和break比較
lastbreal
使用場景一般在server和if中一般使用在location中
URL匹配不總之重寫後的url匹配終止重寫後的url匹配

location分類

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

  2. 正則匹配常用表示式

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

location 優先順序

  1. 相同型別的表示式,字串長的會優先匹配

  2. 按優先順序排列
    =型別
    ^~型別表示式
    正規表示式(和*)型別
    常用字串匹配型別、按字首匹配
    通用匹配(/),如果沒有其它匹配,任何請求都會匹配到

  3. Location優先順序示例

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


location = /documents/abc {
[configuration B] 
}          匹配任何以/documents/abc開頭的地址,當後面的正規表示式沒有匹配到時,才會起作用


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


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

Location優先順序規則

  1. 匹配某個具體檔案

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

  1. 用目錄做匹配訪問某個檔案

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

比較rewrtie和location

  1. 相同點
    都能實現跳轉

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

  3. rewrite會寫在location裡,執行順序
    執行server塊裡面的rewrite指令
    執行location匹配
    執行選定的location中的rewrite指令

Rewrite應用例項

基於域名的跳轉

不能廢除舊域名
從舊域名跳轉到新域名,切保持其引數不變

1.修改預設站點配置檔案(nginx.conf)

server {
    listen       80;
    server_name  www.test.com;
        if ($host = 'www.test.com')
        {
            rewrite ^/(.*)$ http://www.newtest.com/$1 permanent;
        }
    charset utf-8;
    access_log  logs/test.com.access.log  main;

在這裡插入圖片描述

    server {
           listen      80;
           server_name  www.newtest.com;
           charset utf-8;
           access_log /var/log/nginx/www.newtest.com.access.log main;


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


在這裡插入圖片描述

在瀏覽器上查結果
在這裡插入圖片描述

基於客戶端IP地址的跳轉

應用場景,其它IP訪問任何內容都會顯示一個固定維護頁面,只有設定IP可以正常訪問

  1. 修改預設站點配置檔案(nginx.conf)
        set $rewrite true;
        if ($remote_addr = "20.0.0.18"){
            set $rewrite false;
        }
        if ($rewrite = true) {
            rewrite (.+) /maintenance.html;
        }
        location = /maintenance.html {
            root /usr/local/nginx/html;
        }

在這裡插入圖片描述

  1. 新增新域名到站點位置,並在瀏覽器上測試

     echo "this web is Maintaining,please visit later this pen apple" >> /usr/local/nginx/html/maintenance.html
    

在這裡插入圖片描述

  1. 修改本機IP,在客戶機訪問新IP 20.0.0.10 訪問 20.0.0.15
    在這裡插入圖片描述

基於舊新域名的跳轉並加目錄

  1. 在 /usr/local/nginx/html/abc/post/建立1.html
cd /usr/local/nginx/html/
mkdir -p abc/post
cd abc/post/
echo "this is 20.0.0.18" >> 1.html
  1. 修改預設站點配置檔案(nginx.conf)

     location /post {
         rewrite (.+) http://www.test.com/bbs$1 permanent;
     }
    
     location / {
         root   html;
         index  index.html index.htm;
     }
    

在這裡插入圖片描述

  1. 新增新域名到站點位置
    server {
        listen       80;
        server_name  www.newtest.com;
        charset utf-8;
        access_log  /var/log/nginx/www.newtest.com.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

    }

在這裡插入圖片描述

  1. 測試
    在這裡插入圖片描述

基於匹配引數的跳轉

  1. 修改預設站點配置檔案(nginx.conf)
    匹配100-100-任意數字的.html結尾的檔案,重定向到/www.test.com

     if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
         rewrite (.*) http://www.test.com permanent;
     }
    

在這裡插入圖片描述

  1. 測試www.test.com/100-100-100.html
    在這裡插入圖片描述

相關文章