IIS啟用GZIP壓縮之后,原以為可以壓縮所有文件了,包括html、CSS、JS、圖片這些文件,但是當(dāng)我檢查的時(shí)候,發(fā)現(xiàn)并不是這樣的,壓縮的只有html文件,而CSS、JS并沒有壓縮。
在卡卡網(wǎng)的網(wǎng)站速度診斷里的檢測結(jié)果:
html網(wǎng)頁已經(jīng)成功啟用了GZip壓縮
css文件未成功啟用GZip壓縮
后來研究發(fā)現(xiàn),IIS啟用GZip時(shí)默認(rèn)壓縮的只有html網(wǎng)頁,如果需要壓縮css、js等文件,還需要另外簡單配置一個(gè)文件。
打開C:\WINDOWS\system32\inetsrv\MetaBase.xml這個(gè)文件,然后查找“IIsCompressionScheme”,會發(fā)現(xiàn)有三段,后面那個(gè)不用管,只設(shè)置前面兩個(gè)即可。其中,"/LM/W3SVC/Filters/Compression/gzip"用于設(shè)置gzip壓縮,"/LM/W3SVC/Filters/Compression/deflate"用于設(shè)置deflate壓縮。
如需壓縮靜態(tài)文件,則將HcDoStaticCompression和HcDoOnDemandCompression設(shè)置為“TRUE”,并在HcFileExtensions的屬性中加入需要壓縮的靜態(tài)文件:css、js等;
如需壓縮動態(tài)文件,則將HcDoDynamicCompression設(shè)置為“TRUE”,并在HcScriptFileExtensions屬性中加入需要壓縮的動態(tài)文件:aspx、php等。
另外,HcDynamicCompressionLevel屬性和HcOnDemandCompLevel屬性一般都推薦設(shè)置為9, 具有最佳性價(jià)比。
參考如下各參數(shù)設(shè)置:
<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
css
js
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
css
js
xml"
HcOnDemandCompLevel="9"
HcPriority="1"
HcScriptFileExtensions="asp
aspx
dll
exe"
>
</IIsCompressionScheme>
設(shè)置完畢后,重新啟動IIS,再檢測時(shí)發(fā)現(xiàn)css、js等文件已經(jīng)成功啟用GZip了。
您可能對如下文章也感興趣