简体   繁体   English

如何在同一 URL 下托管多个 .NET Core 应用程序?

[英]How to host multiple .NET Core apps under the same URL?

I am building a few web sites in ASP.NET Core (multiple user interface applications and a WebAPI app).我正在 ASP.NET Core 中构建一些网站(多个用户界面应用程序和一个 WebAPI 应用程序)。 They all work together, utilising the WebAPI.它们都使用 WebAPI 协同工作。 For the purpose of my question we'll call them App1, App2 and App3.就我的问题而言,我们将它们称为 App1、App2 和 App3。 I am hosting my websites in IIS.我在 IIS 中托管我的网站。

Typically, in the old .NET Framework (4.5.1), I would have one website in IIS, with multiple web apps to host my multiple applications.通常,在旧的 .NET Framework (4.5.1) 中,我会在 IIS 中拥有一个网站,其中包含多个 Web 应用程序来托管我的多个应用程序。 Like so:像这样:

  • WebSite1 - running on port 443 WebSite1 - 在端口 443 上运行
    • App1应用程序1

    • App2应用程序2

    • App3应用程序3

If the website (WebSite1) runs on port 443 (using the SSL cert), that means all apps are accessible via one domain url as follows:如果网站 (WebSite1) 在端口 443 上运行(使用 SSL 证书),这意味着所有应用程序都可以通过一个域 url 访问,如下所示:

With different app names at the end of the URL to identify them.在 URL 末尾使用不同的应用程序名称来标识它们。

When hosting the new ASP.NET Core applications, I host in IIS as well, and following the documentation for ASP.NET Core web site deployment, I host each .NET Core web app in a different site within IIS.在托管新的 ASP.NET Core 应用程序时,我也在 IIS 中托管,并按照 ASP.NET Core 网站部署文档,将每个 .NET Core Web 应用程序托管在 IIS 内的不同站点中。

As I have one domain name for all my sites, for example, https://www.example.com (I'm using an SSL cert), I have to give the other websites different ports.由于我的所有网站都有一个域名,例如https://www.example.com (我使用的是 SSL 证书),因此我必须为其他网站提供不同的端口。 So I end up with something like this:所以我最终得到了这样的东西:

  • WebSite1 - running on port 443 WebSite1 - 在端口 443 上运行

  • WebSite2 - running on port 444 WebSite2 - 在端口 444 上运行

  • WebSite3 - running on port 445 WebSite3 - 在端口 445 上运行

All of these apps are then accessible via these domain name URLs:然后可以通过这些域名 URL 访问所有这些应用程序:

