IIS 以 50/50 的随机概率重写规则?

IIS Rewrite rules with random probability of 50/50?

我想创建两个 IIS 重写规则,这样规则 A 将 运行 处理 50% 的请求,B 处理另外 50% 的请求。 IIS 重写模块 AFAIK 中没有内置随机 属性。我想在不开发自己的重写模块扩展的情况下实现它。

我希望随机性尽可能 "true"(当然,只要伪随机算法可以是随机的)。

我想到了两种可能:

  1. 获取当前时间戳并使用时间戳的奇偶校验。有这样的服务器变量可用吗?我没找到。
  2. 使用客户端 IP (REMOTE_ADDR) 最后一部分的奇偶校验。

这些选项是否可行?我怎样才能用重写规则来实现它们?有没有更好的解决方案?

看来REMOTE_ADDR选项可行:

<!-- Condition for even IPs (50% connections) -->
<add input="{REMOTE_ADDR}" pattern=".+[02468]$"/>

<!-- Condition for odd IPs (the other 50% connections): -->
<add input="{REMOTE_ADDR}" pattern=".+[13579]$"/>

您可以通过更改图案轻松将其设为 30/70 或 10/90。

以随机方式设置 cookie 的示例配置:

<rewrite>
    <outboundRules>
        <rule name="set new=1 on half the requests" preCondition="new-cookie-is-not-set">
            <match pattern=".*" serverVariable="RESPONSE_Set_Cookie"/>
            <conditions trackAllCaptures="false">
                <add input="{REMOTE_ADDR}" pattern=".+[02468]$"/>
            </conditions>
            <action type="Rewrite" value="new=1; Expires=Fri, 26 Apr 2020 00:00:00 GMT; HttpOnly"/>
        </rule>
        <rule name="set new=0 on the other half" preCondition="new-cookie-is-not-set">
            <match pattern=".*" serverVariable="RESPONSE_Set_Cookie"/>
            <conditions trackAllCaptures="false">
                <add input="{REMOTE_ADDR}" pattern=".+[13579]$"/>
            </conditions>
            <action type="Rewrite" value="new=0; Expires=Fri, 26 Apr 2020 00:00:00 GMT; HttpOnly"/>
        </rule>
        <preConditions>
            <preCondition name="new-cookie-is-not-set">
                <add input="{HTTP_COOKIE}" negate="true" pattern="new=[01]"/>
            </preCondition>
        </preConditions>
    </outboundRules>
</rewrite>