IIS8 RewriteModule / URLRewrite极慢

时间:2017-07-07 13:36:24

标签: url-rewriting iis-8 windows-server-2012

我在Windows Server 2012上运行带有IIS8的网站。我试图确定IIS导致高CPU使用率的原因(IIS通常占用CPU的50%或更多)。服务器全天每秒接收大约40个请求,但每秒只需要1-2个URL需要处理。

我启用了“请求跟踪”,发现一些RewriteModule请求需要100多秒(!)才能完成。我很难确定在硬件足够多的机器上如何实现这一点。在不到一秒的时间内,通过Apache上的mod_rewrite处理完全相同的URL结构。

示例网址如下:

http://<domain-name>/Product/<Parent-Category>/<Child-Category1>/<Child-Category2>/<Child-Category3>/<Product-Name>

随附的重写规则是:

<rule name="Rule" stopProcessing="true">
      <match url="^Product/([^/\?]+)/?([^/\?]+)?/?([^/\?]+)?/?([^/\?]+)?/?([^/\?]+)?/?([^/\?]+)?/?[\?]?(.+)?$"/>
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
      </conditions>
      <action type="Rewrite" url="/Product/index.php?a={R:1}&amp;b={R:2}&amp;c={R:3}&amp;d={R:4}&amp;e={R:5}&amp;f={R:6}&amp;{R:7}" appendQueryString="true"/>
    </rule>

我是否有某种方式可以定义匹配网址,从而导致处理时间过长?如果一些匹配的URL使用许多父/子类别(最多5个,通常为3-4个),则它们包含大量字符。

2 个答案:

答案 0 :(得分:1)

问题肯定在你的正则表达式中。最好的方法是尝试将其拆分为不同的更具体的模式。

如果不是这样,则此规则应保持相同的功能并更快地运行:

options = optimoptions('lsqnonlin', 'FunctionTolerance', 1e-10, 'FiniteDifferenceStepSize',1e-6)

我在正则表达式中删除了不必要的<rule name="Rule" stopProcessing="true"> <match url="^Product/([^/]+)/?([^/]+)?/?([^/]+)?/?([^/]+)?/?([^/]+)?/?([^/]+)?"/> <conditions logicalGrouping="MatchAll" trackAllCaptures="false"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/> </conditions> <action type="Rewrite" url="/Product/index.php?a={R:1}&amp;b={R:2}&amp;c={R:3}&amp;d={R:4}&amp;e={R:5}&amp;f={R:6}" appendQueryString="true"/> </rule> 个检查,因为\?内的模式正在检查没有查询字符串的网址,因此,在正则表达式中进行<match url检查是多余的。

我检查了我的电脑,它的工作速度肯定更快,但您需要仔细检查它是否保持相同的功能

答案 1 :(得分:0)

我做了大量测试并将规则更改为以下内容。这导致CPU下降到1%,之前的100秒完成时间下降到大约50ms。

我仍然不明白这是怎么回事 - 这是一台带有48GB内存的4 CPU / 8核机器,IIS8需要100秒才能将70个字符的字符串拉开,并将其与之前的正则表达式进行比较。除非前面的例子以某种方式创建了一个近乎无限的循环,否则我不知道它可能会如此缓慢。

新规则:

<rule name="Rule" stopProcessing="true">
      <match url="^Product/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)?[/]?(.+)?$"/>
      <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
      </conditions>
      <action type="Rewrite" url="/Product/index.php?a={R:1}&amp;b={R:2}&amp;c={R:3}&amp;d={R:4}&amp;e={R:5}&amp;f={R:6}&amp;{R:7}" appendQueryString="true"/>
    </rule>
相关问题