7

我在 Windows 2008R2、IIS 7 环境中部署了 .Net 应用程序(使用 Outsystems 平台构建)。此应用程序已经可以正常运行。它通过端口 443 上的 HTTPS 服务请求,我可以毫无问题地访问它。

现在,需要使某些用户能够通过 NAT 访问它,该 NAT 被配置为在端口 8200 上提供请求。请求被转发到服务器没有问题,除非响应是重定向 (HTTP 302)。在这些情况下,响应的 Location Header 设置为应用程序实际服务的端口,即端口 443。当客户端的浏览器尝试访问在 Location Header 中设置的 URL 时,它将失败,因为 NAT仅服务于端口 8200 中的请求。

总共:

  • 客户端浏览器访问https://myserver:8200/myapp
  • NAT 转换 URL 并转发到https://myserver:443/myapp;
  • 应用程序以 302 重定向回复https://myserver:443/redirect
  • 客户端浏览器访问https://myserver:443/redirect
  • NAT 阻止此访问,因为它需要端口 8200 ;

这是我试图做的;我在 IIS 中安装了 URL 重写模块并创建了以下规则:

  • 将请求的主机标头(在这些情况下为https://myserver:8200)存储到服务器变量 ORIGINAL_HOST 的入站规则。
  • https://myserver:443/redirect一个出站规则,用存储在我的服务器变量 ORIGINAL_HOST 中的值替换在响应的 Location Header ( ) 中发送的 URL 的主机部分。该规则仅适用于重定向的情况。

我现在面临的问题是,在我启用此规则的那一刻,所有 AJAX 请求都会停止工作,即使该规则不适用于它们,即使不通过 NAT 访问应用程序也是如此。发生这种情况时,我通过开发人员工具中的 IE 网络选项卡看到请求标头和正文是好的,响应标头是好的,但响应正文是空的,不应该是。此外,如果我尝试使用断点在服务器上调试 AJAX 请求,它不会在断点处停止。

所以,我的问题是:使用 URL 重写是解决 NAT/URL 问题的最佳方法吗?如果是这样,我该如何解决 AJAX 问题?如果没有,那么我该如何解决重定向问题?

*编辑:这是我的规则配置:

先感谢您,

若昂·戈麦斯

4

1 回答 1

9

The simplest url rewrite to do what you want is an outbound rule targeting 3xx redirects. The rule can parse the location with regex, and allow you to change the port. I'm not sure if this accomplishes everything you are setting out to do (if port must be dynamic) but it should at least get you most the way there.

The pattern (https?://[^:/]+):?([0-9]+)?(.*) will parse the location as follows:

https://sub.host.com:1337/foo/bar?query=true

{R:1} => https://sub.host.com
{R:2} => 1337
{R:3} => /foo/bar?query=true

Note: If the port is not in the URL, {R:2} will be empty.

Then for your use case, it would reassemble in the action rewrite to:

{R:1}:8200{R:3} => https://sub.host.com:8200/foo/bar?query=true

Outbound URL Rewrite


In web.config <system.webServer>:

<rewrite>
    <outboundRules>
        <rule name="Redirect to port" preCondition="3xx Redirect">
            <match serverVariable="RESPONSE_LOCATION" pattern="(https?://[^:/]+):?([0-9]+)?(.*)" />
            <action type="Rewrite" value="{R:1}:8200{R:3}" />
        </rule>
        <preConditions>
            <preCondition name="3xx Redirect">
                <add input="{RESPONSE_STATUS}" pattern="3[0-9][0-9]" />
            </preCondition>
            <preCondition name="3xx redirect no query string">
                <add input="{RESPONSE_LOCATION}" pattern="^(.*)(\?.+)$" negate="true" />
                <add input="{RESPONSE_STATUS}" pattern="3[0-9][0-9]" />
            </preCondition>
            <preCondition name="3xx redirect with query string">
                <add input="{RESPONSE_STATUS}" pattern="3[0-9][0-9]" />
                <add input="{RESPONSE_LOCATION}" pattern="^(.*)(\?.+)$" />
            </preCondition>
        </preConditions>
    </outboundRules>
</rewrite>

@hally9k - maybe this is what you were looking for also?

于 2016-10-12T20:47:33.253 回答