Interpretation of nginx gzip configuration parameters



This article mainly analyzes gzip related configuration parameters in nginx ngx_http_gzip_module and ngx_http_gzip_static_module.


Name Default configuration action scope Official statement Interpretation in Chinese module
gzip gzip off; http, server, location, if in location Enables or disables gzipping of responses. Sets whether gzip compression for back-end response is turned on, and then returns the compressed content to the front end. ngx_http_gzip_module
gzip_buffers Gzip_buffers 32 4k or 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. Set the number of buffers used to compress the backend response and the size of each buffer. The default size of each buffer is one memory page, which may be 4k or 8k depending on the platform. 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. Specifies the gzip compression level, which defaults to 1. The range that this value can be set is 1-9. 1 is minimum compression (fast processing speed), 9 is maximum compression (slow processing speed). The larger the number, the better the compression and the more CPU time it takes. ngx_http_gzip_module
gzip_disable No default http, server, location Disables gzipping of responses for requests with User-Agent header fields matching any of the specified regular expressions. Regularly matches the value in User-Agent, while gzip is not used for matching. 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. Set the threshold for gzip compression. When the Content-Length size of the back-end response is smaller than this value, gzip compression will not be performed. 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. Sets the minimum http version for gzip compression 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 determine d by the presence of the “Via” request header field. According to the value of the request or the relevant header of the response, it is decided whether gzip should be carried out or not. 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. Specifies which mime types enable gzip compression, default 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. Whether to write Vary: Accept-Encoding into response header 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. After opening, a request (static file) will go to the file system with the same url path to find a file with “.gz” extension. if it exists, it will be sent directly. if it does not exist, gzip compression will be carried out, and then it will be sent out. ngx_http_gzip_static_module


http {
    gzip on;
    gzip_buffers 8 16k; ## 这个限制了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;