【问题标题】:IIS URL Rewrite some rules overriding other rulesIIS URL 重写一些覆盖其他规则的规则
【发布时间】:2020-04-11 13:16:17
【问题描述】:

我们正在将我们的主站点分解为微服务。最初,当我们拆分网站的一部分时,它将在我们的原始服务器上成为它自己的网站,直到它可以移动到它自己的容器中。

这个站点目前是一个 Angular 站点,所以你看我们有规则重写所有内容以转到 index.html。我正在尝试添加一个额外的规则,所以如果它找到了 url。 www.domainname.com/api/auth/.* 到 localhost:8001/{R:1}

我的理解是它将从 /auth/ 之后的 url 中获取所有内容并将其更改为 localhost:8001/{stuff after}

发生的事情是它遵循将所有内容指向索引文件的规则,并且似乎忽略了我的新规则。我第一次和最后一次都试过了,所以我假设我的规则一定有问题。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Api Rule">
                    <match url="domainname.com/api/auth/.*" />
                    <action type="Rewrite" url="localhost:8001/{R:1}" appendQueryString="false" />
                </rule>
                <rule name="AngularJS Routes" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />   
                    </conditions>
                    <action type="Rewrite" url="/" />
                </rule>
                <rule name="CanonicalHostNameRule1">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^www\.domainname\.com$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://www.domainname.com/{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>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

【问题讨论】:

    标签: angular iis url-rewriting microservices


    【解决方案1】:

    规则的评估顺序与它们指定的顺序相同。 (来自文档:URL rewrite module configuration reference - rules evaluation)

    match 元素的url 属性与 URL 的 PATH 部分一起使用。您不应指定域。

    <rule name="Api Rule">
        <match url="^api/auth/(.*)" />
        <action type="Rewrite" url="localhost:8001/{R:1}" appendQueryString="false" />
    </rule>
    

    如果你想与域匹配,你可以添加一个条件

    <rule name="Api Rule">
        <match url="^api/auth/(.*)" />
        <conditions>
            <add input="{HTTP_HOST}" pattern="^domain\.com$" />
        </conditions>
        <action type="Rewrite" url="localhost:8001/{R:1}" appendQueryString="false" />
    </rule>
    

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 2012-10-02
      • 2014-05-01
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 2014-07-28
      相关资源
      最近更新 更多