2017-07-07 108 views
0

我在Windows Server 2012上运行IIS8的网站。我试图确定是什么原因导致IIS的CPU使用率过高(IIS的CPU使用率通常高达50%或更多)。服务器全天接收每秒约40个请求,但可能只需要每秒1-2个需要处理的URL。IIS8 RewriteModule/URLRewrite极其缓慢

我启用了请求跟踪,发现一些RewriteModule请求超过100秒(!)完成。我无法确定如何在具有足够硬件的机器上实现这一点。完全相同的URL结构在不到一秒的时间内通过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个),则其中包含大量字符。

回答

1

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

如果不是的话,这个规则应该保持相同的功能,并提高工作效率:

<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行话是检查URL没有查询字符串,所以,在您的正则表达式中检查?是多余的。

我检查我的电脑和它的作品绝对快,但你需要仔细检查它仍然是相同的功能

+0

我贴我的变化,我上周五晚做,但我接受你的答案,因为它表明了同样的根本性的变化。 –

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>