Apache web page gzip compression enabling and rules

Source: Internet
Author: User

Enable the deflate_Module and headers_Module modules in httpd. conf.

Remove the preceding two sentences:

LoadModule deflate_module modules/mod_deflate.so

LoadModule headers_module modules/mod_headers.so

Add the following code at the bottom of the httpd. conf file to configure the file to be compressed:

<IfModule deflate_module>
SetOutputFilter DEFLATE
# Don't compress images and other
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
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

Check whether Gzip is enabled

Apache uses the Gzip compression algorithm to compress modules: mod_gzip and mod_deflate.

Now the browser also supports the automatic Gzip compression function, which supports Accept-Encoding: gzip and deflate. Here I will test it in the firefox browser.
By viewing the HTTP header, we can quickly determine whether the client browser can accept gzip compression.

If the server enables support for Gzip components, you can customize them in http. conf or. htaccess. Below is a simple example of. htaccess configuration:

The code is as follows: Copy code

# 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>
# Mod_deflate:
<Ifmodule mod_deflate.c>
DeflateCompressionLevel 6 # compression ratio, 6 is the recommended value.
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
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
</Ifmodule>

The file MIME type can be added according to your own situation. As for PDF files, images, music files, and other files, these files are already highly compressed, and repeated compression does not play a major role, however, the performance may be reduced by increasing the CPU processing time and rendering of the browser. Therefore, it is unnecessary to use Gzip compression. After the preceding settings, check the returned HTTP header. If the following information is displayed, the returned data has been compressed. That is, the Gzip compression configured by the website program takes effect.

Content-Encoding: gzip

Note: No matter whether mod_gzip or mod_deflate is used, the information returned here is the same. Because they are all implemented gzip compression methods.

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.