2013-12-20 67 views
0

我試圖設置一箇中間清漆服務器,以允許xyz.com上的Rails應用程序在其他幾個域名上工作。清漆緩存http 301 302標題位置重定向

會發生什麼情況是Rails應用程序不時輸出一些301/302重定向,顯然清漆不會更改這些標題,因此訪問者會被重定向到原始站點(位於面向公衆的清漆服務器後面),所以...錯誤。

有沒有辦法在事物的清漆面上配置此重寫?

下vcl_fetch我試過如下:

if ((beresp.status == 301) || (beresp.status == 302)) { 
    set req.url = regsub(req.url,".*",regsuball(regsub(beresp.http.Location,"^http://[^/]+(.*)","\1"),"[+]","%2520")); 
    return(restart); 

但也許我不明白這是如何工作到底是什麼?任何幫助將不勝感激

回答

1

與你的正則表達式戰鬥後,用例的思考......我想你可能會做一個很簡單的事情,比如重寫位置和緩存對象糾正(並離開重定向到客戶端瀏覽器)。

vcl_fetch

# ... 
if (beresp.status == 301 
    || beresp.status == 302 
) { 
    # Check if we're redirecting to a different site 
    if (! beresp.http.Location ~ req.http.host) { 
    # Rewrite HTTP Location header to cache it and pass redirection to client 
    set beresp.http.Location = regsub(
           beresp.http.Location, 
           "^http://[^/]+/", 
           "http://" + req.http.host + "/" 
           ); 
    } 
} 
# ... 

如果你還是喜歡重啓裏面光油不同的URL請求,我會嘗試(再次vcl_fetch):

# ... 
if (beresp.status == 301 
    || beresp.status == 302 
) { 
    # Add a header so you can debug cleanly on varnishlog 
    set req.http.X-Redirected-Orig = beresp.http.Location; 
    # Rewrite request host 
    set req.http.host = regsub(
         regsub(
          beresp.http.Location, 
          "^http://", 
          "", 
         ), 
         "^([^/]+)/.*$", 
         "\1" 
        ); 
    # Rewrite request url 
    set req.url = regsub(
        beresp.http.Location, 
        "^http://[^/]+/(.*)$", 
        "/\1", 
       ); 
    # Add a header so you can debug cleanly on varnishlog 
    set req.http.X-Redirected-To = "http://" + req.http.host + req.url; 
    return (restart); 
} 
# ... 

PS :請原諒我的regsub縮進,但我認爲它更具可讀性。

+0

謝謝!很有用! –