1. 程式人生 > >Nginx基於gzip壓縮配置參數(Ngx_http_gzip_module)

Nginx基於gzip壓縮配置參數(Ngx_http_gzip_module)

web;nginx;linux;

Ngx_http_gzip_module(壓縮模塊):基於gzip方式對響應報文作壓縮;

官方文檔http://nginx.org/en/docs/http/ngx_http_gzip_module.html

官方定義:The ngx_http_gzip_module module is a filter that compresses responses using the “gzip” method. This often helps to reduce the size of transmitted data by half or even more.

相關指令:

gzip:
定義是否啟用“gzip”壓縮功能,默認不啟用;
gzip_buffers:
定義設置用於壓縮響應的緩沖區數量和大小,默認值:gzip_buffers 32 4k|16 8k,代表多少個緩沖區(number),每個的大小為多少(size);
gzip_comp_level:
設置響應gzip壓縮級別,壓縮級別1~9之間;默認值:gzip_comp_level 1;
gzip_disable:
定義“User-Agent”請求進行正則表達式匹配,User-Agent表示瀏覽器相關版本等,通過User-Agent檢測避開壓縮支持不好的瀏覽器;
gzip_min_length:
設置一個響應壓縮的最小長度;大於此數字進行壓縮;默認:gzip_min_length 20;
gzip_http_version:
定義HTTP協議版本進行壓縮,默認http_version 1.1;
gzip_proxied:
Nginx作為代理服務器時啟用,設置參數;
gzip_types:
定義壓縮的響應內容MIME類型;默認:gzip_types text/html;
gzip_vary:
定義是否在發送客戶端的響應頭部插入“Vary:Accept-Encoding”響應信息,用於客戶端瀏覽器識別內容是否已經進行壓縮;默認:gzip_vary off;

其中gzip_proxied指令:

Syntax:gzip_proxied off | expired | no-cache | no-store
| private | no_last_modified | no_etag | auth |any ...;

Default:
gzip_proxied off;
Context:http, server, location

off:對代理請求不進行壓縮;

expired:定義響應報文首部包含“Expires”頭,則啟用壓縮功能;(Expires定義資源過期時間);

no-cache:定義如果Cache-Control中包含“no-cache”,則啟用壓縮功能;no-cache,緩存遵循web服務器驗證

no-store:定義如果Cache-Control中包含“no-store”,則啟用壓縮功能;no-store,禁止緩存

private:定義如果Cache-Control中包含“private”,則啟用壓縮功能;

no_last_modified:定義包含“Last-Modified”首部,啟用壓縮功能;

no_etag:定義包含“ETag”首部,啟用壓縮功能;

auth:定義包含Authorization首部,則啟用壓縮功能;

any:全部請求都做壓縮;

相關設置:

[root@GaoServer ~]# vim /etc/nginx/nginx.conf
......
#定義相關配置段在“Context:http,server,location;”
    gzip on;
    gzip_buffers 32 4k;    #可以不設置,默認使用默認值;
    gzip_comp_level 5;    #壓縮比越大,過程資源消耗越大;
    #gzip_disable;    #官方建議“msie6”針對IE設置,也可以通過正則表達式匹配“MSIE [4-6]\.”
    #gzip_proxied any; 
    gzip_min_length 64;    
    gzip_types text/css; #默認text/html;
......
#查看相關MIME類型
[root@GaoServer ~]# cat /etc/nginx/mime.types 
types {
    text/html                          html htm shtml;
    text/css                           css;
    text/xml                           xml;
    image/gif                          gif;
......
[root@GaoServer ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@GaoServer ~]# nginx -s reload


本文出自 “Gning丶” 博客,請務必保留此出處http://gning.blog.51cto.com/11847592/1980895

Nginx基於gzip壓縮配置參數(Ngx_http_gzip_module)