You need to enable JavaScript to run this app. IIS8环境下如何移除400、500等错误响应中的Server响应头
优惠活动
大模型
产品
解决方案
定价
更多

IIS8环境下如何移除400、500等错误响应中的Server响应头

全状态码移除IIS Server响应头可行方案

方案1:IIS 10.0.1709及以上版本(原生支持,优先选用)

该版本及更新的IIS原生提供了全局移除Server头的配置,对所有HTTP状态码的响应都生效,不需要额外工具,直接修改站点web.config即可:

<configuration>
  <system.webServer>
    <security>
      <requestFiltering removeServerHeader="true" />
    </security>
  </system.webServer>
</configuration>

配置完成后重启IIS站点生效,所有响应的Server头都会被完全移除。

方案2:低版本IIS(IIS7/8/8.5)使用URL Rewrite出站规则

如果你使用的是不支持上述原生配置的低版本IIS,可以通过URL Rewrite的出站规则覆盖所有响应的Server头,规则会匹配所有状态码的响应,只要存在Server头就将值清空:
首先确保已经安装了URL Rewrite模块,之后在web.config中添加如下配置:

<configuration>
  <system.webServer>
    <rewrite>
      <outboundRules>
        <rule name="移除所有响应的Server头" preCondition="匹配存在Server头的响应" patternSyntax="Wildcard">
          <match serverVariable="RESPONSE_SERVER" pattern="*" />
          <action type="Rewrite" value="" />
        </rule>
        <preConditions>
          <preCondition name="匹配存在Server头的响应" logicalGrouping="MatchAll">
            <add input="{RESPONSE_SERVER}" pattern=".+" />
          </preCondition>
        </preConditions>
      </outboundRules>
    </rewrite>
  </system.webServer>
</configuration>

配置后重启站点,不管是200正常响应还是302、400、500等错误响应,Server头都会被清空。

方案3:自定义HTTP模块(兜底方案,适配所有IIS版本)

如果上述两种方案都不适用,可以通过开发自定义HTTP模块,在所有响应发送到客户端前强制移除Server头,步骤如下:

  1. 新建一个类继承IHttpModule,实现如下逻辑:
using System;
using System.Web;

public class RemoveServerHeaderModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PreSendRequestHeaders += OnPreSendRequestHeaders;
    }

    public void Dispose() { }

    void OnPreSendRequestHeaders(object sender, EventArgs e)
    {
        HttpContext.Current.Response.Headers.Remove("Server");
    }
}
  1. 将编译后的dll放到站点bin目录,然后在web.config中注册模块:
<configuration>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="RemoveServerHeaderModule" type="RemoveServerHeaderModule" />
    </modules>
  </system.webServer>
</configuration>

配套移除其他冗余头

你示例中出现的X-Powered-By: ASP.NET头也可以同步移除,添加如下配置即可:

<configuration>
  <system.web>
    <!-- 移除ASP.NET版本标识 -->
    <httpRuntime enableVersionHeader="false" />
  </system.web>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <!-- 移除X-Powered-By头 -->
        <remove name="X-Powered-By" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 02:06:03