You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在web.config而非applicationHost.config中设置允许的服务器变量

在IIS 10中配置负载均衡后HTTPS重写:allowedServerVariables的配置问题与替代方案

问题说明

我在IIS 10上运行非核心ASP.NET应用,为负载均衡后的HTTPS配置重写规则时遇到困惑:相关配置提到需将HTTPS添加到applicationHost.config的allowedServerVariables列表(或通过URL Rewrite配置),对应配置片段如下:

<rewrite>
    <allowedServerVariables>
        <add name="HTTPS" />
    </allowedServerVariables>
</rewrite>

我原本以为可以在应用专属的web.config中配置该内容,不想修改全局的applicationHost.config,但尝试将<allowedServerVariables>放到重写规则相关节点下时出现错误,想确认它在web.config中的正确位置。

关键结论

经过尝试后确认:allowedServerVariables节点无法在应用级web.config中配置。这个配置节默认被IIS锁定,只能在全局的applicationHost.config中修改,或者通过脚本先解锁该配置节后再添加变量。

替代方案(PowerShell脚本)

由于IIS配置工具使用体验不佳,可通过以下服务器端PowerShell脚本完成配置,脚本通过try/catch保证幂等性(重复执行不会报错):

# 用try/catch保证幂等性
# 此处ErrorAction不生效
# 尽管网上有示例,但Get-WebConfigurationProperty和Get-WebConfiguration都无法检查变量是否已存在
try {
    C:/Windows/system32/inetsrv/appcmd unlock config -section:system.webServer/rewrite/allowedServerVariables
    Add-WebConfigurationProperty -pspath "MACHINE/WEBROOT/APPHOST"  -filter "system.webServer/rewrite/allowedServerVariables" -name "." -value @{name='HTTPS'}
}catch{}

内容的提问来源于stack exchange,提问作者red888

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 16:20:16