序
本文主要解析一下nginx ngx_http_gzip_module以及ngx_http_gzip_static_module中的gzip相關(guān)配置參數(shù)。
gzip
名稱 | 默認(rèn)配置 | 作用域 | 官方說明 | 中文解讀 | 模塊 |
---|---|---|---|---|---|
gzip | gzip off; | http, server, location, if in location | Enables or disables gzipping of responses. | 設(shè)置是否開啟對(duì)后端響應(yīng)的gzip壓縮怎炊,然后返回壓縮內(nèi)容給前端 | ngx_http_gzip_module |
gzip_buffers | gzip_buffers 32 4k或16 8k; | http, server, location | Sets the number and size of buffers used to compress a response. By default, the buffer size is equal to one memory page. This is either 4K or 8K, depending on a platform. | 設(shè)置用于壓縮后端response的buffer的數(shù)量和每個(gè)的大小谭企,默認(rèn)每個(gè)buffer大小為一個(gè)內(nèi)存頁廓译,根據(jù)平臺(tái)不同可能是4k或8k | ngx_http_gzip_module |
gzip_comp_level | gzip_comp_level 1; | http, server, location | Sets a gzip compression level of a response. Acceptable values are in the range from 1 to 9. | 指定gzip壓縮的級(jí)別,默認(rèn)為1债查,該值可設(shè)置的范圍是1-9非区,1為最小化壓縮(處理速度快),9為最大化壓縮(處理速度慢),數(shù)字越大壓縮的越好盹廷,也越占用CPU時(shí)間 | ngx_http_gzip_module |
gzip_disable | 沒有默認(rèn)值 | http, server, location | Disables gzipping of responses for requests with User-Agent header fields matching any of the specified regular expressions. | 正則匹配User-Agent中的值征绸,匹配上則不進(jìn)行g(shù)zip | ngx_http_gzip_module |
gzip_min_length | gzip_min_length 20; | http, server, location | Sets the minimum length of a response that will be gzipped. The length is determined only from the Content-Length response header field. | 設(shè)定進(jìn)行g(shù)zip壓縮的閾值,當(dāng)后端response的Content-Length大小小于該值則不進(jìn)行g(shù)zip壓縮 | ngx_http_gzip_module |
gzip_http_version | gzip_http_version 1.1; | http, server, location | Sets the minimum HTTP version of a request required to compress a response. | 設(shè)定進(jìn)行g(shù)zip壓縮的最小http版本 | ngx_http_gzip_module |
gzip_proxied | gzip_proxied off; | http, server, location | Enables or disables gzipping of responses for proxied requests depending on the request and response. The fact that the request is proxied is determined by the presence of the "Via" request header field. | 根據(jù)request或響應(yīng)的相關(guān)header的值來決定是否進(jìn)行g(shù)zip | ngx_http_gzip_module |
gzip_types | gzip_types text/html; | http, server, location | Enables gzipping of responses for the specified MIME types in addition to "text/html". The special value "*" matches any MIME type (0.8.29). Responses with the "text/html" type are always compressed. | 指定哪些mime types啟用gzip壓縮俄占,默認(rèn)text/html | ngx_http_gzip_module |
gzip_vary | gzip_vary off; | http, server, location | Enables or disables inserting the "Vary: Accept-Encoding" response header field if the directives gzip, gzip_static, or gunzip are active. | 是否往response header里頭寫入Vary: Accept-Encoding | ngx_http_gzip_module |
gzip_static | gzip_static off; | http, server, location | Enables ("on") or disables ("off") checking the existence of precompressed files. The following directives are also taken into account: gzip_http_version, gzip_proxied, gzip_disable, and gzip_vary. | 開啟之后歹垫,接到(靜態(tài)文件)請(qǐng)求會(huì)到url相同的路徑的文件系統(tǒng)去找擴(kuò)展名為".gz"的文件,如果存在直接把它發(fā)送出去颠放,如果不存在排惨,則進(jìn)行g(shù)zip壓縮,再發(fā)送出去 | ngx_http_gzip_static_module |
實(shí)例
http {
gzip on;
gzip_buffers 8 16k; ## 這個(gè)限制了nginx不能壓縮大于128k的文件
gzip_comp_level 2;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_min_length 512; ##單位byte
gzip_http_version 1.0;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip_static on;
//......
}