ASP.NET Core 中的網頁伺服器實作Web server implementations in ASP.NET Core

Tom DykstraSteve SmithStephen HalterChris Ross 提供By Tom Dykstra, Steve Smith, Stephen Halter, and Chris Ross

ASP.NET Core 應用程式執行時,需使用內含式 HTTP 伺服器實作。An ASP.NET Core app runs with an in-process HTTP server implementation. 伺服器實作會接聽 HTTP 要求,並以組成 HttpContext要求功能集合形式,將它們呈現給應用程式。The server implementation listens for HTTP requests and surfaces them to the app as sets of request features composed into an HttpContext.

ASP.NET Core 提供兩個伺服器實作:ASP.NET Core ships two server implementations:

KestrelKestrel

Kestrel 是內含於 ASP.NET Core 專案範本中的預設網頁伺服器。Kestrel is the default web server included in ASP.NET Core project templates.

您可以單獨使用 Kestrel,或與 IIS、Nginx 或 Apache 等「反向 Proxy 伺服器」搭配使用。Kestrel can be used by itself or with a reverse proxy server, such as IIS, Nginx, or Apache. 反向 Proxy 伺服器會從網際網路接收 HTTP 要求,並在進行一些初步處理後,將其轉送至 Kestrel。A reverse proxy server receives HTTP requests from the Internet and forwards them to Kestrel after some preliminary handling.

Kestrel 不使用反向 Proxy 伺服器直接與網際網路通訊

Kestrel 透過 IIS、Nginx 或 Apache 等反向 Proxy 伺服器間接與網際網路通訊

不論設定是否具有反向 Proxy 伺服器,對於 ASP.NET Core 2.0 或更新版本的應用程式,其中之一都是有效且支援的裝載設定。Either configuration—with or without a reverse proxy server—is a valid and supported hosting configuration for ASP.NET Core 2.0 or later apps. 如需詳細資訊,請參閱何時搭配使用 Kestrel 與反向 ProxyFor more information, see When to use Kestrel with a reverse proxy.

您無法在沒有 Kestrel 或自訂伺服器實作的情況下使用 IIS、Nginx 或 Apache。IIS, Nginx, and Apache can't be used without Kestrel or a custom server implementation. ASP.NET Core 是設計用來在自己的處理序中執行,使其行為可在平台之間保持一致。ASP.NET Core was designed to run in its own process so that it can behave consistently across platforms. IIS、Nginx 和 Apache 會聽寫自己的啟動程序和環境。IIS, Nginx, and Apache dictate their own startup procedure and environment. 若要直接使用這些伺服器技術,ASP.NET Core 必須適應每一部伺服器的需求。To use these server technologies directly, ASP.NET Core would need to adapt to the requirements of each server. 使用 Kestrel 等網頁伺服器實作,可讓 ASP.NET Core 裝載在不同的伺服器技術上時,控制啟動處理序和環境。Using a web server implementation, such as Kestrel, ASP.NET Core has control over the startup process and environment when hosted on different server technologies.

IIS 與 KestrelIIS with Kestrel

當您使用 IISIIS Express 作為 ASP.NET Core 的反向 Proxy 時,ASP.NET Core 應用程式會在與 IIS 工作者處理序不同的處理序中執行。When using IIS or IIS Express as a reverse proxy for ASP.NET Core, the ASP.NET Core app runs in a process separate from the IIS worker process. 在 IIS 處理序中,ASP.NET Core 模組會協調反向 Proxy 關聯性。In the IIS process, the ASP.NET Core Module coordinates the reverse proxy relationship. ASP.NET Core Module 的主要功能是要啟動 ASP.NET Core 應用程式、在損毀時進行重新啟動應用程式,以及將 HTTP 流量轉送到應用程式中。The primary functions of the ASP.NET Core Module are to start the ASP.NET Core app, restart the app when it crashes, and forward HTTP traffic to the app. 如需詳細資訊,請參閱 ASP.NET Core 模組For more information, see ASP.NET Core Module.

Nginx 與 KestrelNginx with Kestrel

如需如何在 Linux 上使用 Nginx 作為 Kestrel 之反向 Proxy 伺服器的資訊,請參閱 Linux 上使用 Nginx 的主機For information on how to use Nginx on Linux as a reverse proxy server for Kestrel, see Host on Linux with Nginx.

Apache 與 KestrelApache with Kestrel

如需如何在 Linux 上使用 Apache 作為 Kestrel 之反向 Proxy 伺服器的資訊,請參閱 Linux 上使用 Apache 的主機For information on how to use Apache on Linux as a reverse proxy server for Kestrel, see Host on Linux with Apache.

