To enable gzip compression static content on the Apache server _linux

Source: Internet
Author: User

In order to optimize the access speed of the website, we can compress the static content so as to reduce the time of loading the Web page and save the bandwidth of the user greatly. In this article, I'll explain how to use Apache and. htaccess files for static content compression.

Let me first introduce that we can compress content using two different methods: GZip and deflate.

Introduced

The Gzip method was used in earlier versions of Apache (before Apache 1.3). But after that, Apache introduced the Deflate method, which is not much more effective than gzip (but still very good). However, Gzip no longer provides more support after Apache 1.3. Therefore, your Apache version must be greater than 1.3, and if not, you must upgrade to the latest version of Apache.

Before using compression, you must enable the Apache Mod_deflate module. To enable this module, you only need to remove the module line from the httpd.conf file.

When this module is enabled, your server is ready to provide compressed content. However, the server creates the compressed content only if it receives the corresponding header file from the client. So, now you need to put the following code into your site's htaccess file to inform the server to provide compressed content.

. Htaccess code

<ifmodule mod_deflate.c= "" > # force deflate for mangled headers # Developer.yahoo.com/blogs/ydn/posts/2010/12/pus hing-beyond-gzipping/<ifmodule mod_setenvif.c= "" > <ifmodule mod_headers.c= "" > Setenvifnocase ^ (Accept-E Ncodxng| X-cept-encoding| X{15}|~{15}|-{15}) $ ^ ((gzip|deflate) \s*,?\s*) +| [x~-] {4,13}$ have_accept-encoding requestheader append accept-encoding "gzip,deflate" env=have_accept-encoding </ifmodu Le> </ifmodule> # HTML, TXT, CSS, JavaScript, JSON, XML, HTC: <ifmodule filter_module= "" > Filterdecl are COMPRESS filterprovider COMPRESS DEFLATE resp=content-type $text/html filterprovider COMPRESS DEFLATE Resp=conten T-type $text/css filterprovider COMPRESS DEFLATE resp=content-type $text/plain filterprovider COMPRESS DEFLATE Resp=co Ntent-type $text/xml filterprovider COMPRESS DEFLATE resp=content-type $text/x-component filterprovider COMPRESS-Defla TE resp=content-type $application/javascript filterprovider COMPRESS DEFLATE resp=content-type $application/json filterprovider COMPRESS DEFLATE resp=content-type $application/xml Filter Provider COMPRESS DEFLATE resp=content-type $application/xhtml+xml filterprovider COMPRESS DEFLATE resp=content-type $a Pplication/rss+xml filterprovider COMPRESS DEFLATE resp=content-type $application/atom+xml filterprovider COMPRESS DEF LATE resp=content-type $application/vnd.ms-fontobject filterprovider COMPRESS DEFLATE resp=content-type $image/svg+ XML Filterprovider COMPRESS DEFLATE resp=content-type $application/x-font-ttf filterprovider COMPRESS DEFLATE Resp=con Tent-type $font/opentype filterchain COMPRESS filterprotocol COMPRESS DEFLATE change=yes;byteranges=no </ifmodul e> <ifmodule!mod_filter.c= "" > # Legacy versions of Apache addoutputfilterbytype DEFLATE text/html-TEXT/PL Ain text/css Application/json addoutputfilterbytype DEFLATE application/javascript addoutputfilterbytype DEFLATE text/ XML Application/xml text/x-comPonent addoutputfilterbytype DEFLATE application/xhtml+xml application/rss+xml addoutputfilterbytype DEFLATE applicat  Ion/atom+xml addoutputfilterbytype DEFLATE image/svg+xml application/vnd.ms-fontobject AddOutputFilterByType
 Application/x-font-ttf Font/opentype </ifmodule> </ifmodule>

After placing the above code in your htaccess file, look at the request head of your site. You can see an extra head "accept-encoding". This means that the requesting client can handle the contents of the given compression type and will provide the compressed content.

Accept-encoding:gzip,deflate,sdch

Results

Look at the picture below, how much has been compressed.

From the above picture can be seen, the actual page size of 707KB, using compression is 401KB. As a result, it will eventually improve the performance of your site.

I strongly recommend that you compress the static content of the site because there is no reason not to do so, which is a best practice for web development.

Then attach some of the basic common commands of gzip:

1. Command format:

gzip[parameters [file or directory]

2. Command function:

Gzip is a widely used compression program, the file after it compressed, after its name will be more than ". gz" extension.

3. Command parameters:

-A or--ASCII uses ASCII text mode.

-C or--stdout or--to-stdout the compressed file output to the standard output device, do not change the original file.

-D or--decompress or----uncompress unzip the compressed file.

-F or--force to forcibly compress the file. Ignores the existence of file names or hard connections and whether the file is a symbolic connection.

-H or--help online Help.

-L or--list lists information about compressed files.

-L or--license displays version and copyright information.

-N or--no-name compressed files, the original file name and time stamp are not saved.

-N or--name, save the original file name and time stamp when compressing the file.

-Q or--quiet does not display a warning message.

-R or--recursive recursively, all files and subdirectories under the specified directory are processed together.

-s< compressed Word tail string > or----suffix< compressed word tail string > Change compressed Word tail string.

-T or--test test that the compressed file is correct.

The-V or--verbose displays the instruction execution process.

-V or--version display version information.

-num adjusts the speed of the compression with the specified number num,-1 or--fast represents the fastest compression method (low compression ratio),-9 or--best represents the slowest compression method (high compression ratio). The default value for the system is 6.


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.