Apache enable gzip Compression Web page transfer method

Source: Internet
Author: User
Tags bz2 rar

First let us know about the Apache gzip data.

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.

Second, the Web server processing

The process of HTTP compression is as follows:

After the Web server receives the browser's HTTP request, check to see if the browser supports HTTP compression (accept-encoding information);

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

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;

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;

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

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

Third, open gzip

Apache uses the GZIP compression algorithm to compress modules in two ways: Mod_gzip and mod_deflate.

Now the browser itself is also automatic gzip compression feature, support accept-encoding:gzip,deflate, here I test under the Firefox browser.

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 Support Mod_gzip

Accept-encoding:deflate Support Mod_deflate

Accept-encoding:gzip,deflate

Mod_gzip and Mod_deflate Apache are also supported with the Mod_deflate module to enable gzip, but if you do not compile the relevant module when you install Apache, you will need to install it manually to enable it:

First go to your Apache source directory, find the mod_deflate.c file, the usual location: apachehttpd source directory/modules/filters/mod_deflate.c, go to the directory found above to run the following command:

/usr/local/apache2/bin/apxs-i-c-a mod_deflate.c

Note: Please refer to your own machine in the APXS directory, usually in the bin directory of the Apache installation directory.

The installation is complete, to the Apache modules directory to see if there is a mod_deflates.so,httpd.conf in the open Deflate_module and Headers_module modules:

LoadModule Deflate_module modules/mod_deflate.so

The mod_deflate.so module is loaded and the default installation is automatically written to httpd.conf.

If the server has support for gzip components, then we can customize the file compression in http.conf, here is a simple example of a configuration:

1, Mod_gzip Way

# Mod_gzip

<ifmodule mod_gzip.c>

mod_gzip_on Yes

Mod_gzip_dechunk Yes

Mod_gzip_item_include file \. (html?| TXT|CSS|JS|PHP|PL) $

Mod_gzip_item_include Handler ^cgi-script$

Mod_gzip_item_include MIME ^text/.*

Mod_gzip_item_include MIME ^application/x-javascript.*

Mod_gzip_item_exclude Rspheader ^content-encoding:.*gzip.*

2, Deflate_module Way

(1) strictly match file type

# Mod_deflate:

<ifmodule mod_deflate.c>

Deflatecompressionlevel 6 #压缩率, 6 is the recommended value.

Addoutputfilterbytype DEFLATE Text/plain

Addoutputfilterbytype DEFLATE text/html

Addoutputfilterbytype DEFLATE text/php

Addoutputfilterbytype DEFLATE Text/xml

Addoutputfilterbytype DEFLATE Text/css

Addoutputfilterbytype DEFLATE Text/javascript

Addoutputfilterbytype DEFLATE Application/xhtml+xml

Addoutputfilterbytype DEFLATE Application/xml

Addoutputfilterbytype DEFLATE Application/rss+xml

Addoutputfilterbytype DEFLATE Application/atom_xml

Addoutputfilterbytype DEFLATE Application/x-javascript

Addoutputfilterbytype DEFLATE application/x-httpd-php

Addoutputfilterbytype DEFLATE Image/svg+xml

Addoutputfilterbytype DEFLATE image/gif image/png image/jpe image/swf image/jpeg image/bmp

# Don ' t compress images and other #排除不需要压缩的文件

Browsermatch ^MOZILLA/4 gzip-only-text/html

Browsermatch ^mozilla/4\.0[678] No-gzip

Browsermatch \bmsie!no-gzip!gzip-only-text/html

Setenvifnocase Request_uri. (?: html|htm) $ no-gzip dont-varysetenvifnocase

#SetEnvIfNoCase Request_uri. (?: gif|jpe?g|png) $ no-gzip dont-vary

Setenvifnocase Request_uri. (?: Exe|t?gz|zip|bz2|sit|rar) $ no-gzip dont-vary

Setenvifnocase Request_uri. (?:p df|doc) $ no-gzip dont-vary

(2) Filter file type

# Mod_deflate:

<ifmodule mod_deflate.c>

Deflatecompressionlevel 6

Setoutputfilter DEFLATE #压缩所有文件

#Don ' t compress images and other #过滤不需要压缩文件

#SetEnvIfNoCase Request_uri. (?: gif|jpe?g|png) $ no-gzip dont-vary

Setenvifnocase Request_uri. (?: html|htm) $ no-gzip dont-vary

Setenvifnocase Request_uri. (?: Exe|t?gz|zip|bz2|sit|rar) $ no-gzip dont-vary

Setenvifnocase Request_uri. (?:p df|doc) $ no-gzip dont-vary

The file MIME type can be added according to your own situation, or you can view Connect-type by browser:

Iv. What are the main differences between Mod_gzip and mod_deflate? (from the Internet)

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.

Five, the effect

Gzip Compression not started:

To start gzip compression:

Size from 10.7K to 1.5K, if the file is large, then the effect will be more obvious.

Apache enable gzip Compression Web page transfer method

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.