Nginx uses gzip to compress Web pages

Source: Internet
Author: User

GZIP encoding on the HTTP protocol is a technique used to improve the performance of Web applications. High-traffic Web sites often use gzip compression technology to make users feel faster. This generally refers to the WWW server installed in a feature, when someone to access the server in the site, the server of this feature will compress the content of the Web page to be transmitted to the Computer browser display. Generally, the content of plain text can be compressed to 40% of the original size. So the transmission is fast, the effect This is when you click on the URL and it will show up very quickly. Of course, this also increases the load on the server. This function module is installed in the general server.

There are two obvious benefits of reducing file size, one is to reduce storage space, and the other is to reduce transmission time when transferring files over the network. Gzip is a frequently used Linux system command to compress and decompress files, which is both convenient and useful.

After Nginx installation is complete, we can turn on gzip compression function, here Nginx default only to text/html type of file compression.

The following instruction is to open the gzip command:

    #启用 gzip compression function    gzip on  ;        #默认值是1.1, which means that the request for http/1.1 protocol will be gzip compressed    gzip_http_version 1.1;    
#和http头有关系, add a vary head, to the proxy server, some browsers support compression, and some do not support. Therefore, to avoid wasting unsupported compression, it is necessary to determine whether compression is required based on the HTTP header of the client. gzip_vary on; # compression level, 1 compression than the minimum processing speed, 9 compression than the maximum but the slowest processing, but also the most consumption of CPU, generally set to 3 can be Gzip_comp_level 6; # When you do a front-end proxy, this option is enabled, which means that the compressed gzip_proxied any is unconditionally enabled regardless of the information returned by the backend server's headers header ; # What type of page or document is enabled for compression gzip_types text/plain text/css application/json application/x-javascript text/xml application/ XML Application/xml+rss text/javascript image/jpeg image/gif image/png; # The smallest compressed page, if the page is too small, it may be more pressure, there are more than 1K of the page to enable compression gzip_min_length 1024x768; # Set the system to get several units of cache for storing gzip compressed results data stream gzip_buffers 8k; # Disable IE6 gzip compression gzip_disable "MSIE [1-6]. (?!. *SV1) ";

Related Configuration Description:

1, the default value in Gzip_http_version is 1.1, that is, the request for http/1.1 protocol will be gzip compressed.

If Proxy_pass is used for reverse proxy, then the Nginx and backend upstream server is communicated with http/1.0 protocol, if using nginx through the reverse proxy cache server, the front-end Nginx does not open gzip, And the gzip_http_version on the back end of Nginx is not set to 1.0, then the cache URL will not be compressed by gzip.

2. gzip_disable settings are disabled for IE6 gzip compression

Some versions of IE6 's compression support for gzip is not very good and will cause the page to feign death. Gzip will cause IE6 's suspended animation after the IMG, and the gzip compression of IMG is removed and normal. To ensure that the other IE6 versions are not problematic, the gzip_disable configuration item is added.

3. Gzip_min_length sets the minimum number of bytes that are allowed to compress, and the number of page bytes is obtained from the content-length in header.

The default value is 0, regardless of the number of pages in most compression. It is recommended to set the number of bytes greater than 1k, which may be more or less larger than 1k. namely: Gzip_min_length 1024.

Example:

        gzip on;        Gzip_min_length         2k;        Gzip_buffers            4 16k;        Gzip_http_version       1.0;        Gzip_comp_level 6;        Gzip_types              text/plain application/x-javascript text/css application/xml;        Gzip_vary on               ;

Nginx uses gzip to compress Web pages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.