2011-08-24 37 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>