gpt4 book ai didi

asp.net - 在 IIS8 中使用 Gzip 进行 Json HTTP 压缩

转载 作者:行者123 更新时间:2023-12-03 17:54:18 25 4
gpt4 key购买 nike

好的,我已经读了几个小时了。数十篇 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 中并期望它覆盖吗?

如果需要,很高兴添加进一步的说明。

干杯

最佳答案

在这里聚会迟到了,但是如果没有 AMI,这绝对是可能的。

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

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

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

接下来需要使用 elastic beanstalk extensions在你的项目中。

将以下 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 elastic beanstalk 事件中,一个名为 init.ps1 的新文件将写入 c:\cfn\init.ps1
  • 在命令事件期间,将执行 init.ps1 文件。
  • init.ps1 文件从 S3 下载安装文件并执行它。 (请注意,您可以将所有的 powershell 内联,但这可以保持 YAML 的清洁,并且可以更轻松地在设置过程中使用多个文件。)
  • 关于asp.net - 在 IIS8 中使用 Gzip 进行 Json HTTP 压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13797044/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com