The Web server handles the gzip, deflate compression of HTTP compression

Source: Internet
Author: User

One, what is gzip         

gzip is a data format that, by default, compresses the data section using only the deflate algorithm;

Gzip is a popular file compression algorithm and is now widely used, especially on Linux platforms. When applying gzip compression to a plain text file, the effect is very noticeable and can reduce file size by more than 70%. This depends on the content in the file.

Using the GZIP module in Apache, we can use the GZIP compression algorithm to compress the Web content published by the Apache server before transferring it to the client browser. This compression actually reduces the number of bytes transmitted over the network, the most obvious benefit is that the speed of page loading can be accelerated.

The benefits of faster page loading are self-evident, in addition to saving traffic and improving the user's browsing experience, another potential benefit is that Gzip has a better relationship with search engine crawlers. For example, Google can retrieve pages faster than regular manual crawls by directly reading gzip files. As you can see in the Google Webmaster tool (Google Webmaster tools), sitemap.xml.gz is submitted directly as a sitemap.

While these benefits are not limited to static content, PHP dynamic pages and other dynamically generated content can be compressed by using the Apache compression module, plus other performance tuning mechanisms and the corresponding server-side caching rules, which can greatly improve the performance of the site. Therefore, for PHP programs that are deployed on Linux servers, we recommend that you turn on Use gzip Web compression when the server is supported.

PS: Detail Reference: Http://baike.baidu.com/item/gzip?fr=aladdin

second, what is deflate

deflate is a lossless data compression algorithm that uses both the LZ77 algorithm and Huffman coding (Huffman Coding).

It was originally defined by Phil Katz for his second edition of the PKZip Archive tool, which was later defined in the RFC 1951 specification.

It is generally accepted that deflate is not subject to any patents, and that the format is applied in the gzip compressed file as well as the PNG image file, in addition to being applied in the ZIP file format, prior to the invalidation of the patent associated with LZW (GIF file format).

Deflate compression and decompression of the source code can be found on the free, Universal compression library zlib.

The higher compression rate of the deflate is achieved by 7-zip. AdvanceCOMP also uses this implementation, which compresses gzip, PNG, MNG, and zip files to get smaller file sizes than zlib. In Ken Silverman's Kzip and PNGOut, a more efficient deflate program that requires more user input is used.

deflate is a compression algorithm, which is a kind of enhancement of Huffman coding.

Deflate is almost identical to the code that gzip extracts, and you can synthesize a piece of code.

third, the process of Web server processing HTTP compression

1. After the Web server receives the HTTP request from the browser, check whether the browser supports HTTP compression (accept-encoding information);

2. If the browser supports HTTP compression, the Web server checks the suffix name of the requested file;

3. If the request file is an HTML, CSS and other static files, the Web server to the compression buffer directory to check whether the requested file has the latest compressed file;

4. If the compressed file of the requested file does not exist, the Web server returns the uncompressed request file to the browser and stores the compressed file of the requested file in the compressed buffer directory;

5. If the most recent compressed file of the requested file already exists, the compressed file of the requested file is returned directly;

6. If the request file is a dynamic file, the Web server dynamically compresses the content and returns to the browser, and the compressed content is not stored in the compressed cache directory.

Here are two demo graphs:

Gzip Not used:

After opening using gzip:


iv. differences between gzip and deflate

deflate uses inflateinit (), while Gzip initializes with InflateInit2 (), one more parameter than Inflateinit ():-max_wbits, which represents processing raw deflate data. Because the zlib compressed data block in the GZIP data does not have a zlib header of two bytes. The Zlib library is required to ignore the zlib header when using InflateInit2. In the Zlib manual, Windowbits is required to be 8.        15, but in fact other ranges of data have a special effect, see Zlib.h in the comments, such as negative numbers indicate raw deflate.

Apache's deflate variant may also have no zlib header, which requires the addition of a dummy header after processing. That is, MS Error deflate (raw deflate). zlib header 1th Byte is generally 0x78, the 2nd byte and the first byte should be divisible by 31, see rfc1950. For example, Firefox's zlib dummy head for 0x7801,python zlib.compress () results in head 0x789c.

Deflate is the most basic algorithm, Gzip added 10 bytes of Gzheader in front of deflate raw data, the tail adds 8 bytes of check bytes (optional CRC32 and ADLER32) and length identification bytes.

Differences in the version of the Apache Web server to which they are installed. The Apache 1.x series does not have built-in Web page compression technology, so it uses additional third-party mod_gzip modules to perform compression. While the Apache 2.x official in the development of the time, the page compression into account, built a mod_deflate This module, to replace the mod_gzip. Although both are used by the GZIP compression algorithm, they operate in a similar principle.

Compression quality. Mod_deflate compression speed slightly faster and mod_gzip compression ratio is slightly higher. Generally, by default, Mod_gzip will be 4%~6% more compressed than mod_deflate.

The use of server resources. Generally speaking, Mod_gzip is a bit more expensive for server CPUs. Mod_deflate is a compression module specifically designed to ensure server performance, and mod_deflate requires less resources to compress files. This means that on high-traffic servers, using mod_deflate may load faster than Mod_gzip. That is, when server performance is sufficient, the use of mod_gzip, while consuming server performance, is worthwhile (faster compression and better), and using mod_deflate to ensure performance when server performance is low.

Starting with Apache 2.0.45, mod_deflate can use the Deflatecompressionlevel directive to set the compression level. The value of this instruction can be 1 (the fastest compression, lowest compression quality) to 9 (the slowest compression speed, the highest compression rate) between the integer, the default value is 6 (compression speed and compression quality is more balanced value). This simple change makes the mod_deflate easily comparable to Mod_gzip compression.

