Enable Gzip compression (HTTP compression) on IIS.

  

I. Summary

This article summarizes how to enable Gzip compression for websites hosted with IIS, thereby reducing the size of web page transmission and increasing the speed of users displaying pages. .

II. Foreword.

The knowledge of this article is collected from the Internet, mainly from the Chinese wiki. When using YSlow to detect which optimizations are enabled, Gzip is a key item. Starting Gip compression will immediately reduce the network transfer size of the page.

III. HTTP compression overview

HTTP compression is a method of transferring compressed text content between a web server and a browser. HTTP compression compresses HTML, JavaScript, or CSS files using common compression algorithms such as gzip. The biggest benefit of compression is to reduce the amount of data transmitted by the network, thereby increasing the access speed of the client browser. Of course, it will also increase the burden on the server. Gzip is a relatively common HTTP compression algorithm.

IV. HTTP compression working principle

Web server processing HTTP compression works as follows:

1. After the web server receives the browser's HTTP request, check the browser. Whether to support HTTP compression;
In the HTTP header sent by the user's browser, the "Accept-Encoding: gzip, deflate" parameter indicates that both gzip and deflate compression algorithms are supported.

2. If the browser supports HTTP compression, the web server checks the suffix name of the request file;
static files and dynamic file suffix startup settings need to be set in MetaBase.xml.
static files need to be set: HcFileExtensions Metabase Property (single Click to jump to MSDN instructions)
Dynamic files need to be set: HcScriptFileExtensions Metabase Property (Click to jump to MSDN instructions)

3. If the requested file is a static file such as HTML, CSS, and the file suffix is ​​enabled Compression, the web server checks the compressed cache 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 an uncompressed request file to the browser. And storing the compressed file of the requested file in the compressed buffer directory;

5. If the latest compressed file of the requested file already exists, directly return the compressed file of the requested file;

6. 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 it to the browser, and the compressed content is not stored in the compressed cache directory.


V. Enable HTTP compression in IIS

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

1. Open Internet information Service (IIS) Manager, right click on "Website" -> "Properties" and select "Services". In the "HTTP compression" box select "Compress application files" and "Compress static files", press to set "temporary directory" and "temporary directory maximum limit";

2 reminder: After testing this step has no effect on my machine, can be ignored.
In the Internet Information Services (IIS) Manager, right click on "Web Service Extensions" -> Add a new Web Service Extension.. "", enter the extension "HTTP Compression" in the "New Web Service Extension" box, add "Required File" to C:\\WINDOWS\\system32\\inetsrv\\gzip.dll, where the Windows system directory may have Differently, check "Set extension status to Allow";




3. Open C:\\Windows\\System32\\inetsrv\\MetaBase with a text editor .xml (recommended first),
find Location="/LM/W3SVC/Filters/Compression/gzip for setting up gzip compression,
find Location="/LM/W3SVC/Filters/Compression/deflate" to deflate compression set.
two nodes immediately above. property and the same set.

compressed moving files if necessary, will be provided HcDoDynamicCompression "TRUE", and add the dynamic file suffix name you want to compress in HcScriptFileExtensions, such as aspx; if you need to compress the static file, set HcDoStaticCompression and HcDoOnDemandCompression to "TRUE", and add the static file you need to compress in HcFileExtensions. The suffix name, such as xml, css, etc.; HcDynamicCompressionLevel and HcOnDemandCompLevel indicate the required compression ratio, the value is 0-10, the default is 0.
HcDynamicCompressionLevel attribute description: HcDynamicCompressionLevel Metabase Property
HcOnDemandCompLevel Attribute Description: HcOnDemandCompLevel Metabase Property

Description: These two attribute values ​​are generally recommended to be set to 9, with the best price/performance ratio. But on my window server 2003, the compression ratio is set anyway, jQuery and jQuery UI two files (58k/188k) compression The size is always the same. (20k/45k).

The following is my example:
<IIsCompressionScheme Location="/LM/W3SVC/Filters/Compression/deflate" HcCompressionDll="%windir% \\system32\\inetsrv\\gzip.dll" HcCreateFlags="0" HcDoDynamicCompression="TRUE" H cDoOnDemandCompression="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, it may be used by IIS. Open "Start" -> "Administrative Tools" -> "Services", stop "IIS Admin Service", you can save;


5. Finally, restart IIS. HTTP compression can go to the site to verify the test results to jQuery, for example, core libraries and UI libraries are the original size 57k and 188k, respectively, are compressed and 20k 45k:.

We Http header: Content-Encoding: gzip data after the judge returned property have enabled gzip compression:

detection using YSlow, when only a starting static file compression, gzip compression rating of B:

when you start at the same time when the dynamic file compression, Gzip compression rating of a:.

six points summarize

1 when modifying the MetaBase.xml file, to stop the "IIS Admin service" service. Otherwise it can't be saved.

2. The static compression and dynamic compression ratios are best set to 9.

3. Step 2 above, even if you do not add Web service extensions are the same. Br>

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

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


VII. Summary

This article summarizes how to enable Gzip compression in IIS. In the project half a year ago, I enabled Gzip for the site. Manage their knowledge for future review if you forget.

Copyright © Windows knowledge All Rights Reserved