Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Kestrel ใน C# ASP.NET Core คืออะไร

Kestrel - Web Server Cross-Platform สำหรับ ASP.NET Core

Kestrel เป็น Web Server แบบ Cross-platform ที่สร้างขึ้นสำหรับ ASP.NET Core รองรับทุก Platform ที่ .NET Core รองรับ (Windows, Linux, macOS)

คุณสมบัติหลัก

  • Built-in - รวมอยู่ใน ASP.NET Core โดยค่าเริ่มต้น
  • High Performance - อิงจาก libuv (เก่า) / System.IO.Pipelines + Socket (ใหม่) เร็วมาก
  • Cross-Platform - รันได้ Windows, Linux, macOS เหมือนกัน
  • HTTP/1.1, HTTP/2, HTTP/3 (QUIC) - รองรับ Protocol ทันสมัย
  • Self-hosted - รันใน Process เดียวกับแอป (dotnet.exe)

โหมดการโฮสต์ (Hosting Models)

โหมดคำอธิบายKestrel Role
Out-of-Process (Default ก่อน 2.2)IIS เป็น Reverse Proxy หน้า KestrelInternal Server (Port สุ่ม)
In-Process (Default 2.2+)รันใน IIS Worker Process (w3wp.exe)ไม่ได้ใช้ (ใช้ IIS HTTP Server แทน)
Standalone / Self-hostedรันโดยตรงด้วย dotnet run หรือ DockerEdge Server** (Internet-facing)
Reverse Proxy (Nginx/Apache/IIS)Proxy อยู่หน้า KestrelInternal Server

การใช้งาน Standalone (Development / Container)

// Program.cs - ค่าเริ่มต้นของ ASP.NET Core Template
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello Kestrel!");

app.Run();  // เริ่ม Kestrel

รันด้วย CLI:

dotnet run

Output:

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:5000
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:5001

การตั้งค่า Kestrel (Configuration)

1. ผ่าน appsettings.json

{
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "https://localhost:5000"
      },
      "Https": {
        "Url": "https://localhost:5001",
        "Certificate": {
          "Path": "cert.pfx",
          "Password": "password"
        }
      }
    },
    "Limits": {
      "MaxRequestBodySize": 10485760,  // 10MB
      "MaxConcurrentConnections": 1000,
      "MaxConcurrentUpgradedConnections": 1000,
      "KeepAliveTimeout": "00:02:00",
      "RequestHeadersTimeout": "00:00:30"
    }
  }
}

2. ผ่าน Code (Program.cs)

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureKestrel(options =>
{
    // Listen ทุก IP Port 8080
    options.ListenAnyIP(8080);
    
    // Listen เฉพาะ Localhost Port 5001 HTTPS
    options.ListenLocalhost(5001, listenOptions =>
    {
        listenOptions.UseHttps("cert.pfx", "password");
    });
    
    // Unix Socket (Linux)
    options.ListenUnixSocket("/tmp/kestrel.sock");
    
    // Limits
    options.Limits.MaxRequestBodySize = 10 * 1024 * 1024; // 10MB
    options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2);
});

var app = builder.Build();
app.Run();

Kestrel กับ Reverse Proxy (Production Best Practice)

💡 แนวทางปฏิบัติ Production: ควรใช้ Reverse Proxy (Nginx, Apache, IIS, Traefik, Caddy) หน้า Kestrel เสมอ

เหตุผลที่ต้องใช้ Reverse Proxy

  • Security - Kestrel ไม่ได้ Harden ไว้สำหรับ Internet-facing โดยตรง
  • SSL Termination - Proxy จัดการ Certificate/HTTPS แล้วส่ง HTTP ให้ Kestrel
  • Static Files - Proxy เสิร์ฟ Static Files เร็วกว่า
  • Compression - Gzip/Brotli ที่ Proxy
  • Load Balancing - กระจาย Request ไปหลาย Instance
  • Port 80/443 - Binding พอร์ตต่ำต้อง Root/Admin Proxy ช่วยได้

ตัวอย่าง Nginx Config

server {
    listen 80;
    server_name example.com;
    
    location / {
        proxy_pass https://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

กระบวนการ (Process) ของ Kestrel

  • Standalone: Process = dotnet.exe (หรือ MyApp.exe ถ้า Publish Single-file)
  • In-Process IIS: Process = w3wp.exe (Kestrel ไม่ได้รัน)
  • Out-of-Process IIS: Process = dotnet.exe (Kestrel) + w3wp.exe (IIS)

Kestrel vs IIS vs HTTP.sys

ServerPlatformPerformanceUse Case
KestrelCross-platformสูงมากDefault, Container, Linux, Self-host
IIS HTTP ServerWindowsสูงIn-Process on IIS
HTTP.sysWindowsสูงWindows Auth, Port Sharing, Kernel-mode

สรุป

  • Kestrel = Web Server เริ่มต้นของ ASP.NET Core Cross-platform เร็ว
  • รันใน Process เดียวกับแอป (dotnet.exe)
  • Production ควรใช้ Reverse Proxy (Nginx/IIS/Apache) หน้า Kestrel
  • ตั้งค่าผ่าน appsettings.json หรือ code ใน ConfigureKestrel
  • รองรับ HTTP/1.1, HTTP/2, HTTP/3, Unix Sockets, TLS