Gzip compression can reduce website bandwidth consumption and increase access speed.
The page is compressed on the nginx server and decompressed and parsed on the browser,
At present, most popular browsers are lagging behind gzip compression, so don't worry.
By default, Gzip compression of nginx is disabled, and nginx only compresses text/html by default.
The main configuration is as follows:
Gzip on; # enable gzip_http_version 1.0; # default 1.1 gzip_vary on; gzip_comp_level 6; gzip_proxied any; gzip_types text/plain text/HTML text/CSS application/JSON application/X-JavaScript text/XML application/XML + RSS text/JavaScript; # The compressed file type gzip_buffers is 16 8 K; # sets the size of gzip applied memory, which is used to set the size of gzip applied memory by a multiple of the block size, the function is to apply for memory space multiple by block size # disable gzip for certain browsers. gzip_disable "MSIE [1-6]. (?!. * Sv1) "; # IE6 does not support gzip. You need to disable IE6. It's awful !!!!
Note: The default value of gzip_http_version is 1.1, which means that the HTTP/1.1 request will be compressed by gzip.
If proxy_pass is used for reverse proxy, the nginx and the backend upstream server communicate with each other through HTTP/1.0.
Gzip parameter description:
Determine whether to enable the gzip Module
Param: On | off
Example: gzip on;
Gzip_buffers
Set the size of gzip applied memory. The function is to apply for memory space by a multiple of the block size.
Param1: multiples of int
Param2: the unit behind int (k) is K.
Example: gzip_buffers 4 8 K;
Gzip_comp_level
Set the gzip compression level. The lower the level, the faster the compression speed, the smaller the File compression ratio. The slower the speed, the larger the File compression ratio.
Param: 1-9
Example: gzip_com_level 1;
Gzip_min_length
When the returned content is greater than this value, Gzip is used for compression. The unit is K. When the value is 0, all pages are compressed.
Param: int
Example: gzip_min_length 1000;
Gzip_http_version
Used to identify the HTTP protocol version. Earlier Browsers Do not support gzip compression, and garbled characters are displayed. Therefore, this option is added to support earlier versions. Currently, this option can be ignored.
Partam: 1.0 | 1.1
Example: gzip_http_version 1.0
Gzip_proxied
Nginx is enabled as the reverse proxy,
Param: Off | expired | no-Cache | no-sotre | private | no_last_modified | no_etag | auth | any]
Expample: gzip_proxied no-cache;
Off-Disable Data Compression For All proxy results
Expired-enable compression. If the header contains "expires" header information
No-Cache-enable compression. If the header contains the "cache-control: No-Cache" header
No-store-enable compression. If the header contains the "cache-control: No-store" header
Private-enable compression. If the header contains "cache-control: Private" header information
No_last_modified-enable compression. If the header contains "last_modified" header information
No_etag-enable compression. If the header contains "etag" header information
Auth-enable compression. If the header contains "Authorization" header information
Any-unconditionally compress all result data
Gzip_types
Sets the MIME type to be compressed. Non-set values are not compressed.
Param: text/html | application/X-JavaScript | text/CSS | application/XML
Example: gzip_types text/html;
Gzip_vary on;
It is related to the HTTP header and adds a vary header for the proxy server. Some browsers support compression and some do not support compression, so we can avoid wasting and compressing unsupported HTTP headers, therefore, we can determine whether compression is required based on the HTTP header of the client.