First of all, we first understand the relevant information of Apache Gzip.
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.
Second, Web server processing
The process of HTTP compression is as follows:
After the Web server receives the HTTP request from the browser, 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 request file;
If the request file is a static file such as HTML or CSS, the Web server checks the compressed buffer directory to see if the requested file has the latest compressed file;
If the compressed file of the request 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;
If the latest compressed file of the request file already exists, it returns the compressed file of the request file directly;
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.
Third, open gzip
There are two modules that Apache compresses using the gzip compression algorithm: Mod_gzip and Mod_deflate.
Now the browser itself also automatically gzip compression features, support accept-encoding:gzip,deflate, here I test in Firefox browser.
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
At the same time support Mod_gzip and mod_deflate Apache built-in mod_deflate modules to enable gzip, but if the installation of Apache does not compile the relevant modules, you need to manually install to enable it:
First to your Apache source directory, find mod_deflate.c files, usually location: apachehttpd source directory/modules/filters/mod_deflate.c, go to the directory found above 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 under the bin directory of the Apache installation directory.
Install complete, go to 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
Loading the mod_deflate.so module, the default installation is automatically written to httpd.conf.
If the server is enabled for the gzip component, then we can customize the file compression in http.conf, and here is a simple example of a configuration:
1, Mod_gzip way
Copy Code code as follows:
# 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.*
<ifModule>
2, Deflate_module Way
(1) strictly match file type
Copy Code code as follows:
# 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
</ifmodule>
(2) Filter file type
Copy Code code as follows:
# 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
</IfModule>
File MIME types can be added according to your own situation, or you can view Connect-type from the browser:
Iv. What are the main differences between Mod_gzip and mod_deflate? (from the Internet)
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.
Five, the effect
Gzip Compression not started:
To start gzip compression:
Size from 10.7K to 1.5K, if the file is larger, then the effect will be more obvious.