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

在子域名下创建顶级域名Cookie失败(Express部署于IIS反向代理后)

解决从子域名设置顶级域名Cookie的问题

你的核心问题是IIS反向代理在转发响应时,自动修改了Cookie的Domain属性,导致Express设置的.example.com被替换成了.sub.example.com。以下是针对性的排查和解决步骤:

1. 关闭ARR的响应Host重写功能

IIS的Application Request Routing(ARR)默认会重写响应头中的Host和Cookie Domain,把后端返回的域名替换成当前请求的子域名。解决方法:

  • 打开IIS管理器,选中服务器节点,点击Application Request Routing Cache
  • 选择右侧的Server Proxy Settings
  • 取消勾选Reverse rewrite host in response headers选项,保存配置

2. 添加IIS出站重写规则强制保留Cookie Domain

如果关闭ARR重写后问题仍存在,手动添加出站规则覆盖Cookie的Domain:
在你的web.config的<rewrite>节点下新增<outboundRules>配置:

<outboundRules>
    <rule name="Preserve Top-Level Cookie Domain" preCondition="HasSetCookie">
        <match serverVariable="RESPONSE_Set-Cookie" pattern="(domain=)\.sub\.example\.com" />
        <action type="Rewrite" value="{R:1}.example.com" />
    </rule>
    <preConditions>
        <preCondition name="HasSetCookie">
            <add input="{RESPONSE_Set-Cookie}" pattern=".*domain=.*" />
        </preCondition>
    </preConditions>
</outboundRules>

这个规则会将响应中Set-Cookie头里的domain=.sub.example.com替换为domain=.example.com,强制保留你需要的顶级域名。

3. 验证后端服务的Cookie输出

直接访问Node.js服务的本地地址(http://localhost:3528/set),查看响应头中的Set-Cookie是否包含domain=.example.com:

  • 如果本地响应正确,说明问题完全出在IIS代理环节,执行前两步即可解决
  • 如果本地响应也显示domain=.sub.example.com,检查Express代码是否有其他中间件(比如cookie-parser的额外配置)修改了Cookie属性

4. 确认IIS站点绑定与SSL配置

  • 确保example.com和sub.example.com的IIS绑定都配置了有效的SSL证书,且请求为HTTPS(因为你设置了secure: true,非HTTPS请求下浏览器会忽略该Cookie,但Postman不受此限制)
  • 检查IIS站点的HTTP响应头设置,确认没有额外的规则修改Cookie

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 08:15:01