2013-10-10 23 views
0

我正在使用ManagedFusion重寫器作爲反向代理。配置相當簡單:如果尾部斜線缺失,ManagedFusion重寫器404?

RewriteRule ^/api/(.*) http://www.example.com/api/$1 [P] 

這將工作幾乎任何URL。但是,如果URL發生在而不是以尾部斜線結尾,則會失敗。

像這樣的請求將去完美的罰款:GET api/report/

2013-10-10T11:27:11 [Rewrite] Input: http://localhost:50070/api/report/ 
2013-10-10T11:27:11 [Rule 0] Input: /api/report/ 
2013-10-10T11:27:11 [Rule 0] Rule Pattern Matched 
2013-10-10T11:27:11 [Rule 0] Output: http://www.example.com/api/report/ 
2013-10-10T11:27:11 [Rewrite] Proxy: http://www.example.com/api/report/ 
2013-10-10T11:27:11 ********************************************************************************** 
2013-10-10T11:27:11 [Proxy] Request: http://www.example.com/api/report/ 
2013-10-10T11:27:12 [Proxy] System.Net.HttpWebResponse 
2013-10-10T11:27:12 [Proxy] Received '200 OK' 
2013-10-10T11:27:12 [Proxy] Response: http://localhost:50070/api/report/ 
2013-10-10T11:27:12 [Proxy] Response is being buffered 
2013-10-10T11:27:12 [Proxy] Responding '200 OK' 

然而,這樣的請求將返回404,甚至沒有讓被代理的URL請求:GET api/report/1

2013-10-10T11:27:13 [Rewrite] Input: http://localhost:50070/api/report/1 
2013-10-10T11:27:13 [Rule 0] Input: /api/report/1 
2013-10-10T11:27:13 [Rule 0] Rule Pattern Matched 
2013-10-10T11:27:13 [Rule 0] Output: http://www.example.com/api/report/1 
2013-10-10T11:27:13 [Rewrite] Proxy: http://www.example.com/api/report/1 
(the log file finishes right here) 

這是我的整個配置文件:

RewriteEngine On 
RewriteLog "log.txt" 
RewriteLogLevel 9 
RewriteRule ^/api/(.*) http://www.example.com/api/$1 [P] 

任何想法,我可能會錯在哪裏?

回答

0

編輯:我的解決方法已被接受爲重寫器代碼庫中的解決方案,因此我會將其作爲接受的答案。請提供可能的方法反饋。


找到了解決方法,但我不認爲這是實際的解決方案,所以我會回答我自己的問題,但不會接受它作爲答案。 (除非我後來改變了主意)命運是一個善變的女主人。)

我下載了ManagedFusion.Rewriter的源代碼(最新的一個,顯然來自GitHub,在這裏:https://github.com/managedfusion/managedfusion-rewriter/releases),並將它集成到我的代碼庫中。

類ManagedFusion.Rewriter.RewriterModule包含以下兩種方法:

private void context_PostResolveRequestCache(object sender, EventArgs e) 
{ 
    var context = new HttpContextWrapper(((HttpApplication)sender).Context); 

    // check to see if this is a proxy request 
    if (context.Items.Contains(Manager.ProxyHandlerStorageName)) 
     context.RewritePath("~/RewriterProxy.axd"); 
} 

private void context_PostMapRequestHandler(object sender, EventArgs e) 
{ 
    var context = new HttpContextWrapper(((HttpApplication)sender).Context); 

    // check to see if this is a proxy request 
    if (context.Items.Contains(Manager.ProxyHandlerStorageName)) 
    { 
     var proxy = context.Items[Manager.ProxyHandlerStorageName] as IHttpProxyHandler; 

     context.RewritePath("~" + proxy.ResponseUrl.PathAndQuery); 
     context.Handler = proxy; 
    } 
} 

顧名思義,第一個是PostResolveRequestCache的處理程序中,而第二個是用於PostMapRequestHandler該處理程序。

在我的兩個示例請求中,PostResolveRequestCache處理程序正在調用並正常工作。但是,對於我的失敗請求,PostMapRequestHandler未被執行。

這讓我想,也許由於某種原因,通過使用RewritePath重寫一個看起來不像目錄的資源看起來像一個文件的特定資源阻止了實際的處理程序被拾取,從而阻止了PostMapRequestHandler的提高。

因此,我從升級.NET 3.5的重寫項目4.5和更換這些線路:

if (context.Items.Contains(Manager.ProxyHandlerStorageName)) 
    context.RewritePath("~/RewriterProxy.axd"); 

通過這些的

if (context.Items.Contains(Manager.ProxyHandlerStorageName)) { 
    var proxyHandler = context.Items[Manager.ProxyHandlerStorageName] as IHttpHandler; 
    context.RemapHandler(proxyHandler); 
} 

有了這個,所有的請求都被正確地挑由經理人處理並開始工作。


作爲一個方面說明,我在原來的規則了一些錯誤,而不是

RewriteRule ^/api/(.*) http://www.example.com/api/$1 [P] 

它應該是:

RewriteRule ^/api/(.*) http://www.example.com/api/$1 [QSA,P,NC] 
  • QSA追加查詢字符串的原始請求
  • NC匹配reg不區分大小寫