v. Opening mod_gzip and Mod_deflate

There are two types of modules that are compressed using the GZIP compression algorithm on Apache: Mod_gzip and Mod_deflate. To use gzip Web compression, first make sure that your server turns on support for one of these two components. On Linux servers, more and more space vendors are now opening up their support, and some even support these two modules at the same time.        For example, at present, GoDaddy, Bluehost and Dreamhosts, and other space quotient servers have been supporting both Mod_gzip and mod_deflate.

By looking at the HTTP headers, we can quickly determine whether the client browser that is being used supports accepting gzip compression. If the following message appears in the HTTP header that you send, your browser supports accepting the appropriate gzip compression:

Accept-encoding:gzip supports Mod_gzip accept-encoding:deflate support Mod_deflate accept-encoding:gzip,deflate while supporting Mod_gzip and Mod_deflate

Mod_deflate is Apache comes with the module, of course, after the Apache 2 support, the previous 1 is Mod_gzip, enable mod_deflate can be good to save the page size, It is only the resources and memory that occupy the server. Users can see the page speed much faster. gzip compression using the Mod_deflate module in editions above apache2.0 (including apache2.0)

1. Check the Apache installation mode
Apachectl-l

Discover that Mod_so.c,ok can dynamically add modules without recompiling.

2. Installing Mod_deflate
Locate the original Apache installation package installation Mod_deflate
CD Httpd-2.0.59/modules/filters
/usr/local/apache2/bin/apxs-i-c-a mod_deflate.c

PS:APXS Command parameter Description:
-I this option indicates the need to perform an installation operation to install one or more dynamic shared objects into the server's modules directory.
-A This option automatically adds a loadmodule line to the httpd.conf file to activate the module, or, if this line already exists, enable it.
-A is similar to the-a option, but its added loadmodule command has a pound prefix (#), which means that the module is ready but not yet enabled.
-C This option indicates the need to perform a compilation operation. It first compiles the C source program (. c) files for the corresponding target code file (. o), and then connects the target code with the rest of the target code files (. O and. A) in file, to generate the dynamic shared object Dsofile. If you do not specify the-o option, the output file name is inferred from the first file name in files, which is the default of mod_name.so.

3, modify the Apache http.conf file, remove the comments in front of mod_deflate.so

LoadModule Deflate_module modules/mod_deflate.so

4. Create a new. htaccess file in the root directory, custom compression rules

#GZIP压缩模块配置 <ifmodule mod_deflate.c> #启用对特定MIME类型内容的压缩SetOutputFilter deflatesetenvifnocase Request_uri. (?: GIF|JPE?G|PNG|EXE|T?GZ|ZIP|BZ2|SIT|RAR|PDF|MOV|AVI|MP3|MP4|RM) $ no-gzip dont-vary # Set files that are not compressed Addoutputfilterbytype DEFLATE text/html text/css text/plain text/xml application/x-httpd-php application/ X-javascript #设置对压缩的文件 </ifmodule>

5. Configure the cache lifetime for the specified file, removing the annotations in front of the mod_headers.so module

LoadModule Headers_module modules/mod_headers.so

6. Create a new. htaccess file in the root directory, custom compression rules

#文件缓存时间配置 <filesmatch ". (FLV|GIF|JPG|JPEG|PNG|ICO|SWF|JS|CSS) $ ">header set Cache-control" max-age=2592000 "</FilesMatch>

Inside the file MIME type can be added according to their own situation, as for the PDF, pictures, music documents, and so on itself are already highly compressed format, the role of repetitive compression, but may be due to increase CPU processing time and browser rendering problems to reduce performance. So there is no need to re-compress by gzip. With the above setting and then looking at the returned HTTP headers, the following information indicates that the returned data is already compressed. That is, the gzip compression configured by the Web site program is in effect.

Content-encoding:gzip

Note: The information returned here is the same regardless of whether you use Mod_gzip or mod_deflate. Because they are all implementations of gzip compression methods.


Problems encountered and resolved:

1:

APACH2 After installing mod_deflate restart, directly

load/opt/apache/modules/mod_deflate.so into server:/opt/apache/modules/mod_deflate.so:undefined symbol:deflate Abnormal pain.

What LDD mod_deflate.so after export Lib_library_path Ah, have tried n times, Google also go for n days

Finally on the Google go out an article, finally resolved, the method is as follows: Vi/usr/local/apache2/bin/apr-config modify ldflags= "" for ldflags= "-lz" and then Apxs-ica Mod_ The deflate.c is OK.

2:
APACH2 After installing mod_deflate restart, directly

Module Deflate_module is built-in and can ' t be loaded ...

This means that the module is already installed and no longer loadmodule deflate_module to enable it.

Just do <ifmodule mod_deflate.c> configuration

Reference:

http://qbaok.blog.163.com/blog/static/101292652008101431233385/

Http://blog.sina.com.cn/s/blog_a34721f00101f658.html

http://blog.csdn.net/zhangxinrun/article/details/5711307

Http://baike.baidu.com/view/4795073.htm?fr=aladdin

Http://baike.baidu.com/item/gzip?fr=aladdin

Http://www.cnblogs.com/linzhenjie/archive/2013/03/05/2943635.html

Http://www.cnblogs.com/zhangziqiu/archive/2009/05/17/gzip.html

The Web server handles the gzip, deflate compression of HTTP compression

Related Article

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.