.Net Core 3.1
.Net Core 3.1 启动后的server为server: Kestrel
,修改Program.cs
去除server:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
// 删除Header中的server
webBuilder.ConfigureKestrel(opt =>
{
opt.AddServerHeader = false;
});
webBuilder.UseStartup<Startup>();
});
ConfigureKestrel、UseStartup、UseKestrel上下顺序效果一样。
或者:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
// 删除Header中的server
webBuilder.UseKestrel(opt =>
{
opt.AddServerHeader = false;
});
});
IIS
部署到IIS时,IIS会添加server
和X-Powered-By
头信息。若要移除这两个头信息,需要修改Web.config
:
<configuration>
<system.webServer>
<security>
<requestFiltering removeServerHeader="true" />
</security>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
Apache
部署到Apache时,启动后的server为server: Apache/2.4.46 (Win32) OpenSSL/1.1.1g mod_fcgid/2.3.9a
。Apache无法彻底删除server头,但是可以去掉Apache的版本信息,修改Apache的配置文件httpd.conf
,添加以下两行:
ServerTokens Prod
ServerSignature Off
修改后server为server: Apache
。
Nginx
修改nginx.conf
:
移除server版本信息,http节点下添加:
server_tokens off;
或者移除server,http节点下添加:
more_clear_headers Server;
修改server
如果就是不想显示server: Apache
,可以修改Startup.cs
,设置server头信息,这样Apache就不会覆盖server头信息了:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
app.Use(async (context, next) =>
{
context.Response.Headers["Server"] = "";
await next();
});
// ...
}
修改后server为server:
。