Apache Enable HTTP Compression

Source: Internet
Author: User

First, Gzip introduction

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.

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

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:

Three , 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 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.

Although the use of Gzip also requires client browser support, but do not worry, most of the current browser has been 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 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_gzipaccept-encoding:deflateaccept-encoding:gzip,deflate Supports both Mod_gzip and mod_deflate

gzip compression using the Mod_deflate module in the version above apache2.0 (including apache2.0), the following are the specific configuration steps as follows:

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

LoadModule Deflate_module modules/mod_deflate.so

2. 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>

3. 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

4. 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.

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

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

The first difference is the difference in the version of the Apache Web server where 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.

The second difference is the 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.

So, why use mod_deflate? The third difference is 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.

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

Also, 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.

P.S. For virtual spaces that do not have the above two gzip modules enabled, you can also fallback to use PHP's Zlib function library (also need to see if the server supports) to compress files, but this method is cumbersome to use, and generally consumes server resources, please use it as appropriate. Verbose PHP enable zlib compressed files

Apache Enable HTTP Compression

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.