How do I host ASP.NET Core applications all under one website as different "Applications", meaning they resolve to the same domain name ( https://www.example.com and the forward slash app name identifies which app we want)?我如何将 ASP.NET Core 应用程序作为不同的“应用程序”托管在一个网站下,这意味着它们解析为相同的域名( https://www.example.com并且正斜杠应用程序名称标识我们想要的应用程序)? OR alternatively, how do I take what I have configured now with the ports and route them all so App1 is default for example.com/, App 2 is example.com/app2/, and App 3 is example.com/app3/?或者,我如何使用我现在配置的端口并将它们全部路由,以便 App1 是 example.com/ 的默认值,App 2 是 example.com/app2/,App 3 是 example.com/app3/?

Am I looking at this .NET Core deployment completely the wrong way?我是否完全错误地看待这个 .NET Core 部署?

You could use a reverse proxy for serving multiple ASP Net Core application on the same domain.您可以使用反向代理为同一域上的多个 ASP Net Core 应用程序提供服务。
Using IIS I'm not quite sure but you need to install URL Rewrite and then follow the documentation provided by Microsoft (also this one should be helpful).使用 IIS 我不太确定,但您需要安装 URL Rewrite,然后按照 Microsoft 提供的文档进行操作( 也应该会有所帮助)。
You can also use nginx using location and proxy_pass as follow:您还可以使用locationproxy_pass使用nginx ,如下所示:

...
Some nginx Configuration 
location /App1 {
        proxy_pass http://127.0.0.1:443;
    }
location /App2 {
        proxy_pass http://127.0.0.1:444;
    }
location /App3 {
        proxy_pass http://127.0.0.1:445;
    }
Other configurations...

And then, each time you want add another ASP Net application to your domain you'll need to add another location, run the application on a different point and restart nginx.然后,每次您想向域中添加另一个 ASP Net 应用程序时,您都需要添加另一个位置,在不同的点上运行该应用程序并重新启动 nginx。

I got this working using the advice for Sub-applications here: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.2#sub-applications .我使用这里的子应用程序建议得到了这个工作: https : //docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view= aspnetcore-2.2#sub-applications。

Here's the meaty part, in case the link dies:这是重要的部分,以防链接失效:

To host an ASP.NET Core app as a sub-app under another ASP.NET Core app:要将 ASP.NET Core 应用作为另一个 ASP.NET Core 应用下的子应用托管:

1.Establish an app pool for the sub-app. 1.为子应用建立应用池。 Set the .NET CLR Version to No Managed Code because the Core Common Language Runtime (CoreCLR) for .NET Core is booted to host the app in the worker process, not the desktop CLR (.NET CLR).将 .NET CLR 版本设置为无托管代码,因为 .NET Core 的核心公共语言运行时 (CoreCLR) 被启动以在工作进程中托管应用程序,而不是桌面 CLR (.NET CLR)。

2.Add the root site in IIS Manager with the sub-app in a folder under the root site. 2.在IIS管理器中添加根站点,在根站点下的文件夹中添加子应用程序。

3.Right-click the sub-app folder in IIS Manager and select Convert to Application. 3.右键单击IIS管理器中的子应用程序文件夹并选择转换为应用程序。

4.In the Add Application dialog, use the Select button for the Application Pool to assign the app pool that you created for the sub-app. 4.在“添加应用程序”对话框中,使用“应用程序池”的“选择”按钮分配您为子应用程序创建的应用程序池。 Select OK.选择确定。

As a bonus, my main application is legacy Web Forms and my sub-application is exciting new .NET Core functionality.作为奖励,我的主要应用程序是旧版 Web 窗体,而我的子应用程序是令人兴奋的新 .NET Core 功能。

You don't need to create a site for each app, you can host it as before, as web applications inside one site.您不需要为每个应用程序创建一个站点,您可以像以前一样将其托管为一个站点内的 Web 应用程序。 The only thing you need to do is select unmanaged app pool for your .Net Core applications, that's it.您唯一需要做的就是为您的 .Net Core 应用程序选择非托管应用程序池,就是这样。 So this works: https://domain/app1 and https://domain/app2 and you only need 1 certificate for a web server所以这是有效的: https://domain/app1https://domain/app2并且您只需要 1 个网络服务器证书

Alternatively, you can create site for each app, then you need a sub-domain for each app, like https://app1.domain and like https://app2.domain In this case all sub-domains must point to the same IP (if your server only have one) and you need to set up bindings for each site\\app to it's domain.或者,您可以为每个应用程序创建站点,然后您需要为每个应用程序创建一个子域,例如https://app1.domainhttps://app2.domain在这种情况下,所有子域都必须指向相同的IP(如果您的服务器只有一个)并且您需要为每个站点\\应用程序设置与其域的绑定。 You should bind all sites to the same port (443 or 80).您应该将所有站点绑定到同一个端口(443 或 80)。

For SSL in multiple sites configuration if you have IIS 7 you need to have a wildcard certificate.对于多站点配置中的 SSL,如果您有 IIS 7,您需要有一个通配符证书。 In IIS 8 and above you can use Server name indication feature and have a separate certificate for each site, however I recommend you to use wildcard certificate as well since SNI is new and might not be supported by some browsers在 IIS 8 及更高版本中,您可以使用服务器名称指示功能并为每个站点使用单独的证书,但是我建议您也使用通配符证书,因为 SNI 是新的,某些浏览器可能不支持

In both cases you need to set up a Windows Server Hosting from here Microsoft's download center在这两种情况下,您都需要从这里Microsoft 的下载中心设置 Windows Server Hosting

Suppose your main web directory is :假设您的主 Web 目录是:

mainweb\

Make sure you have a valid ASP.NET application running in your App1 subfolder:确保在 App1 子文件夹中运行有效的 ASP.NET 应用程序:

mainweb\App1\

Now install your dotnet core 2 or 3 app in the App1\\ folder with all the dependencies etc.现在在 App1\\ 文件夹中安装您的 dotnet core 2 或 3 应用程序以及所有依赖项等。

Next, drop this web.config in that same folder:接下来,将此 web.config 放在同一个文件夹中:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\App1.exe" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="outofprocess" />
    </system.webServer>
  </location>
</configuration>

Now suppose you want another dotnet core web api named App2\\ running under mainweb also:现在假设您还希望在 mainweb 下运行另一个名为 App2\\ 的 dotnet 核心 Web api:

  1. Just create the new folder mainweb\\App2\\只需创建新文件夹 mainweb\\App2\\
  2. Create a new asp.net application in that folder via IIS or your special web hosting app.通过 IIS 或您的特殊网络托管应用程序在该文件夹中创建一个新的 asp.net 应用程序。
  3. drop the web.config above and change the one line for the process path to .\\App2.exe删除上面的 web.config 并将进程路径的一行更改为 .\\App2.exe

When the mainweb ASP.NET application starts it will start the dotnet core apps also.当 mainweb ASP.NET 应用程序启动时,它也会启动 dotnet 核心应用程序。 All good and they run under your mainweb as :一切都很好,它们在您的 mainweb 下运行为:

https://mainweb.com/App1/WeatherForecast/ // WeatherForecast is controller name

https://mainweb.com/App2/<ApiControllerName>/

For IIS, you can use Application Request Routing ( ARR ) which combine with URL rewrite module to routing request into each the association between URL port.对于 IIS,您可以使用应用程序请求路由 ( ARR ),它结合URL 重写模块将请求路由到每个 URL 端口之间的关联中。 It is used to build a similar proxy like Nginx and point to an appropriate port following each pattern of the URL.它用于构建类似 Nginx 的代理,并根据 URL 的每个模式指向适当的端口。 You can see a sample with ARR below:您可以在下面看到带有 ARR 的示例:

https://www.iis.net/learn/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing https://www.iis.net/learn/extensions/url-rewrite-module/reverse-proxy-with-url-rewrite-v2-and-application-request-routing

试试看:在文件 Web.config 中,每个应用程序都有不同的名称: <add name="herenewappname" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在同一主机和端口下运行多个应用程序 - Running multiple apps under same host and port 如何检查URL是否在同一主机上 - How to check if a url on same host or not ASP.net-从共享同一接口的不同URL /主机连接到多个Web服务? - ASP.net - connect to multiple Web Services from different URL's/host that share the same interface? 在同一路由URL下具有多个站点的ASP.net MVC路由 - ASP.net mvc routing with multiple sites under the same route url 如何使用 .NET CORE DI 为同一接口注册多个实现 - How to register multiple implementation for same interface using .NET CORE DI .Net Core如何多次注册同一个接口? - How to register same interface multiple times in .Net Core? 如何在 Asp.Net Core 中注册同一接口的多个实现? - How to register multiple implementations of the same interface in Asp.Net Core? 如何为 web 主机和 .NET 核心中的托管服务注入相同的依赖项 object? - How to inject same dependency object for both a web host and a hosted service in .NET Core? .NET 核心重定向到 Controller 在同一解决方案下的另一个项目中 - .NET Core Redirect to Controller in another Project under same Solution 如何在.net core中重写URL? - How to rewrite URL in .net core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM