Nginx rewrite模塊詳解、expires緩存配置及gzip壓縮策略

一、rewrite使用
rewrite的主要功能是實現URI地址的重定向,將用戶請求的URI基於regex所描述的模式進行檢查,匹配到時將其替換爲replacement指定的新的URI,即使用nginx提供的全局變量或自己設置的變量,結合正則表達式和標誌位實現url重寫以及重定向。如果replacement是以http://或https://開頭,則替換結果會直接以重向返回給客戶端。

1)rewrite指令語法結構:rewrite regex replacement [flag]

rewrite使用位置:server{}, location{}, if{}

regex 常用正則表達式說明:

flag標記說明:

2)if指令語法結構:if (condition) { ... }

引入新的上下文,條件滿足時,執行配置塊中的配置指令,大括號內的rewrite指令將被執行。if使用位置:server{}, location{}

condition條件說明:

3)set指令語法結構:set variable value;

用戶自定義變量,變量定義和調用都要以$開頭。set使用位置:server{}, location{}, if{}

4)return指令語法結構:return code [text];

停止處理並返回指定響應碼給客戶。return使用位置:server{}, location{}, if{}

rewrite使用示例:

http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  myformat  '$remote_addr - $remote_user [$time_local] "$request" ';
    access_log  logs/my.log  myformat;
    sendfile        on;
    keepalive_timeout  65;
 
    server {
        listen       8003;
        server_name  www.wf.com;
        location / {
            rewrite '^/images/(.*)\.(png|jpg)$' /img?file=$1.$2;
        set $image_file $1;
        set $image_type $2;
        }
    location /img {
        root html;
        try_files /$arg_file /image404.html;
    }
    location /image404.html {
        return 404 "image not found exception";
    }
    }    
}


如上配置中/images/feixiang.jpg會重寫到/img?file=feixiang.jpg,於是匹配到 location /img。然後通過try_files獲取存在的文件進行返回,如果文件不存在則直接返回404錯誤。

表面看rewrite和location功能有點像,都能實現跳轉,其主要區別在於rewrite是在同一域名內更改獲取資源的路徑,而location是對一類路徑做控制訪問或反向代理,可以proxy_pass到其他機器。很多情況下rewrite也會寫在location裏,它們的執行順序是:(1)執行server塊的rewrite指令;(2)執行location匹配;(3)執行選定的location中的rewrite指令。如果其中某步URI被重寫,則重新循環執行(1)~(3),直到找到真實存在的文件;循環超過10次,則返回500 Internal Server Error錯誤。

二、瀏覽器本地緩存配置及動靜分離
expires語法: expires 60s|m|h|d

expires使用位置:location{}

expires使用示例:

http {
    ......
    server {
        listen       8004;
        server_name  www.wf.com;
        location / {
            root html;
            index index.html index.htm;
        }
        location ~ \.(png|jpg|js|css|gif) {
            root html/images;
            expires 5m;
        }
    }
}

(1)在html目錄下創建一個images文件,在該文件中放一張圖片

(2)修改index.html, 增加<img src="feixiang.jpg"  alt="泉城廣場" />

(3)修改nginx.conf配置,配置兩個location實現動靜分離,並且在靜態文件中增加expires的緩存期限。

三、gzip壓縮策略
瀏覽器請求url,同時聲明當前瀏覽器可以支持壓縮類型(gzip、deflate等),服務端會把內容根據瀏覽器所支持的壓縮策略去進行壓縮並返回給瀏覽器,瀏覽器拿到數據以後進行解碼。

gzip使用示例:

http {
    ......
    server {
        listen       8004;
        server_name  www.wf.com;
        gzip on;
        gzip_buffers 4 16k;
        gzip_comp_level 7;
        gzip_min_length 500;
        gzip_types text/css text/xml application/javascript;
 
        location / {
            root html;
            index index.html index.htm;
        }
        location ~ \.(png|jpg|js|css|gif) {
            root html/images;
            expires 5m;
        }
    }
}

gzip語法說明:

語法

描述

gzip on|off

是否開啓gzip壓縮

gzip_buffers 4 16k

設置系統獲取幾個單位的緩存用於存儲gzip的壓縮結果數據流。4 16k代表以16k爲單位,安裝原始數據大小以16k爲單位的4倍申請內存。

gzip_comp_level [1-9]

壓縮級別,級別越高,壓縮越小,但是會佔用CPU資源

gzip_disable

正則匹配,表示什麼樣的瀏覽器不進行gzip

gzip_min_length

開始壓縮的最小長度(小於多少就不做壓縮)

gzip_http_version 1.0|1.1

表示開始壓縮的http協議版本

gzip_proxied

nginx 做前端代理時啓用該選項,表示無論後端服務器的headers頭返回什麼信息,都無條件啓用壓縮

gzip_types text/pliain,application/xml

對那些類型的文件做壓縮 (類型參考文件conf/mime.conf)

gzip_vary on|off

是否傳輸gzip壓縮標識

gzip使用注意事項:

(1)類似圖片和mp3這樣的二進制文件,沒必要做壓縮處理,因爲這類文件壓縮比很小,壓縮過程會耗費CPU資源。

(2)太小的文件沒必要壓縮,因爲壓縮以後會增加一些頭信息,反而導致文件變大。

(3)Nginx默認只對text/html進行壓縮 ,如果要對html之外的內容進行壓縮傳輸,需要我們進行手動配置。

發佈了15 篇原創文章 · 獲贊 3 · 訪問量 1213
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章