ASP.NET Core全面认识Web服务器Kestrel
WEB前端开发社区 昨天
HTTPS Opaque upgrade used to enable WebSockets(启用WebSocket情况下的不透明升级) Unix sockets for high performance behind Nginx(Nginx高性能模式下的Unix套接字) HTTP2(不支持macOS)
对外暴露有限的HTTP服务 更加安全,反向代理服务器做了一层过滤、防护和转发 通过反向代理服务器实现负载均衡和动态请求分发路由 减少域名使用,降低WAF防火墙防护成本 安全通信 (HTTPS) 配置,HTTPS转HTTP,仅反向代理服务器需要 X.509 证书,并且该服务器可使用普通 HTTP 协议与内部网络的应用服务器通信。
Windows Server 2016/Windows 10 或更高版本 具有 OpenSSL 1.0.2 或更高版本的 Linux(例如,Ubuntu 16.04 或更高版本) macOS 的未来版本将支持 HTTP/2
public class Program{ public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });}
internal static void ConfigureWebDefaults(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration((ctx, cb) =>
{
if (ctx.HostingEnvironment.IsDevelopment())
{
StaticWebAssetsLoader.UseStaticWebAssets(ctx.HostingEnvironment, ctx.Configuration);
}
});
builder.UseKestrel((builderContext, options) =>
{
options.Configure(builderContext.Configuration.GetSection("Kestrel"));
})
.ConfigureServices((hostingContext, services) =>
{
// Fallback
services.PostConfigure<HostFilteringOptions>(options =>
{
if (options.AllowedHosts == null || options.AllowedHosts.Count == 0)
{
// "AllowedHosts": "localhost;127.0.0.1;[::1]"
var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
// Fall back to "*" to disable.
options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" });
}
});
// Change notification
services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>(
new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));
services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();
if (string.Equals("true", hostingContext.Configuration["ForwardedHeaders_Enabled"], StringComparison.OrdinalIgnoreCase))
{
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
// Only loopback proxies are allowed by default. Clear that restriction because forwarders are
// being enabled by explicit configuration.
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
services.AddTransient<IStartupFilter, ForwardedHeadersStartupFilter>();
}
services.AddRouting();
})
.UseIIS()
.UseIISIntegration();
}
serverOptions.Limits.KeepAliveTimeout =TimeSpan.FromMinutes(2);
serverOptions.Limits.MaxConcurrentConnections = 100;serverOptions.Limits.MaxConcurrentUpgradedConnections = 100;
serverOptions.Limits.MaxRequestBodySize = 10 * 1024;
[RequestSizeLimit(100000000)]public IActionResult MyActionMethod()
serverOptions.Limits.MinRequestBodyDataRate = new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));serverOptions.Limits.MinResponseDataRate = new MinDataRate(bytesPerSecond: 100, gracePeriod: TimeSpan.FromSeconds(10));
serverOptions.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(1);
serverOptions.Limits.Http2.MaxStreamsPerConnection = 100;
serverOptions.Limits.Http2.HeaderTableSize = 4096;
serverOptions.Limits.Http2.MaxFrameSize = 16384;
serverOptions.Limits.Http2.MaxRequestHeaderFieldSize = 8192;
serverOptions.Limits.Http2.InitialConnectionWindowSize = 131072;
serverOptions.Limits.Http2.InitialStreamWindowSize = 98304;
serverOptions.AllowSynchronousIO = true;
不看的原因确定内容质量低不看此公众号
赞 (0)