简体   繁体   English

基于子域的IIS路由请求到不同端口

[英]IIS Route Request To Different Ports Based on Sub-domain

I have a domain, let's call it domain.com .我有一个域,我们称之为domain.com

On my DNS provider, I have configured two sub-domains to point to the same IP address.在我的 DNS 提供商上,我已将两个子域配置为指向同一个 IP 地址。 For example:例如:
sub1.domain.com => 185.146.11.17 sub1.domain.com => 185.146.11.17
sub2.domain.com => 185.146.11.17 sub2.domain.com => 185.146.11.17

I've got two sites configured in IIS listening on ports 8080 and 8081 respectively.我在 IIS 中配置了两个站点,分别侦听端口 8080 和 8081。

Now, as traffic is coming through directly on 185.146.11.17:80 , I need to route it to either port 8080 or port 8081 depending on the sub-domain of the request.现在,由于流量直接通过185.146.11.17:80 ,我需要根据请求的子域将其路由到端口8080或端口8081

I've read about rewrite rules and reverse proxies, but am totally confused as to how to simply achieve what I'm needing based on the sub-domain of the request.我已经阅读了有关重写规则和反向代理的内容,但是对于如何根据请求的子域简单地实现我所需要的内容感到非常困惑。

How does one go about this?如何解决这个问题?

After much digging, trial-and-error and pulled out hair, I've managed to solve the problem.经过多次挖掘、反复试验和拔出头发,我设法解决了这个问题。 It seems in this situation, IIS provides much of the heavy lifting for one and does not actually require any explicit reverse proxy and/or rewrite rules.在这种情况下,IIS 似乎为一个人提供了很多繁重的工作,实际上并不需要任何明确的反向代理和/或重写规则。

With the DNS set as below DNS设置如下
sub1.domain.com => 185.146.11.17 sub1.domain.com => 185.146.11.17
sub2.domain.com => 185.146.11.17 sub2.domain.com => 185.146.11.17

All one needs to do is:一个人需要做的就是:
1. Stop/remove default website on port 80 . 1. 停止/删除端口80上的默认网站。
2. Create new website with: 2. 创建新网站:
2.1. 2.1. Site name set to sub1.domain.com .站点名称设置为sub1.domain.com
2.2. 2.2. IP address set to All Unassigned and port set to 80 . IP 地址设置为All Unassigned ,端口设置为80
2.3. 2.3. Host name set to sub1.domain.com .主机名设置为sub1.domain.com
3. Repeat process with sub2.domain.com . 3. 对sub2.domain.com重复该过程。

Following the above steps and it all should magically work.按照上述步骤,这一切都应该神奇地工作。 The host names are very important so be sure not to miss those.主机名非常重要,所以一定不要错过这些。 I would recommend setting this all up for HTTPS and then creating a rule to redirect from HTTP if a request comes in on such a protocol.我建议为 HTTPS 设置这一切,然后创建一个规则,如果请求来自这样的协议,则从 HTTP 重定向。

Thanks to everyone for providing suggestions.感谢大家提供建议。

It sounds like you need to create an ARR load balance to itself.听起来您需要为自己创建一个 ARR 负载平衡。

Prerequisite先决条件

1.Application request routing and URL rewrite installed. 1.应用请求路由和URL重写安装。

2.Three DNS name, one for front-end and two for backend. 2.三个DNS名称,一个用于前端,两个用于后端。

在此处输入图片说明 在此处输入图片说明

3.Three website with domain: 3.三个带域名的网站:

Main Site主站点

在此处输入图片说明

SubSite1子站点 1

在此处输入图片说明

SubSite2子站点2

在此处输入图片说明

4.Create a web farm and add these domain to your web farm. 4.创建一个网络农场并将这些域添加到您的网络农场。 在此处输入图片说明

  1. IIS will create a Load balance routing rule "ARR_MyServer_loadbance" in Server node's URL rewrite, then add condition pattern for it. IIS 会在Server 节点的URL rewrite 中创建一个负载均衡路由规则“ARR_MyServer_loadbance”,然后为其添加条件模式。

在此处输入图片说明

 <globalRules>
                <rule name="ARR_MyServer_loadbalance" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="*" />
                    <action type="Rewrite" url="http://MyServer/{R:0}" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="www.candy.com" />
                    </conditions>
                </rule>
            </globalRules>

6.Go to configuration manager->web farm and set PreserveHostHeader to false. 6.转到配置管理器->网络农场并将PreserveHostHeader设置为false。 在此处输入图片说明 7.Go to your web farm->My Server monitoring and managent, ensure all server are healthy Now you are able to load balance for your main site. 7.转到您的网络农场->我的服务器监控和管理,确保所有服务器都健康现在您可以为您的主站点进行负载平衡。

在此处输入图片说明

Just remember to set HTTP port for each server when you are joining Servers into your web farm.当您将服务器加入您的网络场时,请记住为每个服务器设置 HTTP 端口。 在此处输入图片说明

Applicationhost.config should looks like: Applicationhost.config 应如下所示:

  <webFarm name="MyServer" enabled="true">
            <server address="domain1.candy.com" enabled="true">
                <applicationRequestRouting httpPort="8080" />
            </server>
            <server address="domain2.candy.com" enabled="true">
                <applicationRequestRouting httpPort="8081" />
            </server>
            <applicationRequestRouting>
                <healthCheck url="" />
                <protocol preserveHostHeader="false" />
            </applicationRequestRouting>
        </webFarm>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM