How to enable http compression (Gzip) in IIS

Source: Internet
Author: User
Tags metabase

1. HTTP compression Overview

HTTP compression is used to transfer compressed text content between Web servers and browsers. HTTP compression uses common compression algorithms such as gzip to compress HTML, JavaScript, or CSS files. The biggest advantage of compression is that it reduces the amount of data transmitted over the network and increases the access speed of the client browser. Of course, it also adds a little load on the server. Gzip is a common HTTP compression algorithm.

Ii. How HTTP compression works

How the Web server processes HTTP compression is as follows:

1. After receiving the HTTP request from the browser, the Web server checks whether the browser supports HTTP compression;
The "Accept-Encoding: gzip, deflate" parameter in the HTTP header of the request sent by the browser indicates that the gzip and deflate compression algorithms are supported.

2. If the browser supports HTTP compression, the Web server checks the suffix of the request file;
To start static files and dynamic file suffixes, you must set them in MetaBase. xml.
Static files need to be set: HcFileExtensions Metabase Property (click to jump to the MSDN instructions)
Dynamic file needs to be set: HcScriptFileExtensions Metabase Property (click to jump to the MSDN instructions)

3. If the requested file is a static file such as HTML and CSS, and the file suffix is compressed, the Web server will check whether the latest compressed file of the requested file already exists in the compression buffer directory;

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

5. If the latest compressed file of the request file already exists, the compressed file of the request file will be directly returned;

6. If the requested file is a dynamic file such as ASPX and the file suffix is compressed, the Web server dynamically compresses the content and returns it to the browser. The compressed content is not stored in the compressed cache directory.

3. enable HTTP compression in IIS

By default, IIS does not support HTTP compression and requires simple configuration.

1. Open the Internet Information Service (IIS) manager, right-click "website"-> "properties", and select "service ". In the "HTTP compression" box, select "compressing Application Files" and "compressing static files", and set "temporary directory" and "maximum temporary directory limit" as needed ";

2. Reminder: This step has no function on my machine and can be ignored.
In the Internet Information Service (IIS) manager, right-click "Web Service extension"-> "to add a new Web Service extension... ", enter the extension" HTTP Compression "in the" New Web service extension "box, and add" required file "to C: \ WINDOWS \ system32 \ inetsrv \ gzip. dll, the Windows System directory may vary depending on your installation, select "set extension status to allow ";

3. Open C: \ Windows \ System32 \ inetsrv \ MetaBase. xml in a text editor (backup is recommended ),
Locate Location = "/LM/W3SVC/Filters/Compression/gzip to set gzip Compression,
Locate Location = "/LM/W3SVC/Filters/Compression/deflate" to set deflate Compression.
The above two nodes are next to each other and the Set attributes are the same.

To Compress dynamic files, set HcDoDynamicCompression to "TRUE" and add the dynamic file suffix, such as aspx, to HcScriptFileExtensions. If you need to compress static files, set HcDoStaticCompression and HcDoOnDemandCompression to "TRUE", and add the static file suffix to HcFileExtensions, such as xml and css. HcDynamicCompressionLevel and HcOnDemandCompLevel indicate the compression ratio, the value ranges from 0 to 10. The default value is 0.
HcDynamicCompressionLevel attribute description: HcDynamicCompressionLevel Metabase Property
HcOnDemandCompLevel attribute description: HcOnDemandCompLevel Metabase Property

Note: These two attribute values are generally recommended to be set to 9, with the best cost performance. however, in my window server 2003, the compression ratio is set no matter how, and the size of the files jQuery and jQuery UI (58 k/188 k) is the same after compression. (20 k/45 k ).

Here is my instance:

<IIsCompressionScheme    Location ="/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> <IIsCompressionScheme    Location ="/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. If the file cannot be saved, IIS may be using the file. Open "start"-> "Administrative Tools"-> "Service", stop "IIS Admin Service", and save the settings;

 

5. Finally, restart IIS. You can go to the HTTP compression test site to verify the results. Taking jQuery as an example, the original size of the core class library and UI class library is 57k and 188 k respectively, and the compressed size is 20 k and 45 k respectively:

We can use the Content-Encoding: gzip attribute in the Http header to determine that the returned data has enabled gzip compression:

Use YSlow for detection. When only static File compression is started, Gzip compression is rated B:
 
When dynamic File compression is enabled at the same time, Gzip compression is rated as:

Iv. Summary

1. Stop the "IIS Admin Service" Service when modifying the MetaBase. xml file. Otherwise, the Service cannot be saved.

2. It is best to set static compression and dynamic compression ratio to 9.

3. In Step 2 above, the extension effect is the same even if no Web Service is added.

4. The compression ratio setting does not work for js files, and the size is always the same after compression.

5. The image file size remains unchanged even if gzip compression is enabled.

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.