﻿# 如何調整 IIS7 的HTTP內容壓縮格式(為了讓 IE6 能正常運作)

談到 IE6 就有吐不完的怨氣，最近在幫客戶設定 IIS7 的過程中，為了要讓將輸出的內容全部壓縮，所以設定了 IIS7 內建的內容壓縮功能，包括「動態內容壓縮」與「靜態內容壓縮」，但是啟動完後的幾天卻有使用者反應使用 IE6 瀏覽網站時瀏覽器會掛掉(Crash)，研究了一些時間才發現原來是 IIS7 預設的 gzip 壓縮格式搞的鬼。

![IIS7 內容壓縮，包括「動態內容壓縮」與「靜態內容壓縮」](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/bec4c22ccd302a50acd3-image_3.png "IIS7 內容壓縮，包括「動態內容壓縮」與「靜態內容壓縮」")

在 IIS6 的年代，預設 HTTP 壓縮的格式同時支援 [gzip](http://en.wikipedia.org/wiki/Gzip) 與 [deflate](http://en.wikipedia.org/wiki/DEFLATE) 壓縮格式，這兩種格式也是大多瀏覽器都同時支援的壓縮/解壓縮的格式。不過到了 IIS7 時，在**預設**只會支援 [gzip](http://en.wikipedia.org/wiki/Gzip) 壓縮格式。

雖然 [gzip](http://en.wikipedia.org/wiki/Gzip) 壓縮格式也很常見，且大多數瀏覽器都支援，但是偏偏在微軟自家的 IE6 的期中一個特定版本(版本編號不太確定)對於 [gzip](http://en.wikipedia.org/wiki/Gzip) 解壓縮的程式出現一個[嚴重的錯誤](http://www.google.com/search?hl=zh-TW&esrch=BetaShortcuts&q=ie+gzip+bug&lr=)，這會導致瀏覽器直接關閉(Crash)或死當(Hang)。

而我解決這個問題的方法是將 IIS7 預設的壓縮格式多新增一個 [deflate](http://en.wikipedia.org/wiki/DEFLATE) 壓縮格式的設定，設定的方式可以透過 IIS 內建的 [appcmd](https://learn.microsoft.com/en-us/iis/get-started/getting-started-with-iis/getting-started-with-appcmdexe) 指令進行調整，或手動修改 **applicationHost.config** 設定檔。

**1\. 透過** [**appcmd**](https://learn.microsoft.com/en-us/iis/get-started/getting-started-with-iis/getting-started-with-appcmdexe) **指令進行調整**

首先，沒用過 appcmd 的人可能會找不到 appcmd 的程式位置，在預設 IIS7 安裝下，此工具程式會放在 C:\\Windows\\System32\\inetsrv 目錄下，你必須先開啟**命令提示字元**進入該目錄再執行以下指令：

> appcmd set config /section:httpCompression /+\[name='deflate',dll='%Windir%\\system32\\inetsrv\\gzip.dll'\]

**2\. 手動修改 [applicationHost.config](http://www.google.com/search?hl=zh-TW&esrch=BetaShortcuts&q=applicationHost.config+&lr=&aq=f&oq=#i=0) 設定檔**

IIS7 的所有設定基本上都放在 **C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config** 這個檔案裡，所以只要是 Server-Level 的設定都可以在這裡找到。

先以 “<httpCompression” 進行搜尋，找到之後在這行下方有另一行內容如下：

> <scheme name="gzip" doStaticCompression="true" doDynamicCompression="true" dll="%Windir%\\system32\\inetsrv\\gzip.dll" staticCompressionLevel="10" dynamicCompressionLevel="3" />

你在這行下方手動再加上以下這行宣告：

> <scheme name="deflate" dll="C:\\Windows\\system32\\inetsrv\\gzip.dll" />

\---

其實 IIS7 預設還是有支援 [deflate](http://en.wikipedia.org/wiki/DEFLATE) 壓縮格式的，這部分的程式也包裝在 C:\\Windows\\system32\\inetsrv\\gzip.dll 這支 DLL 裡，只是預設沒啟用而已。但明知 IE6 對 gzip 支援有問題，在 IIS7 的設定中還**預設不啟用**，我在想這期中應該是有鼓勵大家趕快升級到最新版 IE 的意味存在。

※ 若要在 IIS6 下啟動內容壓縮可以參考我之前寫的【[調整 IIS 將輸出的內容全部壓縮](/post/2008/07/03/Configure-IIS-to-compress-all-the-output-content)】文章。

**相關連結**

-   [Changes to compression in IIS7](https://blogs.iis.net/ksingla/changes-to-compression-in-iis7/)
-   [使用指令工具 Appcmd 管理與設定 IIS 7](https://dotblogs.com.tw/tigerlin/2009/01/31/6988)
-   [Getting Started with AppCmd.exe](https://learn.microsoft.com/en-us/iis/get-started/getting-started-with-iis/getting-started-with-appcmdexe)
-   [How to use the Appcmd.exe command-line tool to enable and configure HTTP logging and other features in Internet Information Services 7.0](https://web.archive.org/web/20150215160023/http://support.microsoft.com:80/kb/930909)
