Lesson 6 of 8
Kestrel Configuration & HTTP/2
Kestrel is ASP.NET Core's built-in, cross-platform web server - the fastest .NET server option. In production it's typically run behind Nginx or an Azure Load Balancer as a reverse proxy, but understanding Kestrel configuration enables fine-tuning for performance and security.
Kestrel Production Configuration
// appsettings.json - Kestrel endpoint configuration
{
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://0.0.0.0:5000"
},
"Https": {
"Url": "https://0.0.0.0:5001",
"Certificate": {
"Path": "/etc/ssl/certs/myapp.pfx",
"Password": ""
}
}
},
"Limits": {
"MaxConcurrentConnections": 5000,
"MaxRequestBodySize": 10485760,
"KeepAliveTimeout": "00:02:00",
"RequestHeadersTimeout": "00:00:30"
}
}
}
// In Program.cs - programmatic Kestrel configuration
builder.WebHost.ConfigureKestrel(options =>
{
// HTTP/2 for API + gRPC performance
options.ConfigureEndpointDefaults(lo => lo.Protocols = HttpProtocols.Http1AndHttp2);
// Hardened limits for production
options.Limits.MaxRequestBodySize = 10 * 1024 * 1024; // 10MB
options.Limits.MaxRequestHeaderCount = 100;
options.Limits.MaxRequestHeadersTotalSize = 32 * 1024; // 32KB
options.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30);
options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2);
options.Limits.MinRequestBodyDataRate = new MinDataRate(
bytesPerSecond: 100, // minimum 100 bytes/sec (rejects slow-read attacks)
gracePeriod: TimeSpan.FromSeconds(10));
});
// ━━ Behind Nginx: trust forwarded headers ━━
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
options.KnownNetworks.Clear(); // only needed if IP allowlist required
options.KnownProxies.Clear();
});
// In middleware pipeline (BEFORE everything else):
app.UseForwardedHeaders();Tip
Tip
Practice Kestrel Configuration HTTP2 in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
.NET provides a unified platform for building various application types
Practice Task
Note
Practice Task - (1) Write a working example of Kestrel Configuration HTTP2 from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Kestrel Configuration HTTP2 is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready dotnet code.
Finished this lesson?