简体   繁体   English

Azure网站上的HTTP 1.0代理请求的gzip压缩

[英]gzip compression for HTTP 1.0 proxy requests on Azure Web Sites

Is there any way to get Azure Web Sites to serve gzip'ed content for requests from a HTTP 1.0 proxy like Amazon Web Services CloudFront ? 有没有办法让Azure网站为来自像Amazon Web Services CloudFront这样的HTTP 1.0代理的请求提供gzip的内容? Consider a request like this: 考虑这样的请求:

curl -I -H "accept-encoding: gzip,deflate,sdch" -H "Via: 1.0 {foo.cdn.net}" -0 http://{fooproject}.azurewebsites.net/

It seems that the general way to accomplish is to add the following element to system.webServer : 似乎要完成的一般方法是将以下元素添加到system.webServer

<httpCompression noCompressionForHttp10="false" noCompressionForProxies="false" />

It also seems that httpCompression is only valid in ApplicationHost.config and not web.config which means that it's not overwriteable on Azure Web Sites. 似乎httpCompressionApplicationHost.config 有效 ,而不是web.config ,这意味着它在Azure网站上不可覆盖。

Any suggestions for workarounds? 有关解决方法的任何建议吗?

Additional resources: 其他资源:

IIS won't compress for HTTP/1.0 requests. IIS不会压缩HTTP / 1.0请求。 You can override this behaviour by setting: 您可以通过设置来覆盖此行为:

appcmd set config -section:system.webServer/httpCompression /noCompressionForHttp10:"False"

Check these 2 articles, they might help you if you want to send compressed (gzip) content back to the clients: 检查这两篇文章,如果您想将压缩(gzip)内容发送回客户端,它们可能会对您有所帮助:

http://christesene.com/mvc-3-action-filters-enable-page-compression-gzip/ http://christesene.com/mvc-3-action-filters-enable-page-compression-gzip/

http://www.west-wind.com/weblog/posts/2012/Apr/28/GZipDeflate-Compression-in-ASPNET-MVC http://www.west-wind.com/weblog/posts/2012/Apr/28/GZipDeflate-Compression-in-ASPNET-MVC

The auto-magical HTTP module that does the job is presented below. 执行此任务的自动魔术HTTP模块如下所示。 You need to register it in your Web.config file. 您需要在Web.config文件中注册它。

/// <summary>
/// Provides HTTP compression support for CDN services when
/// ASP.NET website is used as origin.
/// </summary>
public sealed class CdnHttpCompressionModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PreRequestHandlerExecute += Context_PreRequestHandlerExecute;
    }

    public void Dispose()
    {
    }

    void Context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        var application = (HttpApplication)sender;
        var request = application.Request;
        var response = application.Response;

        // ---------------------------------------------------------------------

        bool allowed = false;

        string via = request.Headers["Via"];
        if (!string.IsNullOrEmpty(via))
        {
            if (via.Contains(".cloudfront.net"))
            {
                // Amazon CloudFront
                allowed = true;
            }

            // HINT: You can extend with other criterias for other CDN providers.
        }

        if (!allowed)
            return;

        // ---------------------------------------------------------------------

        try
        {
            if (request["HTTP_X_MICROSOFTAJAX"] != null)
                return;
        }
        catch (HttpRequestValidationException)
        {
        }

        // ---------------------------------------------------------------------

        string acceptEncoding = request.Headers["Accept-Encoding"];
        if (string.IsNullOrEmpty(acceptEncoding))
            return;

        string fileExtension = request.CurrentExecutionFilePathExtension;
        if (fileExtension == null)
            fileExtension = string.Empty;
        fileExtension = fileExtension.ToLowerInvariant();

        switch (fileExtension)
        {
            case "":
            case ".js":
            case ".htm":
            case ".html":
            case ".css":
            case ".txt":
            case ".ico":
                break;

            default:
                return;
        }

        acceptEncoding = acceptEncoding.ToLowerInvariant();
        string newContentEncoding = null;

        if (acceptEncoding.Contains("gzip"))
        {
            // gzip
            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
            newContentEncoding = "gzip";
        }
        else if (acceptEncoding.Contains("deflate"))
        {
            // deflate
            response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
            newContentEncoding = "deflate";
        }

        if (newContentEncoding != null)
        {
            response.AppendHeader("Content-Encoding", newContentEncoding);
            response.Cache.VaryByHeaders["Accept-Encoding"] = true;
        }
    }
}

The module is designed to work with IIS 7.0 or higher in integrated pipeline mode (Azure Websites have exactly this out of the box). 该模块设计用于在集成管道模式下使用IIS 7.0或更高版本(Azure网站具有开箱即用的功能)。 That is the most widespread configuration, so generally it just works once you attach it. 这是最普遍的配置,所以一般来说只要你附上它就可以了。 Please note that the module should be the first one in a list of modules. 请注意,该模块应该是模块列表中的第一个模块。

Web.config registration sample: Web.config注册示例:

<configuration>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="CdnHttpCompressionModule" preCondition="managedHandler" type="YourWebsite.Modules.CdnHttpCompressionModule, YourWebsite" />
      <!-- You may have other modules here -->
    </modules>  
  <system.webServer>
</configuration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM