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

时间:2019-12-25 10:26:32

标签: iis reverse-proxy url-rewrite-module

我有一个域,我们称它为domain.com

在我的DNS提供程序上,我已经配置了两个子域以指向相同的IP地址。例如:
sub1.domain.com => 185.146.11.17
sub2.domain.com => 185.146.11.17

我在IIS中配置了两个站点,分别侦听端口8080和8081。

现在,由于流量直接来自185.146.11.17:80,因此我需要根据请求的子域将其路由到端口8080或端口8081

我已经阅读了有关重写规则和反向代理的内容,但是对于如何根据请求的子域简单地实现我所需要的内容却完全感到困惑。

该如何处理?

2 个答案:

答案 0 :(得分:0)

听起来您需要为其自身创建ARR负载平衡。

先决条件

1。安装了应用程序请求路由和URL重写。

2。三个DNS名称,一个用于前端,两个用于后端。

enter image description here enter image description here

3。三个具有域名的网站:

主站点

enter image description here

SubSite1

enter image description here

SubSite2

enter image description here

4。创建一个Web场并将这些域添加到您的Web场中。 enter image description here

  1. IIS将在服务器节点的URL重写中创建一个负载平衡路由规则“ ARR_MyServer_loadbance”,然后为其添加条件模式。

enter image description here

 <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。转到配置管理器-> Web场并将PreserveHostHeader设置为false。 enter image description here 7.转到您的Web场->我的服务器监视和代理,确保所有服务器都正常 现在,您可以为主站点进行负载平衡了。

enter image description here

仅记得在将服务器加入Web场时为每个服务器设置HTTP端口。 enter image description here

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>

答案 1 :(得分:0)

经过大量的挖掘,反复试验并拔出头发,我设法解决了这个问题。在这种情况下,IIS似乎为它提供了很多繁重的工作,并且实际上并不需要任何显式的反向代理和/或重写规则。

DNS设置如下
sub1.domain.com => 185.146.11.17
sub2.domain.com => 185.146.11.17

所有需要做的是:
1.停止/删除端口80上的默认网站。
2.创建新网站,网址为:
2.1。网站名称设置为sub1.domain.com
2.2。 IP地址设置为All Unassigned,端口设置为80
2.3。主机名设置为sub1.domain.com
3.使用sub2.domain.com重复该过程。

按照上述步骤操作,一切都应该神奇。主机名非常重要,因此请确保不要错过这些主机名。我建议将其全部设置为HTTPS,然后创建一个规则,以便在这样的协议上出现请求时从HTTP重定向。

感谢大家提供建议。