Web 套接字服务器动态端口的 IIS 反向代理



正如你在下面看到的,我有 2 条规则,它们基本上对 websocket 服务器做同样的事情。是否有可能将这 2 条规则组合在一个通用规则中,同时允许在不添加更多规则的情况下添加更多 websocket 服务器。类似于"wss://somerandomdomain.net:{PORT}(.*(" => "http://localhost:{PORT}/{R:1}" ?

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<outboundRules>
<rule name="ReverseProxyOutboundRule1" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^http(s)?://localhost:8080/(.*)" />
<action type="Rewrite" value="http{R:1}://www.videolounge.net/{R:2}" />
</rule>
<preConditions>
<preCondition name="ResponseIsHtml1">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
</outboundRules>
<rules>
<rule name="Web Socket Reverse" enabled="true" stopProcessing="true">
<match url="wss://somerandomdomain.net:35000(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
</conditions>
<action type="Rewrite" url="http://localhost:35000/{R:1}" />
</rule>
<rule name="Web Socket Reverse 2" enabled="true" stopProcessing="true">
<match url="wss://somerandomdomain.net:35001(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
</conditions>
<action type="Rewrite" url="http://localhost:35001/{R:1}" />
</rule>
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost:8080/{R:1}" />
</rule>
</rules>
</rewrite>
<urlCompression doStaticCompression="false" doDynamicCompression="false" />
</system.webServer>
</configuration>

我认为<match url="wss://somerandomdomain.net:35001(.*)" />不起作用,因为此参数永远不会包含完整的URL。它只涵盖请求 URI 部分。

我认为您可以尝试 {CACHE_URL} 变量。 此规则是否满足您的要求?我没有要测试的 websocket 示例。所以我只是根据您的要求编写此规则。

<rule name="1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://localhost{C:1}/{C:3}" />
<conditions>
<add input="{CACHE_URL}" pattern="wss://somerandomdomain.net(:([0-9]+))?/(.*)" />
</conditions>
</rule>