2012-12-10 10 views
5

Ok, tôi đã đọc hàng giờ về điều này. Hàng chục bài đăng và blog SO, v.v. Không tìm thấy câu trả lời.Json HTTP-Compression Với Gzip Trong IIS8

Mục tiêu: bật tính năng nén http động của phản hồi json từ dịch vụ WCF của tôi.

Lưu ý: gzip đã làm việc cho nội dung tĩnh và cho nội dung động khi applicationhost.config chứa sau:

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files"> 
      <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" /> 
     <dynamicTypes> 
      <add mimeType="text/*" enabled="true" /> 
      <add mimeType="message/*" enabled="true" /> 
      <add mimeType="application/x-javascript" enabled="true" /> 
      <add mimeType="application/json; charset=utf-8" enabled="true" /> 
      <add mimeType="*/*" enabled="false" /> 
     </dynamicTypes> 
     <staticTypes> 
      <add mimeType="text/*" enabled="true" /> 
      <add mimeType="message/*" enabled="true" /> 
      <add mimeType="application/x-javascript" enabled="true" /> 
      <add mimeType="application/atom+xml" enabled="true" /> 
      <add mimeType="application/xaml+xml" enabled="true" /> 
      <add mimeType="*/*" enabled="false" /> 
     </staticTypes>  
</httpCompression> 
</system.webServer> 

Đáng tiếc là trên máy chủ Tôi đang sử dụng các dòng sau là mất tích từ applicationhost.config:

<add mimeType="application/json; charset=utf-8" enabled="true" /> 

Và tôi không thể thêm nó bằng tay vì máy chủ là một thể hiện AWS EC2 đưa ra bởi đàn hồi Beanstalk (như vậy tôi có thể thay đổi nó vào một trường hợp nhưng không phải trên tất cả các trường mỗi khi họ được đưa ra).

Cũng may, các applicationhost.config bao gồm dòng này:

<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" /> 

Điều đó có nghĩa rằng tôi không thể ghi đè lên phần httpCompression trong web.config ứng dụng của tôi.

Câu hỏi của tôi: có cách tiếp cận nào khác để cho phép nén gzip nội dung động mà tôi nên thử không?

Nếu ghi đè lênModeDefault = "Cho phép" thì tôi có thể đặt phần httpCompression trong web.config của ứng dụng và mong muốn ghi đè lên?

Vui lòng thêm làm rõ thêm nếu cần.

Chúc mừng

+0

Tôi cũng đang tìm giải pháp cho cùng một vấn đề này. –

+0

Trùng lặp ... http://stackoverflow.com/questions/4584956/compressing-a-web-service-response-for-jquery http://stackoverflow.com/questions/10795165/iis-7-5 -not-compressing-json-khi-set-in-application-web-config – ncubica

+0

cách bạn giải quyết vấn đề của mình ?? – ncubica

Trả lời

0

Đến bữa tiệc ở đây, nhưng điều này chắc chắn có thể không có AMI.

Tạo nhóm s3 để lưu trữ (các) tệp thiết lập của bạn. Trong ví dụ này, tôi có một thùng được gọi là mys3bucket. Tải tệp sau lên thùng dưới mybucket/ebExtensions/setup.ps1

Tệp này đã sửa đổi cấu hình máy chủ ứng dụng gốc.

write-host "Applying IIS configuration ..." 

$globalConfig = "C:\Windows\System32\inetsrv\config\applicationHost.config" 

$xmlDoc = new-object System.Xml.XmlDocument 

$xmlDoc.Load($globalConfig) 

#Set minimum compression size 
write-host "Setting minimum compression size ..." 
$xmlCurrentNode = $xmlDoc.SelectSingleNode("/configuration/system.webServer/httpCompression") 
if ($xmlCurrentNode -eq $null) 
{ 
    $xmlCurrentNode = $xmlDoc.CreateElement("httpCompression") 
    $xmlDoc.SelectSingleNode("/configuration/system.webServer").AppendChild($xmlCurrentNode) 
} 
$xmlCurrentNode.SetAttribute("minFileSizeForComp", "10240") 
$xmlCurrentNode.SetAttribute("dynamicCompressionEnableCpuUsage", "70") 
write-host "Done." 

