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. Preface.

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 very critical one. Starting Gip compression will immediately reduce the network transfer size of the page.


III. HTTP compression overview

HTTP compression is on the web server and A method of transferring compressed text content between browsers. 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 works

Web server processing HTTP compression works as follows:

1.Web server receives browser HTTP After the request, check if the browser supports 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 (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 HTML, CSS, etc. static The file and the file suffix have compression enabled, then the web server checks the compressed 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 to the browser. Uncompressed please File, and 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. 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 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 Different, check "Set Extended Status to Allow";





3.Open C:\\Windows with a text editor \\System32\\inetsrv\\MetaBase.xml (recommended first),
find Location="/LM/W3SVC/Filters/Compression/gzip to set up gzip compression,
find Location="/LM/W3SVC/Filters /Compression/deflate" is used to set the deflate compression.
The above two nodes are next to each other and the properties are the same.

If you need to compress dynamic files, then HcDoDynamicCompres Set sion to "TRUE", and add the dynamic file suffix name you want to compress in HcScriptFileExtensions, such as aspx; if you need to compress static files, set HcDoStaticCompression and HcDoOnDemandCompression to "TRUE", and add the compression you need in HcFileExtensions. Static file suffixes, 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 The size after compression 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" 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" HcOnDemandCompLe Vel="9" HcPriority="1" HcScriptFileExtensions="asp
aspx dll exe" ></IIsCompressionScheme>


4. Save the MetaBase.xml file after editing; if the file Unable to save, it is possible that IIS is using the file. 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. Also provided by the company to see ChinaCache CDN also enabled Gzip compression.

Copyright © Windows knowledge All Rights Reserved