问题描述
让我们说我们有2个独立的应用程序,一个Web API应用程序和一个MVC应用程序都写在.NET 4.5。如果你是主机头下举办IIS中的MVC应用程序会是可能单独承载Web API应用在IIS主机头下
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 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服务器和应用程序都从一个Web服务器运行IIS服务。如果你运行一个负载平衡器后面的多个Web服务器,只要API和Web站点在同一台服务器上运行时,此方法也适用。
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:\sites\mymvcapp
c:\sites\mymvcapp\api
您可以执行以下操作:
创建一个子应用程序可以让你的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:\sites\api
c:\sites\mvcapp
您可以在Windows中设置了路口。在命令行*:
You can set up Junctions in Windows. From the command line*:
cd c:\sites
mklink /D /J mymvcapp c:\sites\mvcapp
cd mymvcapp
mklink /D /J api c:\sites\api
然后进入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(最好的做法是创建一个启用的站点 -
CONF这是一个符号链接站点可用
,每当部署可以摧毁符号链接),做到以下几点:
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
*我发出从内存交界处命令;我做这在几个月前,但最近没有,所以让我知道,如果有与命令发出
*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 :该配置文件并不意味着是完整的配置文件 - 只显示必要的反向代理的设置。根据您的环境可能有其他的设置,你需要设置。
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,同时保持它们分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!