Json HTTP压缩与Gzip在IIS8



好吧,我已经读了好几个小时了。数十篇SO帖子和博客等。找不到答案。

目标:启用WCF服务中json响应的动态http压缩。

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

<httpCompression directory="%SystemDrive%inetpubtempIIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%system32inetsrvgzip.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中,并期望它进行覆盖?

如果需要,很乐意添加进一步的澄清。

干杯

这里的聚会迟到了,但如果没有AMI,这肯定是可能的。

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

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

write-host "Applying IIS configuration ..."
$globalConfig = "C:WindowsSystem32inetsrvconfigapplicationHost.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."

接下来,你需要在你的项目中使用弹性豆茎延伸。

将以下init.config文件添加到网站根目录下的.eextensions文件夹中。这是一个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:cfninit.ps1"

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

以上执行以下

  • 在Files elastic beanstall事件中,一个名为init.ps1的新文件将被写入c:\cfn\init.ps1
  • 在Commands事件期间,将执行init.ps1文件
  • init.ps1文件从S3下载安装文件并执行它

最新更新