Iis 7 IIS7 GZIP压缩-高温高压压缩段
我试图在IIS7上配置Iis 7 IIS7 GZIP压缩-高温高压压缩段,iis-7,web-config,gzip,Iis 7,Web Config,Gzip,我试图在IIS7上配置httpCompression。通过谷歌搜索,我发现可以使用config中的httpCompression部分来实现。问题是,我无法从web.config使其工作 当我在applicationHost.config中进行配置时,一切都可以根据需要进行,但我希望能够针对每个应用程序而不是全局进行配置 我将applicationHost.config中的节定义更改为,并将httpCompression节移动到web.config: <httpCompression dir
httpCompression
。通过谷歌搜索,我发现可以使用config中的httpCompression
部分来实现。问题是,我无法从web.config使其工作
当我在applicationHost.config
中进行配置时,一切都可以根据需要进行,但我希望能够针对每个应用程序而不是全局进行配置
我将applicationHost.config
中的节定义更改为
,并将httpCompression
节移动到web.config:
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
<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>
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/json" enabled="true" />
<add mimeType="application/json; charset=utf-8" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
</httpCompression>
我错过了什么?看起来IIS根本不从web.config读取压缩配置
每次更改后,我都会对应用程序池进行回收,因此这不是问题。您应该检查整个应用程序池
如果您从
applicationHost
中删除了该节,则可能是从父目录的machine.config
或web.config
继承的。根据此服务器故障答案:-您不能在web.config中更改httpCompression,需要在applicationHost.config文件中完成。以下是我在Azure web角色中用于修改applicationHost.config文件和添加mime类型以进行压缩的代码:
using (var serverManager = new ServerManager())
{
var config = serverManager.GetApplicationHostConfiguration();
var httpCompressionSection = config.GetSection("system.webServer/httpCompression");
var dynamicTypesCollection = httpCompressionSection.GetCollection("dynamicTypes");
Action<string> fnCheckAndAddIfMissing = mimeType =>
{
if (dynamicTypesCollection.Any(x =>
{
var v = x.GetAttributeValue("mimeType");
if (v != null && v.ToString() == mimeType)
{
return true;
}
return false;
}) == false)
{
ConfigurationElement addElement = dynamicTypesCollection.CreateElement("add");
addElement["mimeType"] = mimeType;
addElement["enabled"] = true;
dynamicTypesCollection.AddAt(0, addElement);
}
};
fnCheckAndAddIfMissing("application/json");
fnCheckAndAddIfMissing("application/json; charset=utf-8");
serverManager.CommitChanges();
}
使用(var serverManager=newservermanager())
{
var config=serverManager.GetApplicationHostConfiguration();
var httpCompressionSection=config.GetSection(“system.webServer/httpCompression”);
var dynamictypescolection=httpCompressionSection.GetCollection(“dynamicTypes”);
操作fnCheckAndAddIfMissing=mimeType=>
{
if(dynamicTypesCollection.Any)(x=>
{
var v=x.GetAttributeValue(“mimeType”);
if(v!=null&&v.ToString()==mimeType)
{
返回true;
}
返回false;
})==假)
{
ConfigurationElement addElement=dynamicTypesCollection.CreateElement(“添加”);
addElement[“mimeType”]=mimeType;
addElement[“enabled”]=true;
dynamicTypesCollection.AddAt(0,addElement);
}
};
fnCheckAndAddIfMissing(“应用程序/json”);
fnCheckAndAddIfMissing(“application/json;charset=utf-8”);
serverManager.CommitChanges();
}
ServerManager
来自NuGet中的Microsoft.Web.Administration
包。我找到了解决方案。我将applicationHost.config中的节定义从修改为(allowDefinition=“Everywhere”解决了这个问题)。感谢you@AlexDn:我想您应该将您的解决方案作为实际答案发布。@sharptooth实际上我只能在IIS服务器级别上配置它。未找到针对每个应用程序配置的解决方案。@AlexDn:无论如何,您的解决方案及其限制说明在作为答案发布时将更有用。