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

Windows Server 2022 IIS反向代理:请求头重写不生效问题求助

IIS反向代理中Connection头未重写为upgrade的解决方法

1. 确认服务器变量的允许配置

确保URL Rewrite模块的「允许的服务器变量」列表中已正确添加HTTP_CONNECTION:

  • 打开IIS管理器,选中目标站点
  • 点击右侧「URL Rewrite」,再选择「查看服务器变量」
  • 检查HTTP_CONNECTION是否在允许列表内,若缺失则添加(变量名不区分大小写,但建议统一为大写)

2. 修正web.config配置

在原有配置基础上,添加ARR代理的头强制设置节点,确保Connection头被正确传递:

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://127.0.0.1:8888/{R:1}" />
                    <serverVariables>
                        <set name="HTTP_UPGRADE" value="{HTTP_UPGRADE}" />
                        <set name="HTTP_CONNECTION" value="upgrade" />
                        <set name="HTTP_HOST" value="{HTTP_HOST}" />
                    </serverVariables>
                </rule>
            </rules>
        </rewrite>
        <!-- 启用ARR代理并强制设置Connection头 -->
        <proxy enabled="true" preserveHostHeader="true">
            <headers>
                <add name="Connection" value="upgrade" />
            </headers>
        </proxy>
    </system.webServer>
</configuration>

3. 调整ARR全局代理设置

  • 回到IIS管理器的服务器节点,点击「Application Request Routing Cache」
  • 选择右侧「Server Proxy Settings」,勾选「Enable proxy」
  • 确保「Preserve client IP in the X-Forwarded-For header」和「Reverse rewrite host in response headers」选项已勾选,避免头信息被默认过滤或修改

4. 生效验证

  • 保存web.config后,执行iisreset命令重启IIS服务
  • 使用curl或Postman发送测试请求,例如:
    curl -H "Upgrade: websocket" http://你的IIS域名/测试路径
    
  • 检查后端服务器接收的请求头,确认Connection字段已被设置为upgrade

内容的提问来源于stack exchange,提问作者九条凛

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 00:05:07