如何托管来自同一服务器的 Web 应用程序和 API,同时将它们分开? [英] How do I host a web application and an API from the same server while keeping them in separate?

查看:16
本文介绍了如何托管来自同一服务器的 Web 应用程序和 API,同时将它们分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有 2 个独立的应用程序,一个 Web Api 应用程序和一个 MVC 应用程序,它们都是用 .NET 4.5 编写的.如果您要在 IIS 中的主机标头https://www.mymvcapp.com/"下托管 MVC 应用程序,它会是可以在 IIS 中的主机头https://www.mymvcapp.com/api/ 下单独托管 Web Api 应用程序?

Let's say we have 2 separate applications, a Web Api application and a MVC application both written in .NET 4.5. If you were to host the MVC application in IIS under the host header "https://www.mymvcapp.com/" would it be possible to host the Web Api application separately in IIS under the host header "https://www.mymvcapp.com/api/"?

在 IIS 中运行 2 个应用程序的进程需要分开.我知道托管、自托管和使用 IIS 托管的不同方法.如果可能的话,我想使用 IIS.

The processes running the 2 applications in IIS need to be separate. I know of the separate methods of hosting, self hosting and hosting using IIS. I would like to use IIS if at all possible.

此外,如果每个应用程序都在单独的服务器上,我将如何托管两个应用程序(一个 API 和一个 Web 应用程序),以便我可以从 http://www.mymvcapp.com/api?

Also, how would I host two applications (an API and a web application) if each were on a separate server so that I could serve the api from http://www.mymvcapp.com/api?

推荐答案

至少有 4 种方法可以做你想做的事.前两种方法适用于如果您有 1 个 Web 服务器,并且这两个应用程序都由运行 IIS 的那个 Web 服务器提供服务.如果您有多个 Web 服务器在负载平衡器后面运行,则此方法也适用,只要 API 和网站在同一台服务器上运行即可.

There are at least 4 ways of doing what you want to do. The first two methods are for if you have 1 web server, and both applications are served from that one web server running IIS. This method also works if you have multiple web servers running behind a load-balancer, so long as the API and the Web site are running on the same server.

后两种方法使用所谓的反向代理",本质上是一种根据您接收的流量类型将流量从一个服务器(代理服务器)路由到多个内部服务器的方法.这适用于当您在一组服务器上运行 Web 服务器并在另一组服务器上运行 API 时.你可以使用任何你想要的反向代理软件,我提到 nginx 和 HAProxy 因为我过去都用过.

The second two methods are using what's called a "Reverse Proxy", essentially a way to route traffic from one server (the proxy server) to multiple internal servers depending on what type of traffic you're receiving. This is for when you run your web servers on a set of servers and run your API on a different set of servers. You can use any reverse proxy software you want, I mention nginx and HAProxy because I've used both in the past.

在 IIS 中有两种方法可以做到:

There are two ways to do it in IIS:

如果你的物理文件夹结构如下:

If your physical folder structure is as follows:

c:sitesmymvcapp
c:sitesmymvcappapi

您可以执行以下操作:

创建子应用程序将允许您的API"可从 www.mymvcapp.com/api 访问该站点,无需任何路由更改.

Creating a child application will allow your "API" site to be reachable from www.mymvcapp.com/api, without any routing changes needed.

这样做:

  • 打开 IIS 管理器
  • 单击站点"中的相应站点左侧的文件夹树
  • 右键单击API 文件夹
  • 点击转换为应用程序"

缺点是所有子应用程序都继承其父应用程序的 Web 配置,如果您在其中设置冲突,您会看到一些运行时异常(如果它可以正常工作).

The downside is that all Child Applications inherit the web config of their parent, and if you have conflicting settings in there, you'll see some runtime weirdness (if it works at all).

第二种方式是让应用程序保持独立性的一种方式;同样,您无需进行任何路由.

The second way is a way to do it so that the applications maintain their separateness; and again you don't have to do any routing.

假设两个文件夹结构:

c:sitesapi
c:sitesmvcapp

您可以在 Windows 中设置 Junction.从命令行*:

You can set up Junctions in Windows. From the command line*:

cd c:sites
mklink /D /J mymvcapp c:sitesmvcapp
cd mymvcapp
mklink /D /J api c:sitesapi

然后进入 IIS 管理器,并将两者都转换为应用程序.这样,API 将在 api 中可用,但实际上不会与父级共享其 web.config 设置.

Then go into IIS Manager, and convert both to applications. This way, the API will be available in api, but not actually share its web.config settings with the parent.

如果您使用 nginx 或 haproxy 作为反向代理,您可以将其设置为将调用路由到每个应用程序.

If you use nginx or haproxy as a reverse proxy, you can set it up to route calls to each app depending.

在您的 nginx.conf 中(最佳做法是创建一个 sites-enabled conf,它是 sites-available 的符号链接,并且您可以在部署时销毁该符号链接)执行以下操作:

In your nginx.conf (best practice is to create a sites-enabled conf that's a symlink to sites-available, and you can destroy that symlink whenever deploying) do the following:

location / {
    proxy_pass http://mymvcapp.com:80
}
location /api {
    proxy_pass http://mymvcapp.com:81
}

然后您需要设置正确的 IIS 设置,让每个站点侦听端口 80 (mymvcapp) 和端口 81 (api).

and then you'd set the correct IIS settings to have each site listen on ports 80 (mymvcapp) and ports 81 (api).

acl acl_WEB hdr_beg(host) -i mymvcapp.com
acl acl_API path_beg -i /api

use_backend API if acl_API
use_backend WEB if acl_WEB

backend API
server web mymvcapp.com:81

backend WEB
server web mymvcapp.com:80


*我从内存中发出 Junction 命令;我几个月前这样做了,但不是最近,所以如果命令有问题,请告诉我


*I'm issuing the Junction command from memory; I did this a few months ago, but not recently, so let me know if there are issues with the command

注意:配置文件并不是完整的配置文件——仅用于显示反向代理所需的设置.根据您的环境,您可能需要设置其他设置.

NB: the config files are not meant to be complete config files -- only to show the settings necessary for reverse proxying. Depending on your environment there may be other settings you need to set.

这篇关于如何托管来自同一服务器的 Web 应用程序和 API,同时将它们分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