Apache opens the GZIP compression webpage

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

First we read the related data in Apache Gzip.

A, gzip

Gzip is a popular file compression algorithm, Now the widely used, Especially in the Linux platform. When using the Gzip compression to a plain text file, The effect is very obvious, Some can reduce more than 70% of the size of the file. It depends on the contents of the file. Using the Gzip module in Apache, Webpage content we can use the Gzip compression algorithm to compress the Apache server and then transferred to the client browser. So after the compressed actually reduces the number of bytes of network transmission, The most obvious advantage is that can speed up the webpage loading speed.

Webpage loading speed benefits it is self-evident, in addition to save traffic, improve the user's browsing experience, another potential advantage is that Gzip and search engine crawlers have a better relationship.

Two, the Web server processes

Process of HTTP compression.:

The Web server receives an HTTP request browser, check if the browser supports HTTP compression (Accept-Encoding information),

If the browser supports HTTP compression, Web server check request file name suffix,

If the requested file is HTML, CSS, static files, Web servers to compress new compressed file buffer directory checks whether the requested file already exists,

If the compressed file request file does not exist, the Web server to the browser returns request file is not compressed, and storing the compressed file request file in directory compression buffer,

If the new compressed file request file already exists, it returns the requested file directly compressed file,

If the requested file is a dynamic document, dynamic Web server compressed content and is returned to the browser, compressed content is not stored in the compressed cache directory.

Three, open the Gzip

Apache uses Gzip to compress the module has two types: mod_gzip and mod_deflate.

Now the browser itself automatically Gzip compression function, support Accept-Encoding: gzip, deflate, here I test in Firefox browser.  

By looking at the head of the HTTP, we can quickly determine the use of the client if the browser supports gzip compression.

If the following information transmitted in HTTP head, is that your browser supports received the corresponding gzip compression:

Accept-Encoding: gzip support mod_gzip

Accept-Encoding: deflate support mod_deflate

Accept-Encoding: gzip,deflate

Supports both mod_gzip and mod_deflate Apache has built-in mod_deflate module to enable the gzip function, but if you install Apache not compiled modules, we need a time you install manually, to enable it:

First of all to your Apache source code directory, find mod_deflate.c file, usually location: apachehttpd source code directory /modules/filters/mod_deflate.c, run directory below into the top to find the command:

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

Note: please refer to the apxs directory of your own machine, usually in the Apache installation directory under the bin directory.

The installation is complete, to the Apache modules directory is it right? Have a look with mod_deflates.so, open deflate_Module and headers_Module module in httpd.conf:

LoadModule deflate_module modules/mod_deflate.so

Load the mod_deflate.so module, the default installation will be automatically written to the httpd.conf.

If the server is opened on the support of Gzip component, so that we can be compressed in the http.conf customization file, here is a simple example of configuration:


# 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.*



(1)Strict matching file types

# mod_deflate:

<ifmodule mod_deflate.c>

DeflateCompressionLevel 6 #Compression ratio, 6 is recommended.

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  #Exclude unwanted compressed files

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 .(?:pdf|doc)$ no-gzip dont-vary


(2)The filter file types

# mod_deflate:

<ifmodule mod_deflate.c>

DeflateCompressionLevel 6

SetOutputFilter DEFLATE    #All file compression

#Don’t compress images and other  #The filter does not need the compressed file

#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 .(?:pdf|doc)$ no-gzip dont-vary


MIME file types can be added according to their own situation, you can also view the connect-type through the browser:

Four, what is the main difference between mod_gzip and mod_deflate? (from the Internet)

The first difference is the difference between Apache Web server version of the installation of their. Apache 1.x has no built-in webpage compression technology, So just to perform compression with third additional mod_gzip modules. Apache 2.x official in the development of, The webpage compression into account, Built in mod_deflate module, Replace mod_gzip with. Although both use Gzip compression algorithm, The operation principle of them is similar. The second difference is the compression quality. Mod_deflate compression speed slightly faster and the mod_gzip high compression ratio. General by default, Mod_gzip will compress 4%~6% more than mod_deflate. So, Why use mod_deflate? The third difference is the occupancy of server resources. In general mod_gzip on server CPU occupied higher. Mod_deflate is designed to ensure that the server performance and the use of a compression module, Mod_deflate requires fewer resources to the compressed file. This means that in high traffic server, Using mod_deflate can be faster than mod_gzip loading speed.   

Five, effect

Did not start Gzip compression:

Start the Gzip compression:

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Jacqueline at October 23, 2013 - 7:44 PM