在IIS8中使用Gzip进行Json HTTP压缩

Jim*_*y_m 5 asp.net iis wcf json gzip

好的,我已经读了好几个小时了.数十个SO帖子和博客等.没有找到答案.

目标:从我的WCF服务启用json响应的动态http压缩.

注意:当applicationhost.config包含以下内容时,gzip已经适用于静态内容和动态内容:

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

不幸的是在服务器上我使用applicationhost.config中缺少以下行:

<add mimeType="application/json; charset=utf-8" enabled="true" />
Run Code Online (Sandbox Code Playgroud)

而且我无法手动添加它,因为服务器是由Elastic Beanstalk启动的AWS EC2实例(因此我可以在一个实例上更改它,但在启动时不在所有实例上更改它).

还不幸的是,applicationhost.config包含以下行:

<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
Run Code Online (Sandbox Code Playgroud)

这意味着我无法覆盖应用程序的web.config中的httpCompression部分.

我的问题:我是否应该尝试其他方法来启用动态内容的gzip压缩?

如果overrideModeDefault ="Allow",那么我可以将httpCompression部分放在我的应用程序的web.config中并期望它覆盖吗?

如果需要,很乐意进一步澄清.

干杯

Avn*_*ner 0

虽然迟到了,但如果没有 AMI,这绝对是可能的。

创建一个 s3 存储桶来托管您的设置文件。在此示例中,我有一个名为 mys3bucket 的存储桶。将以下文件上传到 mybucket/ebExtensions/setup.ps1 下的存储桶

该文件修改了根应用程序主机配置。

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

接下来,您需要在项目中使用弹性 beanstalk 扩展。

将以下 init.config 文件添加到网站根目录下的 .ebextensions 文件夹中。这是一个 YAML 文件,因此请使用空格缩进而不是制表符。

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

确保您的实例具有通过角色访问存储桶的权限,否则在对 Read-S3Object 的调用中传递 aws 凭证

以上执行以下操作

  • 在 Files elastic beanstalk 事件中,名为 init.ps1 的新文件将被写入 c:\cfn\init.ps1
  • 在命令事件期间,将执行 init.ps1 文件。
  • init.ps1 文件从 S3 下载安装文件并执行它。(请注意,您可以将所有 powershell 内联,但这可以保持 YAML 干净,并且可以在以后的安装过程中更轻松地使用多个文件。)