【问题标题】:IIS Rewrite rules with random probability of 50/50?IIS 以 50/50 的随机概率重写规则?
【发布时间】:2018-04-03 19:09:34
【问题描述】:
我想创建两个 IIS 重写规则,这样规则 A 将在 50% 的请求上运行,而 B 在其他 50% 上运行。 IIS 重写模块 AFAIK 中没有内置随机属性。我想在不开发自己的重写模块扩展的情况下实现它。
我更喜欢随机尽可能“真实”(当然,伪随机算法可以是随机的)。
我想过两种可能:
- 获取当前时间戳并使用时间戳的奇偶校验。是否有这样的服务器变量可用?我没找到。
- 使用客户端 IP 最后一部分的奇偶校验 (REMOTE_ADDR)。
这些选项之一是否可行?如何使用重写规则来实现它们?有没有更好的解决方案?
【问题讨论】:
标签:
iis
random
url-rewrite-module
iis-8.5
【解决方案1】:
看起来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>