2012-12-13 30 views
5

Có cách nào để nhận Azure Web Sites để phân phát nội dung gzip'ed cho các yêu cầu từ proxy HTTP 1.0 như Amazon Web Services CloudFront không? Hãy xem xét một yêu cầu như thế này:nén gzip cho yêu cầu proxy HTTP 1.0 trên Azure Web Sites

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

Dường như cách tổng quát để thực hiện là thêm các yếu tố sau đây để system.webServer:

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

Cũng dường như httpCompression is only valid trong ApplicationHost.config và không web.config có nghĩa là nó không thể ghi đè lên Azure Web Sites.

Bất kỳ đề xuất nào về cách giải quyết?

Tài nguyên bổ sung:

Trả lời

0

Mô-đun HTTP tự động phép thuật thực hiện công việc được trình bày bên dưới. Bạn cần phải đăng ký nó trong tệp Web.config của bạn.

/// <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; 
     } 
    } 
} 

Mô-đun được thiết kế để hoạt động với IIS 7.0 trở lên trong chế độ đường ống tích hợp (Trang web Azure có chính xác điều này ngoài hộp). Đó là cấu hình phổ biến nhất, vì vậy thường nó chỉ hoạt động khi bạn đính kèm nó. Xin lưu ý rằng mô-đun phải là mô-đun đầu tiên trong danh sách các mô-đun. mẫu đăng ký

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

IIS sẽ không nén cho HTTP/1.0 yêu cầu. Bạn có thể ghi đè hành vi này bằng cách đặt:

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

Các vấn đề liên quan