【问题标题】:IIS 7.5 gzip compression at shared hosting共享主机上的 IIS 7.5 gzip 压缩
【发布时间】:2011-11-02 12:58:41
【问题描述】:

我有一个使用 ASP.NET 并托管在 IIS 7.5 共享主机上的网站,因此我无法直接访问 IIS 设置。现在我想使用 IIS 功能启用我的页面和 css/js 文件的 gzip 压缩,但是在 Internet 上找到的所有方法都没有对我有用。例如,当我将 here 添加到我的 Web.config 中时,没有任何变化:没有错误,没有压缩。

这可能吗?如果没有,最好的选择是什么?

【问题讨论】:

  • 那么在 web.config 中进行这些修改后,您在检查器中看不到 Content-Encoding: gzip 吗?
  • 是的,这就是我写“无压缩”的意思=)

标签: asp.net web-config gzip iis-7.5 shared-hosting


【解决方案1】:

我知道这是超级旧的,但我发现这个链接实际上对我有用。我有 arvixe 托管,它们也不允许压缩。我尝试了 web.config 中所有不起作用的东西,但这在全局中起作用了

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

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,因此使用了使用 global.asax 调用的 .net 压缩以及使用 .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>
    

    在 global.asax 的 Application_PreRequestHandlerExecute 中

    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 相关的 SEO 以及共享托管。我确实尝试过我的托管服务提供商的支持,但他们只是推销了更多奇特的托管包。对于像 .svgz 文件这样的强制压缩内容,我使用了单独的文件夹和该文件夹的 web.config 的 system.webserver 的 httpProtocol 我写道:

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

    【讨论】:

      【解决方案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

      【讨论】:

      • 它也不起作用,所以据我了解,服务器没有给我这样的权限?我对它没有显示任何错误的事实感到困惑。
      • 好吧,我想也许服务器设置为不允许 gzip 压缩以节省 CPU/内存资源。您应该与您的服务器提供商核实它是否在服务器上被禁用,如果它仍然启用它。
      最近更新 更多