2017-07-07 148 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>