Kestrel 服务器 是默认的跨平台 HTTP 服务器实现。
Kestrel自承载:Kestrel Web 服务器无需任何其他外部 Web 服务器(如 IIS 或 HTTP.sys)即可运行。
最近出现.NET
平台中Asp.Net Core
被曝CVE-2025-55315漏洞,可以查看,安全评定危害等级为9.9
,算是史诗级漏洞,修复方式除了升级安全补丁版本,除了升级runtime
或 sdk
版本外,和可以考虑将服务升级为http/2
,直接改变报文的服务组合方式.官方只维护更新NET10
、NET9
、NET8
。低版本用.Net Framework
(不建议)、增加前置Web
服务器例如IIS、Nginx
过滤请求头部或者请求使用http/2
、Kestrel
启用Https
以及配置Http/2
,以下是主要讲解使用nginx
以及Kestrel
启用https
方式,对于安装runtime
和sdk
就不再累述,可以直接查看github
上的讨论内容或者看其他网友的文章
前置准备
创建证书
本地可以使用dotnet dev-certs 创建证书,也可以使用OpenSSL
创建。但是需要生成.pem
与.key
私钥,文件尾缀无所谓,主要是文件格式得对上。
创建目录ssl
存放证书。
mkdir ssl
cd ssl
不添加证书密码设置参数-np
即可(推荐方法)。
dotnet dev-certs https --format pem -ep ./localhost.pem -np
A valid HTTPS certificate is already present.
生成证书pem
格式证书文件,设置-p <自定义证书密码>
,就需要在加载证书时,输入证书密码(不推荐),对于nginx
在Windows
中运行存在问题。
dotnet dev-certs https --format pem -ep ./localhost.pem -p <自定义证书密码>
A valid HTTPS certificate is already present.
生成文件结果如下:
本地演示项目
模拟的服务站点此处使用一个新建项目ASP.NET Core Web API 空白项目
。
创建一个目录ssl
存储证书,同时设置文件属性为始终复制或者较新复制。
目录结构如下:
│ appsettings.Development.json
│ appsettings.json
│ Program.cs
│ WeatherForecast.cs
│ WebApp.csproj
│ WebApp.csproj.user
│ WebApp.http
├─Properties
│ launchSettings.json
└─ssllocalhost.keylocalhost.pem
调整Program.cs
调整默认代码中注释http
重定向到https
中间件。
//app.UseHttpsRedirection();
调试加载配置Properties/launchSettings.json
内容如下。
{"http": {"commandName": "Project","dotnetRunMessages": true,"launchBrowser": false,"applicationUrl": "http://localhost:5100;","environmentVariables": {"ASPNETCORE_ENVIRONMENT": "Development"}}
}
服务访问设置为5100
,运行访问天气接口http://localhost:5100
。
Nginx
配置Https
与Http/2
Nginx
版本要求必须在1.9.5
以上,如此才能支持HTTP/2
。
查看版本
nginx -V
nginx version: nginx/1.25.3
配置证书
server {listen 5443 ssl http2;server_name localhost;ssl_certificate ssl/localhost.pem;ssl_certificate_key ssl/localhost.key;ssl_session_cache shared:SSL:1m;ssl_session_timeout 5m;ssl_ciphers HIGH:!aNULL:!MD5;ssl_prefer_server_ciphers on;location / {proxy_pass http://localhost:5100;}
}
验证配置
如果提示[emerg] the "http2" parameter requires ngx_http_v2_module
,则表示当前nginx
编译过程中并未包含ngx_http_v2_module
模块,需要重新编译,编译过程不在此处细说,网上查找资料;或者使用openrestry
,这是nginx
的一个国人分支并加入lua
脚本等灵活支持,同样选择1.25.3
版本。
>nginx -t
nginx: [warn] the "listen ... http2" directive is deprecated, use the "http2" directive instead in ./conf/nginx.conf:111
nginx: the configuration file ./conf/nginx.conf syntax is ok
nginx: configuration file ./conf/nginx.conf test is successful
如果带验证的证书输入密码错误会输出如下结果,Windows
不建议设置密码:
>nginx -t
nginx: [warn] the "listen ... http2" directive is deprecated, use the "http2" directive instead in ./conf/nginx.conf:111
Enter PEM pass phrase:
nginx: [emerg] cannot load certificate key "./conf/ssl/localhost.key": PEM_read_bio_PrivateKey() failed (SSL: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt error:23077074:PKCS12 routines:PKCS12_pbe_crypt:pkcs12 cipherfinal error error:2306A075:PKCS12 routines:PKCS12_item_decrypt_d2i:pkcs12 pbe crypt error error:0907B00D:PEM routines:PEM_read_bio_PrivateKey:ASN1 lib)
nginx: configuration file ./conf/nginx.conf test failed
启动服务
在Windows
中使用start
启动nginx
。
start nginx
查看端口和进程。
>netstat -ano|findstr 5443TCP 0.0.0.0:5443 0.0.0.0:0 LISTENING 24072
>taskkill |findstr 24072
nginx.exe 24072 Console 1 11,260 K
验证服务
浏览器访问https://localhost:5443/weatherforecast
,结果如下,请求协议为h2
表示配置成功。
Kestrel
配置Https
与Http/2
这里只讲解通过配置文件进行配置的方式,通过代码的方式可以查看官方文档。
需要注意的是Properties/launchSettings.json
中配置端口不能与Kestrel
节点端口一样。
修改launchSettings.json
修改Properties/launchSettings.json
配置,服务端口不能与Kestrel
节点端口冲突。
{"$schema": "https://json.schemastore.org/launchsettings.json","profiles": {"http": {"commandName": "Project","dotnetRunMessages": true,"launchBrowser": false,"applicationUrl": "http://localhost:5000", // 改为其他端口而不是5100"environmentVariables": {"ASPNETCORE_ENVIRONMENT": "Development"}}}
}
添加Kestrel
节点
配置appsettings.Development.json
或者 appsettings.json
,添加Kestrel
节点,端口为5100
并且使用https
。
{"Kestrel": {"Endpoints": {"HTTPS": {"Url": "https://localhost:5100","SslProtocols": [ "Tls12", "Tls13" ],"Protocols": "Http2"}},"Certificates": {"Default": {"Path": "ssl/localhost.pem", "KeyPath": "ssl/localhost.key"}}}
}
验证服务
运行项目,可以看到已经访问https
。
访问https://localhost:5100/weatherforecast
,执行结果如下,表示配置成功: