Nguon: Microsoft Learn · .NET 8.0
Kestrel web server trong ASP.NET Core
Kestrel là một web server đa nền tảng (cross-platform) cho ASP.NET Core. Kestrel là server được khuyến nghị cho ASP.NET Core và được cấu hình mặc định trong các project template của ASP.NET Core.
Các tính năng của Kestrel bao gồm:
- Cross-platform (đa nền tảng): Kestrel là web server đa nền tảng chạy trên Windows, Linux và macOS.
- High performance (hiệu suất cao): Kestrel được tối ưu hóa để xử lý nhiều kết nối đồng thời một cách hiệu quả.
- Lightweight (nhẹ): Tối ưu hóa để chạy trong môi trường hạn chế tài nguyên như container và thiết bị edge.
- Security hardened (bảo mật vững chắc): Kestrel hỗ trợ HTTPS và được tôi luyện chống các lỗ hổng web server.
- Hỗ trợ giao thức rộng: Kestrel hỗ trợ các giao thức web phổ biến, bao gồm:
- HTTP/1.1, HTTP/2 và HTTP/3
- WebSockets
- Tích hợp với ASP.NET Core: Tích hợp liền mạch với các thành phần ASP.NET Core khác như middleware pipeline, dependency injection và hệ thống cấu hình.
- Flexible workloads (linh hoạt): Kestrel hỗ trợ nhiều workload:
- Các app framework ASP.NET như Minimal APIs, MVC, Razor pages, SignalR, Blazor và gRPC.
- Xây dựng reverse proxy với YARP.
- Extensibility (mở rộng): Tùy chỉnh Kestrel thông qua configuration, middleware và custom transports.
- Performance diagnostics (chẩn đoán hiệu suất): Kestrel cung cấp các tính năng chẩn đoán hiệu suất tích hợp như logging và metrics.
Bắt đầu
Các project template ASP.NET Core sử dụng Kestrel mặc định khi không được hosted với IIS. Trong Program.cs được tạo từ template, phương thức WebApplication.CreateBuilder gọi UseKestrel nội bộ:
csharp
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();Để biết thêm về cấu hình WebApplication và WebApplicationBuilder, xem Minimal APIs quick reference.
Hành vi khi đính kèm debugger
Các timeout và rate limit sau đây không được thực thi khi có debugger được đính kèm vào process Kestrel:
KestrelServerLimits.KeepAliveTimeoutKestrelServerLimits.RequestHeadersTimeoutKestrelServerLimits.MinRequestBodyDataRateKestrelServerLimits.MinResponseDataRateIConnectionTimeoutFeatureIHttpMinRequestBodyDataRateFeatureIHttpMinResponseDataRateFeature
Tài nguyên bổ sung
- Configure endpoints for Kestrel web server
- Configure options for the ASP.NET Core Kestrel web server
- Use HTTP/2 with the ASP.NET Core Kestrel web server
- When to use a reverse proxy with the ASP.NET Core Kestrel web server
- Host filtering with ASP.NET Core Kestrel web server
- Troubleshoot and debug ASP.NET Core projects
- Enforce HTTPS in ASP.NET Core
- Configure ASP.NET Core to work with proxy servers and load balancers
- RFC 9110: HTTP Semantics (Section 7.2: Host and :authority)
- Khi sử dụng UNIX sockets trên Linux, socket không tự động bị xóa khi ứng dụng tắt. Xem GitHub issue này.