ASP.NET Core Tutorialprovides basic and advanced concepts of C# for beginners and professionals.

Kestrel in ASP.NET Core

Back to: ASP.NET Core Tutorial

Kestrel in ASP.NET Core

Kestrel is the cross-platform web server included and used by default in ASP.NET Core applications. It's lightweight, fast, and designed to handle both development and production environments.


What is Kestrel?

Kestrel is:

  • A web server that listens for HTTP requests and forwards them to your ASP.NET Core application.

  • Cross-platform and built on top of libuv (older versions) or Sockets (newer versions).

  • The default web server in ASP.NET Core since version 1.0.


ASP.NET Core Hosting Model with Kestrel


+-------------------+ | Browser | +--------+----------+ | v +------------+-------------+ | Kestrel Web Server | +------------+-------------+ | v +------------+-------------+ | Middleware Components | +------------+-------------+ | v +------------+-------------+ | Controllers / Endpoints | +--------------------------+


Kestrel vs IIS/Nginx/Apache

Feature Kestrel IIS/Nginx/Apache
Type Application Web Server Reverse Proxy / Full Web Server
Cross-platform ✅ Yes ❌ IIS, ✅ Nginx/Apache
Static file handling ✅ But basic ✅ Optimized
TLS/HTTPS support ✅ Yes ✅ Yes
Reverse Proxy Required? Optional (Prod recommended) Not needed (they are full servers)
Use Case ASP.NET Core apps General purpose + ASP.NET Core

In production, it's recommended to use Kestrel behind a reverse proxy (like IIS or Nginx) for enhanced security, load balancing, and better handling of TLS, logs, and static files.


Configuring Kestrel

You can configure Kestrel in Program.cs (or Startup.cs in older versions):

Basic Example (ASP.NET Core 6+ / 7+):


csharp
var builder = WebApplication.CreateBuilder(args);
// Configure Kestrel options
builder.WebHost.ConfigureKestrel(serverOptions => { serverOptions.Limits.MaxRequestBodySize = 10 * 1024;// 10 KB
serverOptions.ListenAnyIP(5000);// HTTPserverOptions.ListenLocalhost(5001, listenOptions =>
{ listenOptions.UseHttps();
// HTTPS});});
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();


HTTPS with Kestrel

Kestrel supports HTTPS with certificates. You can configure it in appsettings.json:


json
{ "Kestrel": { "Endpoints": { "Https": { "Url": "https://localhost:5001", "Certificate": { "Path": "certs/localhost.pfx", "Password": "yourpassword" } } } } }

Or configure it programmatically:


builder.WebHost.ConfigureKestrel(
options => { options.Listen(IPAddress.Any, 5001, listenOptions => { listenOptions.UseHttps("path/to/cert.pfx", "password"); }); }
);


Performance

Kestrel is high-performance and asynchronous, designed to handle thousands of concurrent connections. Microsoft benchmarks show Kestrel to be among the fastest web servers available.


Features and Limits in Kestrel

You can set various limits:


csharp
options.Limits.MaxRequestBodySize = 10 * 1024;
// Max body size
options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2);
options.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30);


When to Use Kestrel Directly

  • For microservices or containerized applications in Docker/Kubernetes.

  • When you're running apps behind a load balancer or API Gateway.

  • For development and testing environments.


Summary

Feature Kestrel
Type Web server for ASP.NET Core
Cross-platform ✅ Yes
Default server ✅ Yes (since ASP.NET Core 1.0)
Reverse proxy support ✅ Recommended in production
HTTPS support ✅ Yes
Performance 🔥 High-performance, async I/O
Scroll to Top