2011-08-24 77 views
2

我有一個網站,它使用ASP.NET並託管在IIS 7.5共享託管,所以我沒有直接訪問IIS設置。現在我想使用IIS功能啓用我的頁面和css/js文件的gzip壓縮,但是在互聯網上找到的食譜都不適合我。例如,當我在我的Web.config中添加寫入here的內容時,沒有任何更改:沒有錯誤,沒有壓縮。在共享主機上的IIS 7.5 gzip壓縮

這可能嗎?如果不是,最好的選擇是什麼?

+0

所以你看不到內容編碼web.config中作出這些修改後:gzip的在檢查? –

+0

是的,這就是我的意思是我的寫作「不壓縮」=) – aplavin

回答

3

嘗試配置樣品在這篇文章的底部:

http://www.iis.net/ConfigReference/system.webServer/urlCompression

<configuration> 
    <system.webServer> 
     <urlCompression doStaticCompression="true" doDynamicCompression="false" /> 
    </system.webServer> 
</configuration> 

在這種question它說:

「是的,你可以啓用壓縮與web.config中,正如下面的文章所示 - 但它可以取決於服務器允許網站的權限。「

看看這可以幫助你:

IIS 7.5 ASP.NET-4 Gzip compression

或者這樣:

GZip Compression On IIS 7.5 is not working

+0

它也行不通,據我瞭解,服務器不給我這樣的權限?我感到困惑的是它沒有顯示任何錯誤。 – aplavin

+4

那麼,我想可能是服務器設置爲不允許gzip壓縮來節省CPU /內存資源。您應該檢查您的服務器提供商是否在服務器上禁用了服務器,以及是否無法啓用它。 –

4

我遇到同樣的問題,所以所使用的.NET的壓縮使用調用Global.asax的along-使用.aspx擴展名重命名靜態CSS,並使用通過Web.config重寫URL。在.css.aspx之上我簡單地添加了

<%@ Page ContentType="text/css" %> 

在web.config中的配置system.webserver的重寫規則:

<rule name="san aspx"> 
    <match url=".*[^/]$" /> 
     <conditions logicalGrouping="MatchAll"> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
     </conditions> 
    <action type="Rewrite" url="{R:0}.aspx" /> 
</rule> 
<rule name="revert aspx"> 
    <match url="(.*)\.aspx$"/> 
     <conditions logicalGrouping="MatchAll"> 
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> 
     </conditions> 
    <action type="Rewrite" url="{R:1}" /> 
</rule> 

在Application_PreRequestHandlerExecute Global.asax中的

HttpApplication app = sender as HttpApplication; 
string acceptEncoding = app.Request.Headers["Accept-Encoding"]; 
Stream prevUncompressedStream = app.Response.Filter; 
if (acceptEncoding != null && acceptEncoding.Length > 0) { 
    acceptEncoding = acceptEncoding.ToLower(); 
    if (acceptEncoding.Contains("gzip")){ 
     app.Response.Filter = new GZipStream(prevUncompressedStream, CompressionMode.Compress); 
     app.Response.AppendHeader("Content-Encoding", "gzip"); 
    } 
}  
Response.Cache.VaryByHeaders["Accept-Encoding"] = true; 

看起來嚇人,但與建立正確的認識做,它就像魅力一樣工作,值得與PageSpeed相關的搜索引擎優化以及共享主機上的節省。我曾嘗試與我的託管服務提供商的支持,但他們只是銷售更多異國情調的託管軟件包。對於像名.svgz文件強制壓縮內容,我用單獨的文件夾和httpProtocol該文件夾的web.config的system.webserver的我寫道:

<customHeaders> 
    <add name="Content-Encoding" value="gzip" /> 
</customHeaders>