创建 PowerShell 脚本以启用 HTTPCompression 并在网站的配置文件中使用命令行 (appcmd) 添加 MimeType

问题描述

我想启用 httpcompression,然后使用 appcmd 将 mimetypes 添加到 web.config 文件。 我知道我们可以从

applicationHost.config

文件。 IIS7.5以上默认开启

我们可以从

%windir%\System32\inetsrv\config\applicationHost.config

但我的要求是使用 appcmd 直接启用和添加 mime 类型到 web.config 文件(基本上覆盖 applicationHost.config 中现有的设置)

解决方法

您可以使用以下命令启用和禁用站点压缩:

appcmd set config "site1" /section:urlCompression /doDynamicCompression:True

appcmd set config "urlsample" /section:urlCompression /doStaticCompression:True

要添加 MIME 类型,请使用以下语法:

appcmd set config /section:staticContent /+"[fileExtension='string',mimeType='string']"

变量 fileExtension 字符串是文件扩展名。变量 mimeType 字符串是 MIME 类型。例如,要创建 MIME 类型,请在命令提示符下键入以下内容,然后按 ENTER:

appcmd set config /section:staticContent /+"[fileExtension='.xyz',mimeType='application/octet-stream']"

有关添加 MIME 类型的更多信息,您可以参考此链接:To add a MIME type

,
$appcmdpath="$env:windir\system32\inetsrv\appcmd.exe"
$path="{PathofHostedAppFromIIS}/"

ECHO 'Remove Existing sections if any this is important if we face issues any issue while it for the first time or running the script mutiple times incase of any error'
& $appcmdpath clear config $path -section:system.webServer/httpCompression /delete:true /commit:app
& $appcmdpath clear config $path -section:system.webServer/urlCompression /delete:true /commit:app

ECHO 'Enable Http Compression'
& $appcmdpath set config $path -section:system.webServer/httpCompression /directory:'%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files' /commit:app

ECHO 'Clear apphost default compression'
& $appcmdpath set config $path -section:system.webServer/httpCompression /~"staticTypes" /commit:app

ECHO 'Add default stypes available in Apphost'
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='text/*',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='message/*',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/javascript',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/atom+xml',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='application/xaml+xml',enabled='True']" /commit:app
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='image/svg+xml',enabled='True']" /commit:app

ECHO 'Below script can be added to exclude mime types '
& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='image/jpeg',enabled='False']" /commit:app

& $appcmdpath set config $path -section:system.webServer/httpCompression /+"staticTypes.[mimeType='*/*',enabled='False']" /commit:app

ECHO 'Enable URL Compression'
& $appcmdpath set config $path -section:system.webServer/urlCompression /doDynamicCompression:"true"  /doStaticCompression:"true" /commit:app

我们使用 commit:app 在应用程序的 web.config 中添加配置条目 您可以使用 /commit:apphost 在位于

的全局 apphost 文件中添加相同的内容
%windir%\System32\inetsrv\config\applicationHost.config

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...