开发者

IIS 7.5 gzip compression at shared hosting

I have a web-site, which uses ASP.NET and is hosted at IIS 7.5 shared hosting, so I have no direct 开发者_运维问答access to IIS settings. Now I want to enable gzip compression of my pages and css/js files using IIS capabilities, but none of recipes found at the Internet worked for me. For example, when I add what is written here to my Web.config, nothing changes: no errors, no compression.

Is this possible? If not, what's the best alternative?


I encountered the same problem, and so used .net's compression invoked using global.asax along-with renaming static css with .aspx extension and using url rewrite through Web.config Atop the .css.aspx I simply added

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

In rules of rewrite of system.webserver of configuration of Web.config:

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

In Application_PreRequestHandlerExecute of 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;

It appears intimidating, but done with building proper understanding, it works like charm and is worth the savings on SEO related with PageSpeed as well as shared hosting. I did try with my hosting provider's support, but they just marketed more exotic hosting packages instead. For compulsorily compressed content like .svgz file, I've used separate folder and httpProtocol of system.webserver of that folder's web.config I wrote:

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


Try the configuration sample at the bottom of this article:

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

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

In this question it says that:

"Yes you can enable compression with the web.config, as the article below shows- but it can depend on the permissions on the server allows sites."

See if this helps you:

IIS 7.5 ASP.NET-4 Gzip compression

Or this:

GZip Compression On IIS 7.5 is not working


I know this is super old but I found this link which actually worked for me. I have arvixe hosting and they dont allow compression either. I tried everything in web.config that didnt work but this in the global worked

http://forums.asp.net/t/1599397.aspx?Best+Way+to+Enable+HTTP+Compression

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