部署程序

因为 ASP.NET Core 程序能够运行在 Windows、Mac 以及 Linux 上,你拥有多种部署程序的方式。这一章里,我将教给你最常用(也是最简单)的上线方式。

ASP.NET Core 通常会部署到下列环境之一:

  • 一部 Dockers 主机 任何有能力托管 Docker 容器的机器都能用来托管 ASP.NET Core 程序。创建 Docker 镜像是个非常快捷的部署程序的方式,尤其是在你熟悉 Docker 的情况下。(如果你还不熟悉,别担心!我会在后面逐步介绍。)

  • Azure 微软的 Azure 对 ASP.NET Core 程序提供原生的支持。如果你有一个 Azure 订阅,你只要创建一个 Web App 并上传你的项目文件即可。下一节,我会介绍通过 Azure CLI 完成这种操作。

  • Winddows 你可以在 Windows 上使用 IIS 网络服务器托管 ASP.NET Core 程序。一般来说,部署到 Azure 更容易(也更便宜),不过你要是愿意自己管理 Windows 服务器,这也是个可行的方案。

ASP.NET Core 里包含一个名为 Kestrel 的快速轻量级的 web 开发服务器。你每次运行 并浏览 的时候,用的就是这个服务器。当你把程序部署到生产环境的时候,它仍会在幕后使用 Kestrel。但强烈建议你在 Kestrel 之前添加一个反向代理,因为 Kestrel 并不具有负载均衡和其它更成熟的 Web 服务器所具有的其它特性。

在 Linux(和 Docker 容器)里,你可以用 Nginx 或者 Apache web 服务器接收从互联网上传入的请求,并派发到你用 Kestrel 托管的程序。如果你用的是 Windows,IIS 也能处理这个工作。

如果你把程序托管在 Azure,这些就都是自动为你处理的。在 Docker 那一节,我会讲述如何配置 Nginx 进行反向代理。


Deploy the application

You’ve come a long way, but you’re not quite done yet. Once you’ve created a great application, you need to share it with the world!

Because ASP.NET Core applications can run on Windows, Mac, or Linux, there are a number of different ways you can deploy your application. In this chapter, I’ll show you the most common (and easiest) ways to go live.

ASP.NET Core applications are typically deployed to one of these environments:

  • Azure. Microsoft Azure has native support for ASP.NET Core applications. If you have an Azure subscription, you just need to create a Web App and upload your project files. I’ll cover how to do this with the Azure CLI in the next section.

  • Windows. You can use the IIS web server on Windows to host ASP.NET Core applications. It’s usually easier (and cheaper) to just deploy to Azure, but if you prefer managing Windows servers yourself, it’ll work just fine.

If you don’t care about the guts of hosting ASP.NET Core applications and just want the step-by-step instructions, feel free to skip to one of the next two sections.

ASP.NET Core includes a fast, lightweight web server called Kestrel. It’s the server you’ve been using every time you ran and browsed to . When you deploy your application to a production environment, it’ll still use Kestrel behind the scenes. However, it’s recommended that you put a reverse proxy in front of Kestrel, because Kestrel doesn’t yet have load balancing and other features that more mature web servers have.

On Linux (and in Docker containers), you can use Nginx or the Apache web server to receive incoming requests from the internet and route them to your application hosted with Kestrel. If you’re on Windows, IIS does the same thing.