Apache Enable Gzip Compression implementation method _linux

Source: Internet
Author: User

First, Gzip introduction

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

With the GZIP module in Apache, we can use the GZIP compression algorithm to compress the content of the Web page published by the Apache server and then transfer it to the client browser. This actually reduces the number of bytes transmitted by the network, and the most obvious benefit is that it can speed up the loading of the Web page.

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

These benefits are not limited to static content, PHP dynamic pages and other dynamically generated content can be compressed using the Apache compression module, plus other performance tuning mechanism and the corresponding server-side caching rules, which can greatly improve the performance of the site. Therefore, for PHP programs deployed on Linux servers, we recommend that you open the use of gzip web compression in case of server support.

second, the Web server processing HTTP compression process is as follows:

1. When the Web server receives the HTTP request from the browser, check to see if the browser supports HTTP compression (accept-encoding information);

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

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

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

5. If the latest compressed file of the request file already exists, then directly returns the compressed file of the request file;

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 diagrams:

No use of gzip:

After opening using gzip:

Third, the implementation

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 has enabled support for one of these two components. On Linux servers, more and more space vendors are now open to support, and some even support these two modules. For example, currently GoDaddy, Bluehost and dreamhosts and other space business servers have also supported the Mod_gzip and mod_deflate.

Although the use of Gzip also requires the support of the client browser, but do not worry, most browsers have already supported gzip, such as IE, Mozilla Firefox, Opera, Chrome and so on.

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

Accept-encoding:gzip Support Mod_gzip
Accept-encoding:deflate Support Mod_deflate

Accept-encoding:gzip,deflate also supports mod_gzip and mod_deflate gzip compression in apache2.0 above (including apache2.0) editions using the Mod_deflate module, The following is a detailed configuration step as follows:

1, modify Apache http.conf file, remove mod_deflate.so before the annotation

Copy Code code as follows:

LoadModule Deflate_module modules/mod_deflate.so

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

Copy Code code as follows:

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

3. Configure the cache lifetime for the specified file, and remove the comments in front of the mod_headers.so module

Copy Code code as follows:

LoadModule Headers_module modules/mod_headers.so

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

Copy Code code as follows:

#文件缓存时间配置
<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 PDFs, pictures, music documents, such as these are already highly compressed format, the role of repetitive compression is not large, but may be due to increase CPU processing time and browser rendering problems and reduce performance. So there's no need to compress it again through gzip. With the above settings and then view the returned HTTP headers, the following message indicates that the returned data has been compressed. 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 the implementation of the GZIP compression method.

In addition, there are some online check tool http://tool.chinaz.com/Gzips/to detect whether your site content has been gzip compression.

Iv. What are the main differences between Mod_gzip and mod_deflate? Which is better to use?

The first difference is the difference between installing their Apache Web server version. The Apache 1.x series does not have built-in web compression technology, so it uses an additional Third-party Mod_gzip module to perform compression. and the Apache 2.x official in the development, the Web page compression into account, built mod_deflate This module, to replace the mod_gzip. Although both are using the GZIP compression algorithm, they work in a similar principle.

The second difference is compression quality. Mod_deflate compression speed is slightly faster and mod_gzip compression ratio is slightly higher. Generally by default, Mod_gzip will have more 4%~6% compression than mod_deflate.

So, why use mod_deflate? The third difference is the occupation of server resources. Generally speaking, mod_gzip is a bit higher on the server CPU. Mod_deflate is a compression module specifically designed to ensure server performance, mod_deflate requires fewer resources to compress files. This means that in high traffic servers, using mod_deflate may be faster than mod_gzip loading.

Don't understand? In short, if your site, less than 1000 independent visitors a day, want to speed up the loading speed of the Web page, use Mod_gzip. Although it will cost some additional server resources, it is worthwhile. If your site has more than 1000 independent visitors per day, and you are using a shared virtual host and have limited resources allocated to your system, using mod_deflate will be a better choice.

In addition, starting with the Apache 2.0.45, mod_deflate can use the Deflatecompressionlevel directive to set the compression level. The directive can be a value of 1 (the fastest compression, the lowest compression quality) to 9 (the slowest compression speed, the highest compression rate) between the integer, its default value is 6 (compression speed and compression quality more balanced value). This simple change makes mod_deflate easier to compare with mod_gzip compression.

P.S. For not enabling the above two gzip modules of virtual space, you can also retire to the second use of PHP's Zlib function library (also need to see whether the server is supported) to compress files, but this method is more cumbersome to use, and generally will be more time-consuming server resources, please use carefully according to the situation. Detailed PHP enable zlib compressed files

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.