How to transfer a GZIP compressed webpage using mod_deflate in apache

Source: Internet
Author: User

This article introduces how to use apache to enable mod_deflate's GZIP compression for webpage transmission when the server bandwidth is insufficient. In this way, the compression ratio of webpages and images is about 85%, you can imagine the bandwidth saved in this way.

The original gzipformat is a compressed file format with the extension name .gz. HTTP uses it to improve the WEB application performance, accelerate the download speed of HTTP request returned content, increase user experience, and reduce network bandwidth usage
Apache has a built-in mod_deflate module to enable the gzip function. However, if the relevant modules are not compiled during apache installation, You need to manually install the module to enable it:
First, go to your apache source code directory and find the mod_deflate.c file.
Locate mod_deflate.c is available for Mac and Linux.
Location: apachehttpd source code directory/modules/filters/mod_deflate.c
Go to the directory found above and run the following command:
/Usr/local/apache/bin/apxs-I-c-a mod_deflate.c
NOTE: For the apxs directory, refer to your own machine, usually in the bin directory of the apache installation directory.
After the installation is complete, go to the modules directory of apache to see if mod_deflates.so is available. If you have this file, edit the conf/httpd. conf configuration file in the apache installation directory:
LoadModule deflate_module modules/mod_deflate.so
Load mod_deflate.so Module
Add the following content to the site configuration in httpd. conf:
AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript
DeflateCompressionLevel 9
SetOutputFilter DEFLATE
The first line indicates what type of content to enable GZIP compression, the second line indicates the compression level, and the third line indicates that the deflate module is enabled to perform GZIP compression on the output of the site (that is, compression is enabled for all outputs)
OK, pull it. After restarting apache httpd, you can search for some gzip detection sites for the effect. Generally, the compression ratio exceeds % 50, which can effectively increase the access speed and site bandwidth utilization. Good Luck!
Possible problems:
Problem: After apache is started, mod_deflate.so: undefined symbol: inflateEnd appears in the log.
Solution: Add LoadFile/usr/local/lib/libz. so or LoadFile/usr/lib/libz. so before LoadModule deflate_module modules/mod_deflate.so.
Note: Find the libz. so location.

 

Mod_deflate is the standard filter module in Apache2. The function of this module is the same as that of mod_gzip: compress various web page components to speed up web page download.

Generally, you do not need to recompile Apache when adding modules in Apache2. You only need to dynamically Add the required modules. First, find the mod_deflate.c file in the modules folder of the Apache source installation folder ......

In Apache2.2.2, The mod_deflate module is in the modules/filters folder:

The Code is as follows: Copy code

[Root @ localhost modules] # cd/apache-source-install-path/modules/filters

[Root @ localhost filters] #/apache-installed-path/bin/apxs-I-a-c mod_deflate.c

If the installation is successful, the following message is displayed:

Chmod 755/apache-installed-path/modules/mod_deflate.so
[Activating module 'release' in/apache-installed-path/conf/httpd. conf]

Open the/apache-installed-path/conf/httpd. conf file and search for "mod_deflate". As you can see, this module has been loaded, as shown below:

LoadModule deflate_module modules/mod_deflate.so

Since the module is loaded, you can compress the required content. The apache official website provides detailed mod_deflate configuration instructions. The following is my mod_deflate configuration for the test Website:

The Code is as follows: Copy code

<IfModule mod_deflate.c>
DeflateCompressionLevel 9 # Set the compression level to the highest

# Compressing html, text, xml, and php
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php

# Compressing html, htm, xml, php, css, and js
AddOutputFilter DEFLATE html htm xml php css js
</IfModule>

The mod_deflate compression module added to apache2 is OK. Of course, after making the above changes to httpd. conf, you should save the file and restart the apache service. In this case, check whether the page is compressed by using http. The following figure shows the effects of mod_deflate compression on the test website of Northwestern University:


Mod_deflate compression result

Experiments show that the mod_deflate module of Apache can be used to compress webpage content and reduce the webpage size to about 60% on average. Especially for non-image and non-multimedia websites, the effect is more obvious.

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.