Interpretation of Nginx gzip module

Table of contents

Basic introduction to gzip

How gzip works

gzip in Nginx

It is not recommended to enable the gzip scenario in Nginx


Basic introduction to gzip

gzip is the abbreviation of GNUzip, which was first used for file compression in UNIX systems. Gzip encoding over HTTP protocol is a technology used to improve the performance of web applications. The web server and client (browser) must both support gzip . Currently, mainstream browsers such as Chrome, Firefox, IE, etc. all support this protocol. Common servers such as Apache, Nginx, and IIS also support gzip.

How gzip works

 

  • 1) The browser requests the URL and sets the attribute accept-encoding:gzip in the request header. Indicates that the browser supports gzip.
  • 2) After receiving the request sent by the browser, the server determines whether the browser supports gzip. If it supports gzip, it will send compressed content to the browser. If it does not support it, it will send uncompressed content to the browser. Generally, both browsers and servers support gzip, and the response headers returned contain content-encoding:gzip.
  • 3) After receiving the response from the server, the browser determines whether the content is compressed. If it is compressed, it decompresses and displays the page content.

gzip in Nginx

In order to improve the speed at which users obtain response data, the Nginx server can gzip compress the response data and reduce the size of the response data before sending it to the client browser. Compared with allowing users to browse Web pages, the above method displays faster.

To enable response data gzip compression (ngx_http_gzip_module module) function, the user's browser needs to also support gzip decompression function

instruction Scope default value Command value options Instruction description
gzip http、server、location、if in location off on or off Enable gzip function
gzip_buffers http、server、location 32 4k or 16 8k -- Set gzip compression buffer
gzip_comp_level http、server、location 1 -- Set the gzip compression level, the value range is 1~9. The larger the value of this command, the higher the degree of compression.
gzip_disable http、server、location -- -- Turn off gzip compression when the content of the User-Agent attribute field in the request header matches the command value.
gzip_http_version http、server、location 1.1 1.0 or 1.1 Sets the earliest HTTP protocol version for compressed requests
gzip_min_length http、server、location 20 -- Set the minimum length of response data with gzip compression enabled, based on the value of Content-Length in the response header. If Content-Length does not exist, the directive is invalid; if the directive value is 0, it means all compression
gzip_proxied http、server、location off off or expired or no-cache or no-store or private or no_last_modified or no_etag or auth or any Determine whether to enable gzip compression based on the response header attribute field of the response data returned by the proxy server
gzip_types http、server、location text/html -- Set the MIME type of response data that can be gzip compressed. When the command value is *, it means all MIME types.
gzip_vary http、server、location off on or off Add Vary:Accept-Encoding in the response header and return it to the front-end proxy or CDN server to determine whether to send a gzip cached copy to the client to prevent the proxy or CDN server from responding to a cached copy that does not have gzip compression. Browser with decompression capabilities

The command value options of the gzip_proxied directive are described as follows:

  • off: Turn off the command function;
  • expired: If the HTTP response header contains the attribute field Expires, compression is enabled;
  • no-cache: If the HTTP response header contains the attribute field Cache-Control: no-cache, compression is enabled;
  • no-store: If the HTTP response header contains the attribute field Cache-Control: no-store, compression is enabled;
  • private: If the HTTP response header contains the attribute field Cache-Control:private, compression is enabled;
  • no_last_modified: If the HTTP response header does not contain the attribute field Last-Modified, compression is enabled;
  • no_etag: If the HTTP response header does not contain the attribute field ETag, compression is enabled;
  • auth: If the HTTP response header contains the Authorization attribute field, compression is enabled;
  • any: Enables compression for all response data.

When the client browser does not support gzip compression, you can use the ngx_http_gunzip_module module to decompress the compressed data and send it to the client. Browsers that support gzip compression are not processed. 

gzip_static always;                  # 始终发送静态的gzip压缩数据
gunzip on;                           # 若客户端浏览器不支持gzip压缩数据,则解压后发送
gunzip_buffers 16 8k;                                         # 解压缓冲区大小为128KB
gzip_proxied expired no-cache no-store private auth;    # 当被代理的服务器符合条件时,
                                                        # 对响应数据启用gzip压缩

gzip on;                             # 启用动态gzip压缩功能
gzip_min_length  1k;                 # 响应数据超过1KB时启用gzip压缩
gzip_buffers     4 16k;              # 动态压缩的缓冲区大小是64KB
gzip_comp_level 3;                   # 压缩级别为3
gzip_types       text/plain application/x-javascript
                text/css application/xml text/javascript
                application/x-httpd-php image/jpeg
                image/gif image/png; # 对指定的MIME类型数据启用动态压缩
gzip_vary on;                        # 向前端代理或缓存服务器发送添加"Vary: Accept-
                                     # Encoding"的响应数据

It is not recommended to enable the gzip scenario in Nginx

Although the Gzip compression function of Nginx is easy to use, it is not recommended to enable this compression function for the following two types of file resources.

  • 1) Reason for picture type resources (and video files) : Pictures such as jpg and png files themselves are compressed, so even after gzip is turned on, there is not much difference between the size before and after compression.
  • 2) Large file resources Reason: It will consume a lot of CPU resources and may not have obvious effects.

Guess you like

Origin blog.csdn.net/m0_62436868/article/details/133126602