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

5

好的,我已经阅读了数小时有关此事的信息。阅读了许多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>

很遗憾,在我使用的服务器上,applicationhost.config中缺少以下行:
<add mimeType="application/json; charset=utf-8" enabled="true" />

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

不幸的是,applicationhost.config包括这行:

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

这意味着我不能在应用程序的web.config中覆盖httpCompression部分。

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

如果overrideModeDefault =“Allow”,那么我是否能够将httpCompression部分放置在应用程序的web.config中并期望它被覆盖?

如果需要进一步澄清,请告诉我。

干杯


我也在寻找解决这个问题的方法。 - John Egbert
Duplicate... https://dev59.com/QVPTa4cB1Zd3GeqPgBMRhttps://dev59.com/pmTWa4cB1Zd3GeqPCVrO - ncubica
你是如何解决你的问题的? - ncubica
2
大家好 - 更新一下帖子,经过大量研究,似乎目前无法实现我上面描述的内容。障碍在于Beanstalk,在使用纯BS部署时,您无法运行影响所需设置的管理cls。我们改用自定义AMI,该AMI具有在系统启动时运行的进程 - 在这里我们更改apphost文件。希望对您有所帮助。 - Jimmy_m
1个回答

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."

接下来,您需要在项目中使用弹性 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"

请确保您的实例通过角色获得对存储桶的权限,否则在调用Read-S3Object时传递aws凭证

以上操作执行以下内容

  • 在Files弹性Beanstalk事件上,将写入一个名为init.ps1的新文件到c:\cfn\init.ps1
  • 在Commands事件期间,将执行init.ps1文件。
  • init.ps1文件从S3下载设置文件并执行它。(请注意,您可以将所有powershell内联,但这会使YAML保持清洁,并使其更容易在设置过程中使用多个文件。)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接