Enable gzip compression on IIS (HTTP compression)

Source: Internet
Author: User
Tags metabase

I. Summary

This article summarizes how to enable gzip compression for Web sites that are hosted by IIS, reducing the size of the Web network transfer and increasing the speed at which users display pages.

Two. Preface.

The knowledge points of this paper are collected from the Internet, mainly from the Chinese wiki. Gzip is a key item when using YSlow to detect which optimizations are enabled on the site. Starting GIP compression will instantly reduce the size of the page's network transfer.

Three. HTTP Compression overview

HTTP compression is a way to transfer compressed text content between a Web server and a browser. HTTP compression uses a common compression algorithm such as gzip to compress HTML, JavaScript, or CSS files. The greatest benefit of compression is to reduce the amount of data transmitted over the network, thereby increasing the access speed of the client browser. Of course, it will also increase the burden of a little server. Gzip is a more common type of HTTP compression algorithm.

Four. How HTTP Compression works

How the Web server handles HTTP compression works as follows:

After the 1.WEB server receives the HTTP request from the browser, check whether the browser supports HTTP compression;
In the HTTP header where the user's browser sends the request, the "accept-encoding:gzip, deflate" parameter indicates that two compression algorithms are supported for gzip and deflate.

2. If the browser supports HTTP compression, the Web server checks the suffix name of the requested file;
Static files and dynamic file suffixes are all required to be set up in MetaBase.xml.
Static files need to be set: HcFileExtensions Metabase Property (click to jump to MSDN instructions)
Dynamic files need to be set: HcScriptFileExtensions Metabase Property (click to jump to MSDN instructions)

3. If the request file is a static file such as HTML, CSS, and the file suffix is compressed, the Web server checks to the compression buffer directory to see if the latest compressed file of the requested file already exists;

4. If the compressed file of the requested file does not exist, the Web server returns the uncompressed request file to the browser and stores the compressed file of the requested file in the compressed buffer directory;

5. If the most recent compressed file of the requested file already exists, the compressed file of the requested file is returned directly;

6. If the request file is a dynamic file such as aspx and the file suffix is enabled for compression, the Web server dynamically compresses the content and returns to the browser, and the compressed content is not stored in the compressed cache directory.

Five. Enable HTTP compression in IIS

IIS does not support HTTP compression by default and requires a simple configuration

1. Open Internet Information Services (IIS) Manager, right-click Web site, properties, and select Services. Select "Compress application Files" and "Compress static files" in the "HTTP Compression" box, set "Temp directory" and "Maximum limit of temporary directory" as required;

2. Warning: After testing this step in my machine has no effect, can be ignored.  
in Internet Information Services (IIS) Manager, right-click Web Service Extensions, Add a new Web service extension ... ", enter the extension" HTTP Compression "in the" new Web service Extension "box, adding" required files "to C:\WINDOWS\system32\inetsrv\ Gzip.dll, where the Windows system directory may differ depending on your installation, select "Set extended status to allow";  
 

3. Open the C:\Windows\System32\inetsrv\MetaBase.xml with a text editor (it is recommended to back up first),
Locate location = "/lm/w3svc/filters/compression/gzip is used to set gzip compression,
Locate location = "/lm/w3svc/filters/compression/deflate" to set deflate compression.
The top two nodes are next to each other. and set the same properties.

If you need to compress the dynamic file, set HcDoDynamicCompression to "TRUE" and add the dynamic file suffix name, such as ASPX, that you want to compress in hcscriptfileextensions, if you need to compress the static file, Set HcDoStaticCompression and HcDoOnDemandCompression to "TRUE" and add the static file suffix names that you need to compress in hcfileextensions, such as XML, CSS, etc. , HcDynamicCompressionLevel and HcOnDemandCompLevel indicate the required compression rate, the value is 0-10, and the default is 0.
HcDynamicCompressionLevel attribute Description: HcDynamicCompressionLevel Metabase Property
HcOnDemandCompLevel attribute Description: HcOnDemandCompLevel Metabase Property

Note: These two property values are generally recommended to be set to 9, with the best price/performance ratio. But on my Windows Server 2003, the compression rate is set anyway, and the size of jquery and jquery ui two files (58k/188k) is always the same. (20k/45k).

Here is my example:

<IIsCompressionSchemeLocation= "/lm/w3svc/filters/compression/deflate"Hccompressiondll= "%windir%\system32\inetsrv\gzip.dll"HcCreateFlags= "0"HcDoDynamicCompression= "TRUE"HcDoOnDemandCompression= "TRUE"HcDoStaticCompression= "true"HcDynamicCompressionLevel= "9"HcFileExtensions= "htm html txt js css swf xml"HcOnDemandCompLevel= "9"HcPriority= "1"HcScriptFileExtensions= "ASP
aspx dll EXE "></IIsCompressionScheme><IIsCompressionSchemeLocation= "/lm/w3svc/filters/compression/gzip"hccompressiondll= "%windir%\system32\inetsrv\gzip.dll" Hccreateflags= "1" hcdodynamiccompression= "TRUE" hcdoondemandcompression= "TRUE" hcdostaticcompression= "true" hcdynamiccompressionlevel= "9" Hcfileextensions= "htm html txt js css swf xml" hcondemandcomplevel=" 9 "hcpriority=" 1 "hcscriptfileextensions= "ASP
aspx dll exe" ></ Iiscompressionscheme>

4. Save the MetaBase.xml file after editing, or the file may be in use by IIS if the file cannot be saved. Open "Start", "Administrative Tools", "services", after stopping "IIS Admin service", you can save;

5. Finally, restart IIS. You can verify the results to the HTTP Compression test Web site. In jquery, for example, the core class library and UI class library original sizes are 57k and 188k, respectively, 20k and 45k after compression:

We determine that the returned data has been enabled for gzip compression by using the: content-encoding:gzip Property in the HTTP header:

With YSlow detection, when only static file compression is initiated, the GZIP compression rating is B:

When dynamic file compression is started at the same time, gzip compression is rated as a:

Six. Summary of Points

1. When modifying the MetaBase.xml file, stop the IIS Admin service services. Otherwise, it cannot be saved.

2. Static compression and dynamic compression rates are best set to 9.

3. In step 2 above, the effect is the same even if you do not add a Web service extension.

4. The compression rate setting does not work for JS files and is always the same size after compression.

5. The picture file does not change even if gzip compression size is enabled.

Seven. Summary

This article summarizes how to enable gzip compression in IIS. Finishing this article is mainly to organize their knowledge so that later forget the review.

Enable gzip compression on IIS (HTTP compression)

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.