问题描述
好的,我已经读了好几个小时了。数十个SO帖子和博客等。没有找到答案。
Ok, I've been reading for hours about this. Dozens of SO posts and blogs, etc. No answer to be found.
目标:从我的WCF服务启用json响应的动态http压缩。
Goal: enable dynamic http compression of json response from my WCF service.
注意:gzip已经适用于applicationhost.config包含以下内容时的静态内容和动态内容:
Note: gzip already works for static content and for dynamic content when applicationhost.config contains the following:
<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中缺少以下行:
Unfortunately on the server I'm using the following line is missing from applicationhost.config:
<add mimeType="application/json; charset=utf-8" enabled="true" />
我无法手动添加它,因为服务器是Elastic Beanstalk发布的AWS EC2实例(如此我可以在一个实例上更改它,但不能在所有实例上更改它们。)
And I cannot add it manually because the server is an AWS EC2 instance launched by Elastic Beanstalk (as such I could change it on one instance but not on all instances whenever they are launched).
同样不幸的是,applicationhost.config包含以下行:
Also unfortunately, the applicationhost.config includes this line:
<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
这意味着我无法覆盖应用的web.config中的httpCompression部分。
Which means that I cannot override the httpCompression section in my app's web.config.
我的问题:我是否有其他方法可以尝试对动态内容进行gzip压缩?
My question: are there other approaches to enabling gzip compression of dynamic content that I should try?
如果overrideModeDefault =Allow那么我是否可以将httpCompression部分放在我的应用程序的web.config中并期望它覆盖?
If overrideModeDefault="Allow" would I then be able to place the httpCompression section in my app's web.config and expect it to override?
如果需要,很高兴添加进一步的说明。
Happy to add further clarification if needed.
干杯
推荐答案
参加聚会的时间已晚,但如果没有AMI,这绝对是可能的。
Late to the party here, but this is definitely possible without an AMI.
创建一个s3存储桶来托管您的安装文件。在这个例子中,我有一个名为mys3bucket的存储桶。将以下文件上传到mybucket / ebExtensions / setup.ps1下的存储桶
Create an s3 bucket to host your setup file(s). In this example I have a bucket called mys3bucket. Upload the following file to the bucket under mybucket/ebExtensions/setup.ps1
此文件修改了根应用程序主机配置。
This file modified the root application host config.
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."
接下来你需要使用在您的项目中。
Next you need to use elastic beanstalk extensions in your project.
将以下init.config文件添加到网站根目录下的.ebextensions文件夹中。这是一个YAML文件,因此请使用空格缩进而不是制表符。
Add the following init.config file to the .ebextensions folder off the root of your web site. This is a YAML file so use space indentation not tabs.
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
- 期间执行init.ps1文件的命令事件。
- init.ps1文件从S3下载安装文件并执行它。 (注意,您可以将所有powershell内联,但这样可以保持YAML的清洁,并且在安装过程中更容易使用多个文件。)
这篇关于在IIS8中使用Gzip进行Json HTTP压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!