| REST-vs-GraphQL :👈 | 👉:ConfigureServices and Configure methods |
All about Kestrel |
Kestrel in ASP.NET Core
Kestrel is the default, cross-platform web server that comes built-in with ASP.NET Core. Itβs lightweight, high-performance, and designed to run on Windows, Linux, and macOS without needing IIS or Apache in front of it.
1. What is Kestrel?
Kestrel is the built-in, cross-platform web server used by ASP.NET Core applications.
It is responsible for listening for incoming HTTP requests from clients such as browsers, mobile applications, Postman and other services. It processes the HTTP connection and passes the request into the ASP.NET Core request-processing pipeline.
In simple terms, Kestrel is the web server that receives HTTP requests and sends HTTP responses for an ASP.NET Core application.
For example, when a user opens:
https://example.com/api/products
Kestrel can receive the HTTP request, pass it through ASP.NET Core middleware and route it to the appropriate controller or endpoint.
Protocols Supported: HTTP/1.1, HTTP/2, HTTP/3, and WebSockets.
Deployment Modes:
- Standalone β Kestrel directly serves requests.
- Behind a Reverse Proxy β e.g., IIS, Nginx, Apache for SSL termination, load balancing, or extra security.
2. Why Do We Need Kestrel?
Consider an ASP.NET Core Web API application.
When a browser or mobile application sends a request, something must:
- Listen for incoming network connections.
- Accept HTTP requests.
- Read HTTP headers and request data.
- Make the request available to the ASP.NET Core application.
- Send the HTTP response back to the client.
- Handle multiple client connections efficiently.
Kestrel performs these web-server responsibilities.
Without a web server such as Kestrel, the ASP.NET Core application would not have a component to receive HTTP requests over a network.
π Why Kestrel Is Used in .NET Core
| Reason | Benefit |
|---|---|
| Cross-Platform | Runs on Windows, Linux, macOS β ideal for containerized and cloud deployments. |
| High Performance | Optimized for handling thousands of concurrent connections using async I/O. |
| Lightweight | Minimal overhead, great for microservices and APIs. |
| Security Hardened | Supports HTTPS/TLS and is resistant to common web server vulnerabilities. |
| Flexible | Works with Minimal APIs, MVC, Razor Pages, SignalR, Blazor, gRPC, and custom workloads. |
| Extensible | Can be customized via configuration, middleware, and custom transports. |
3. How Kestrel Works in ASP.NET Core
π§ How It Fits in ASP.NET Core
- Kestrel starts and binds to a port.
- It listens for HTTP requests from clients.
- Requests go through the middleware pipeline (auth, logging, routing, etc.).
- The app generates a response, which Kestrel sends back to the client.
In short: Kestrel is the engine that powers ASP.NET Core apps. Itβs fast, portable, and production-ready β whether you run it alone for simplicity or behind a reverse proxy for enterprise-grade deployments.
Consider a client requesting product information from an ASP.NET Core Web API.
Step-by-step execution:
- Client sends a request: A browser sends an HTTP request to the server.
- Kestrel receives the request: Kestrel listens on the configured endpoint and accepts the connection.
- Middleware processing: Kestrel passes the request into the ASP.NET Core middleware pipeline.
- Routing: ASP.NET Core identifies the matching endpoint.
- Authentication and authorization: The application verifies the user's identity and permissions when configured.
- Controller execution: The matching controller action or endpoint executes.
- Business logic: Services perform the required operations, such as retrieving product information.
- Response generation: ASP.NET Core creates the HTTP response.
- Kestrel sends the response: Kestrel writes the response back to the client.
4. Kestrel Architecture
Kestrel is designed to handle network connections and HTTP communication efficiently.
Its major responsibilities include:
4.1 Network Connection Handling
Kestrel listens on configured IP addresses and ports.
For example:
http://localhost:5000
https://localhost:5001
It accepts incoming TCP connections and handles the underlying network communication.
4.2 HTTP Protocol Support
Kestrel supports modern HTTP protocols, including:
- HTTP/1.1: The widely used HTTP protocol with persistent connections and request-response communication.
- HTTP/2: Supports multiplexing multiple streams over a single connection, header compression and other performance improvements.
- HTTP/3: Uses QUIC over UDP instead of TCP and provides modern connection and transport features.
Actual protocol availability depends on the operating system, TLS configuration and deployment environment.
4.3 Asynchronous Request Processing
Kestrel uses asynchronous I/O to handle network operations efficiently.
For example, when an application waits for a database response, asynchronous operations can allow a thread to perform other work instead of blocking while waiting for the database.
Example:
[HttpGet]
public async Task<IActionResult> GetProducts()
{
var products = await _productService.GetProductsAsync();
return Ok(products);
}
Here:
- The request is received by Kestrel.
- The controller invokes an asynchronous service operation.
- The application awaits the database-related operation.
- The request can resume when the asynchronous operation completes.
- The response is returned through Kestrel.
Important: Kestrel does not automatically make synchronous application code asynchronous. Developers must use asynchronous APIs appropriately.
4.4 HTTPS and TLS Support
Kestrel supports HTTPS and TLS encryption.
When HTTPS is configured, Kestrel can perform TLS negotiation and encrypt communication between the client and server.
Example:
https://localhost:5001
HTTPS protects information such as authentication tokens, passwords and API data while it travels across the network.
5. How to Configure Kestrel
Kestrel can be configured through application code, configuration files, environment variables and command-line arguments.
5.1 Default Kestrel Configuration
In a typical ASP.NET Core application, Kestrel is configured automatically when the application uses the standard web application builder.
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello from Kestrel!");
app.Run();
When you run the application, ASP.NET Core starts its configured web server, normally Kestrel.
Run:
dotnet run
You may see output similar to:
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
The actual port depends on the launch configuration and environment.
5.2 Configure Kestrel in Program.cs
You can explicitly configure the Kestrel listening endpoint.
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(options =>
{
options.ListenLocalhost(5000);
options.ListenLocalhost(5001, listenOptions =>
{
listenOptions.UseHttps();
});
});
var app = builder.Build();
app.MapGet("/", () => "Hello from Kestrel!");
app.Run();
Explanation:
ConfigureKestrel()provides access to Kestrel server configuration.ListenLocalhost(5000)configures an HTTP endpoint on localhost port 5000.ListenLocalhost(5001)configures another endpoint on localhost port 5001.UseHttps()enables HTTPS for the second endpoint using an available certificate configuration.
For production deployments, configure and manage TLS certificates properly rather than relying on development certificates.
5.3 Configure Kestrel in appsettings.json
You can configure endpoints in the application configuration file.
{
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:5000"
},
"Https": {
"Url": "https://localhost:5001"
}
}
}
}
ASP.NET Core can load this configuration when Kestrel is configured to use the application configuration.
Example:
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel((context, options) =>
{
options.Configure(context.Configuration.GetSection("Kestrel"));
});
var app = builder.Build();
app.MapGet("/", () => "Kestrel configuration example");
app.Run();
5.4 Configure Kestrel Using Environment Variables
Environment variables are useful when deploying applications across development, testing and production environments.
For example, the ASP.NET Core environment can be configured using:
ASPNETCORE_ENVIRONMENT=Production
Kestrel endpoint configuration can also be supplied using configuration keys such as:
Kestrel__Endpoints__Http__Url=http://0.0.0.0:5000
The double underscore (__) represents a configuration hierarchy separator.
6. What Is the Difference Between Kestrel and IIS?
This is one of the most common ASP.NET Core interview questions.
Kestrel is the built-in, cross-platform web server for ASP.NET Core.
IIS (Internet Information Services) is Microsoft's Windows web server and hosting platform.
| Feature | Kestrel | IIS |
|---|---|---|
| Type | Web server | Web server and hosting platform |
| Platform | Cross-platform | Windows |
| Performance | Designed for efficient HTTP processing | Provides web hosting and server management capabilities |
| ASP.NET Core hosting | Can host applications directly | Can host ASP.NET Core applications using the ASP.NET Core Module |
| Process management | Runs the application process when self-hosted | Can manage hosting through application pools and the ASP.NET Core Module |
| SSL/TLS | Supports HTTPS configuration | Provides IIS-managed HTTPS configuration |
| Typical usage | Self-hosted applications, containers and behind reverse proxies | Windows-based enterprise hosting |
7. Kestrel vs IIS vs Nginx
These technologies can perform related but different responsibilities.
| Feature | Kestrel | IIS | Nginx |
|---|---|---|---|
| Primary role | ASP.NET Core web server | Windows web server and hosting platform | Web server and reverse proxy |
| Platform | Cross-platform | Windows | Cross-platform |
| Reverse proxy | Not normally used as the external reverse proxy in a typical deployment | Can act as a reverse proxy | Commonly used as a reverse proxy |
| Direct ASP.NET Core hosting | Yes | Through ASP.NET Core Module and Kestrel in the common in-process/out-of-process hosting models | Normally proxies requests to Kestrel |
| Static file serving | Can serve static files through ASP.NET Core middleware | Supports static file serving | Supports static file serving |
8. What Is a Reverse Proxy and Why Is It Used with Kestrel?
A reverse proxy is a server that receives client requests and forwards them to an application server.
In production, Kestrel is often placed behind a reverse proxy such as:
- IIS.
- Nginx.
- Apache HTTP Server.
- Cloud load balancers or ingress controllers.
Example architecture:
Why use a reverse proxy?
- Centralized TLS termination.
- Load balancing across application instances.
- Request routing.
- Security and network isolation.
- Centralized logging and request management.
- Serving static files efficiently.
A reverse proxy is not mandatory for every ASP.NET Core deployment. Kestrel can be exposed directly when the hosting environment is appropriately configured and secured.
πΌ Kestrel in Reverse Proxy Mode
π How It Works
- Client Request: A browser, mobile app, or API client sends an HTTP/HTTPS request.
-
Reverse Proxy Layer (optional but common in production):
- IIS (Windows), Nginx (Linux), or Apache sits in front of Kestrel.
- Handles SSL/TLS termination (offloading encryption work).
- Can perform load balancing across multiple Kestrel instances.
- Adds extra security filtering and request buffering.
-
Kestrel Web Server:
- Listens on internal ports (e.g., localhost:5000 or unix:/tmp/kestrel.sock).
- Processes requests through the ASP.NET Core middleware pipeline.
- Routes to controllers, minimal APIs, Razor Pages, SignalR hubs, etc.
-
Application Logic:
- Executes business logic, queries databases, calls external APIs.
- Returns a response back through Kestrel β Reverse Proxy β Client.
π Why Use a Reverse Proxy with Kestrel
- Security: Hide Kestrel from direct internet exposure.
- Performance: Offload SSL/TLS and static file serving to a highly optimized proxy.
- Scalability: Distribute requests across multiple app instances.
- Compatibility: Integrate with existing enterprise infrastructure.
9. How Kestrel Works Behind IIS
ASP.NET Core applications can be hosted with IIS using the ASP.NET Core Module (ANCM).
There are two important hosting models.
9.1 In-Process Hosting
In in-process hosting, the ASP.NET Core application runs inside the IIS worker process (w3wp.exe).
The ASP.NET Core Module loads the application into the IIS process, and IIS HTTP Server handles the requests. Kestrel is not used in this hosting model.
9.2 Out-of-Process Hosting
In out-of-process hosting, the ASP.NET Core application runs in a separate process, commonly dotnet.exe or the application's executable.
IIS receives the external request and forwards it through the ASP.NET Core Module to Kestrel.
Important interview point: Kestrel is used in the common out-of-process IIS hosting model, but not in the in-process hosting model.
10. Kestrel in Docker and Kubernetes
Kestrel is commonly used when deploying ASP.NET Core applications in containers.
Example Dockerfile:
FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS runtime
WORKDIR /app
EXPOSE 8080
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
COPY . .
RUN dotnet publish -c Release -o /app/publish
FROM runtime AS final
WORKDIR /app
COPY --from=build /app/publish .
ENTRYPOINT ["dotnet", "MyApi.dll"]
In a container, Kestrel can listen on the configured container port.
For example:
ASPNETCORE_HTTP_PORTS=8080
In Kubernetes, traffic commonly follows this architecture:
Multiple application pods can run independently, each hosting its own Kestrel instance.
11. Kestrel Performance and Limits
Kestrel is designed for high-performance HTTP request processing. However, application performance depends on the complete system, not only the web server.
Important configuration areas include:
11.1 Connection Limits
You can configure limits on connections and other server resources.
builder.WebHost.ConfigureKestrel(options =>
{
options.Limits.MaxConcurrentConnections = 100;
});
This example limits the number of concurrent connections to 100. Choose limits based on expected traffic, resource capacity and workload characteristics.
11.2 Request Body Size
You can configure the maximum request body size.
builder.WebHost.ConfigureKestrel(options =>
{
options.Limits.MaxRequestBodySize = 10 * 1024 * 1024;
});
This example sets a 10 MB maximum request body size for requests governed by this limit.
11.3 Request Timeouts
Kestrel supports request timeout configuration to help protect applications from requests that take too long.
Timeout values should be chosen according to the application's workload. Long-running operations may require different timeout policies.
11.4 Keep Application Code Efficient
Kestrel cannot compensate for inefficient application code.
Follow these practices:
- Use asynchronous I/O for database and network operations.
- Avoid unnecessary database queries.
- Use caching where appropriate.
- Use pagination for large API responses.
- Avoid blocking calls such as unnecessary
.Resultand.Wait(). - Monitor memory usage and CPU consumption.
- Use proper logging and distributed tracing.
12. Kestrel Security Best Practices
- Use HTTPS in production.
- Configure valid TLS certificates.
- Keep the .NET runtime and ASP.NET Core packages updated.
- Set appropriate request-size and timeout limits.
- Use authentication and authorization where required.
- Configure trusted proxies correctly when running behind a reverse proxy.
- Do not trust forwarded headers from arbitrary clients.
- Avoid exposing internal application ports unnecessarily.
- Use appropriate network firewall rules.
- Monitor server logs and application health.
13. Common Misconceptions About Kestrel
- Kestrel is a framework: Incorrect. It is the web server used by ASP.NET Core.
- Kestrel only works on Windows: Incorrect. It is cross-platform.
- Kestrel always requires IIS: Incorrect. It can host ASP.NET Core applications directly.
- Kestrel is always used when hosting behind IIS: Incorrect. The common out-of-process model uses Kestrel, while in-process hosting does not.
- Kestrel automatically improves application performance: Incorrect. Application code, database queries, network latency and infrastructure also affect performance.
- Kestrel is a reverse proxy: Incorrect. It is primarily an application web server.
14. Interview Questions and Answers
Q1. What is Kestrel in ASP.NET Core?
Kestrel is the built-in, cross-platform web server for ASP.NET Core. It listens for HTTP requests, processes network communication and passes requests into the ASP.NET Core application pipeline.
Q2. Why does ASP.NET Core use Kestrel?
Kestrel provides an efficient, cross-platform web server that can host ASP.NET Core applications directly or work behind reverse proxies such as IIS and Nginx.
Q3. Is Kestrel a web server or an application server?
Kestrel is a web server. It handles HTTP connections and communication for ASP.NET Core applications.
Q4. Can Kestrel run without IIS?
Yes. Kestrel can host ASP.NET Core applications directly on Windows, Linux and macOS, including in containers.
Q5. What is the difference between Kestrel and IIS?
Kestrel is a cross-platform web server built for ASP.NET Core. IIS is a Windows web server and hosting platform that provides additional hosting and management capabilities.
Q6. Why do we use Nginx in front of Kestrel?
Nginx can act as a reverse proxy, handle TLS termination, route requests and distribute traffic across multiple application instances.
Q7. Does Kestrel support HTTPS?
Yes. Kestrel supports HTTPS using TLS certificates configured for its listening endpoints.
Q8. What is the difference between in-process and out-of-process IIS hosting?
In-process hosting runs the ASP.NET Core application inside the IIS worker process without Kestrel. Out-of-process hosting runs the application in a separate process and IIS forwards requests to Kestrel through the ASP.NET Core Module.
Q9. Does Kestrel support HTTP/2 and HTTP/3?
Yes. Kestrel supports HTTP/2 and HTTP/3, subject to operating-system, transport and TLS configuration requirements.
Q10. Can Kestrel handle multiple requests simultaneously?
Yes. Kestrel is designed to handle concurrent connections and requests using asynchronous network I/O and efficient resource management.
Q11. Is Kestrel suitable for production?
Yes. Kestrel is suitable for production deployments. It can be exposed directly with appropriate security configuration or hosted behind a reverse proxy or load balancer.
Q12. What happens when a request reaches Kestrel?
Kestrel accepts the connection, processes the HTTP request and passes it into the ASP.NET Core middleware pipeline. The application processes the request and generates a response, which Kestrel sends back to the client.
15. Final Summary
- Kestrel is the built-in, cross-platform web server for ASP.NET Core.
- It receives HTTP requests and sends HTTP responses.
- It supports HTTP/1.1, HTTP/2 and HTTP/3.
- It supports HTTPS and TLS.
- It can run independently without IIS.
- It is commonly used behind IIS, Nginx and cloud load balancers.
- It is widely used in Docker and Kubernetes deployments.
- It is used in the out-of-process IIS hosting model, but not in the in-process hosting model.
Remember: Kestrel is the web server that allows an ASP.NET Core application to receive and respond to HTTP requests.
π‘ Pro Tip for .NET Core APIs
For development, you can run Kestrel standalone. For production, Microsoft recommends Kestrel behind a reverse proxy β especially if you need SSL termination, multiple app instances, or advanced routing.
Official Microsoft Documentation
| REST-vs-GraphQL :👈 | 👉:ConfigureServices and Configure methods |