共享主机上的 IIS 7.5 gzip 压缩

apl*_*vin 2 asp.net gzip shared-hosting web-config iis-7.5

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

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

Cha*_*pul 5

我遇到了同样的问题,因此使用了使用 global.asax 调用的 .net 压缩,以及使用 .aspx 扩展名重命名静态 css 并通过 Web.config 使用 url 重写 在我简单添加的 .css.aspx 之上

<%@ Page ContentType="text/css" %>
Run Code Online (Sandbox Code Playgroud)

在 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>
Run Code Online (Sandbox Code Playgroud)

在 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;
Run Code Online (Sandbox Code Playgroud)

它看起来令人生畏,但通过建立适当的理解,它就像魅力一样,值得在与 PageSpeed 以及共享托管相关的 SEO 上节省。我确实尝试过我的托管服务提供商的支持,但他们只是销售更多异国情调的托管包。对于像 .svgz 文件这样的强制压缩内容,我使用了单独的文件夹和该文件夹的 web.config 的 system.webserver 的 httpProtocol 我写道:

<customHeaders>
    <add name="Content-Encoding" value="gzip" />
</customHeaders>
Run Code Online (Sandbox Code Playgroud)