HTTP.sysHTTP.sys

如果您在 Windows 上執行 ASP.NET Core 應用程式,則 HTTP.sys 是 Kestrel 的替代方案。If ASP.NET Core apps are run on Windows, HTTP.sys is an alternative to Kestrel. 通常建議使用 Kestrel 以達到最佳效能。Kestrel is generally recommended for best performance. HTTP.sys 可以用於下列情況:應用程式公開到網際網路,且必要功能是由 HTTP.sys 而非 Kestrel 支援。HTTP.sys can be used in scenarios where the app is exposed to the Internet and required features are supported by HTTP.sys but not Kestrel. 如需 HTTP.sys 功能的資訊,請參閱 HTTP.sysFor information on HTTP.sys features, see HTTP.sys.

HTTP.sys 直接與網際網路通訊

HTTP.sys 也可用於只公開到內部網路的應用程式。HTTP.sys can also be used for apps that are only exposed to an internal network.

HTTP.sys 直接與內部網路通訊

ASP.NET Core 伺服器基礎結構ASP.NET Core server infrastructure

可在 Startup.Configure 方法中使用的 IApplicationBuilder 會公開 IFeatureCollection 類型的 ServerFeatures 屬性。The IApplicationBuilder available in the Startup.Configure method exposes the ServerFeatures property of type IFeatureCollection. Kestrel 和 HTTP.sys (ASP.NET Core 1.x 中的 WebListener) 只會個別公開單一功能,IServerAddressesFeature,但不同的伺服器實作可能會公開其他的功能。Kestrel and HTTP.sys (WebListener in ASP.NET Core 1.x) only expose a single feature each, IServerAddressesFeature, but different server implementations may expose additional functionality.

IServerAddressesFeature 可用來找出伺服器實作在執行階段已繫結的連接埠。IServerAddressesFeature can be used to find out which port the server implementation has bound at runtime.

自訂伺服器Custom servers

如果內建伺服器不符合應用程式的需求,則可以建立自訂伺服器實作。If the built-in servers don't meet the app's requirements, a custom server implementation can be created. Open Web Interface for .NET (OWIN) 指南示範如何撰寫以 Nowin 為基礎的 IServer 實作。The Open Web Interface for .NET (OWIN) guide demonstrates how to write a Nowin-based IServer implementation. 只有應用程式使用的功能介面需要實作,但至少必須支援 IHttpRequestFeatureIHttpResponseFeatureOnly the feature interfaces that the app uses require implementation, though at a minimum IHttpRequestFeature and IHttpResponseFeature must be supported.

伺服器啟動Server startup

使用 Visual StudioVisual Studio for MacVisual Studio Code 時,若應用程式是由整合式開發環境 (IDE) 啟動,就會啟動伺服器。When using Visual Studio, Visual Studio for Mac, or Visual Studio Code, the server is launched when the app is started by the Integrated Development Environment (IDE). 在 Visual Studio on Windows 中,您可以使用啟動設定檔搭配 IIS Express/ASP.NET Core 模組 或主控台來啟動應用程式和伺服器。In Visual Studio on Windows, launch profiles can be used to start the app and server with either IIS Express/ASP.NET Core Module or the console. 在 Visual Studio Code 中,應用程式和伺服器是由 Omnisharp 啟動,可啟動 CoreCLR 偵錯工具。In Visual Studio Code, the app and server are started by Omnisharp, which activates the CoreCLR debugger. 若使用 Visual Studio for Mac,則應用程式和伺服器會由 Mono Soft-Mode 偵錯工具啟動。Using Visual Studio for Mac, the app and server are started by the Mono Soft-Mode Debugger.

當您在專案資料夾中使用命令提示字元啟動應用程式時,dotnet run 會啟動應用程式和伺服器 (僅限 Kestrel 和 HTTP.sys)。When launching an app from a command prompt in the project's folder, dotnet run launches the app and server (Kestrel and HTTP.sys only). 組態是由 -c|--configuration 選項指定,會設為 Debug (預設值) 或 ReleaseThe configuration is specified by the -c|--configuration option, which is set to either Debug (default) or Release. 如果 launchSettings.json 檔案中出現啟動設定檔,請使用 --launch-profile <NAME> 選項來設定啟動設定檔 (例如,DevelopmentProduction)。If launch profiles are present in a launchSettings.json file, use the --launch-profile <NAME> option to set the launch profile (for example, Development or Production). 如需詳細資訊,請參閱 dotnet run.NET Core 發佈封裝主題。For more information, see the dotnet run and .NET Core distribution packaging topics.

其他資源Additional resources