#Enable dynamic compression 
write-host "Enabling dynamic compression ..." 
$xmlCurrentNode = $xmlDoc.SelectSingleNode("/configuration/system.webServer/urlCompression") 
if ($xmlCurrentNode -eq $null) 
{ 
    $xmlCurrentNode = $xmlDoc.CreateElement("urlCompression") 
    $xmlDoc.SelectSingleNode("/configuration/system.webServer").AppendChild($xmlCurrentNode) 
} 
$xmlCurrentNode.SetAttribute("doDynamicCompression", "true") 
write-host "Done." 

#Add dynamic types for compression 
write-host "Adding dynamic types ..." 
$xmlCurrentNode = $xmlDoc.SelectSingleNode("/configuration/system.webServer/httpCompression/dynamicTypes") 
if ($xmlCurrentNode -eq $null) 
{ 
    $xmlCurrentNode = $xmlDoc.CreateElement("dynamicTypes") 
    $xmlDoc.SelectSingleNode("/configuration/system.webServer/httpCompression").AppendChild($xmlCurrentNode) 
} 
$xmlCurrentNode = $xmlDoc.SelectSingleNode("/configuration/system.webServer/httpCompression/dynamicTypes/add[@mimeType='application/*']") 
if ($xmlCurrentNode -eq $null) 
{ 
    $xmlCurrentNode = $xmlDoc.CreateElement("add") 
    $xmlCurrentNode.SetAttribute("mimeType", "application/*") 
    $xmlDoc.SelectSingleNode("/configuration/system.webServer/httpCompression/dynamicTypes").AppendChild($xmlCurrentNode) 
} 
$xmlCurrentNode.SetAttribute("enabled", "true") 
write-host "Done." 

write-host "Saving the target config file ..." 
$xmlDoc.Save("$globalConfig") 
write-host "Done." 

Tiếp theo, bạn cần sử dụng elastic beanstalk extensions trong dự án của mình.

Thêm tệp init.config sau vào thư mục .ebextensions ngoài gốc của trang web của bạn. Đây là một tập tin YAML để sử dụng không gian thụt đầu dòng không phải tab.

files: 
    "c:/cfn/init.ps1": 
    content: | 
     $instanceDoc = Invoke-RestMethod 'http://169.254.169.254/latest/dynamic/instance-identity/document' 
     $extPath = "c:\cfn\.ebextensions" 
     if (Test-Path $extPath) { 
      Remove-Item $extPath -Recurse 
     } 
     Read-S3Object -BucketName "mys3bucket" -Folder $extPath -KeyPrefix '/ebExtensions' -Region ($instanceDoc.region) 
     . (Join-Path $extPath -ChildPath '\setup.ps1') 
container_commands: 
    00-invoke-init: 
    command: powershell.exe -nologo -noprofile -file "c:\cfn\init.ps1" 

Hãy chắc chắn rằng ví dụ bạn có quyền xô qua một vai trò, nếu không vượt qua AWS thông tin trong các cuộc gọi đến Read-S3Object

Trên đây thực hiện như sau

  • Trên Tập tin beanstalk đàn hồi sự kiện một tập tin mới gọi là init.ps1 sẽ được ghi vào c: \ cfn \ init.ps1
  • Trong sự kiện Lệnh, tệp init.ps1 được thi hành.
  • init.Tệp ps1 tải xuống tệp thiết lập từ S3 và thực hiện nó. (Lưu ý bạn có thể đặt tất cả các quyền hạn trong nội tuyến nhưng điều này giữ cho YAML sạch và giúp dễ dàng sử dụng nhiều tệp trong khi thiết lập.)