2012-05-19 34 views
2

我正在測試Varnish以增加Magento的加載時間,到目前爲止緩存已經非常棒。我可以爲index.php提供32-35頁/秒的速度,而對於目錄頁面,最大速度爲1200 /秒。不過,我遇到了一個我非常苦惱的問題。我一直試圖解決這個問題好幾天了。將產品添加到購物車時,它會重定向到主頁,然後顯示「商品XXX已添加到購物車」。我可以看到Varnish讓302將其返回到我添加該項目的頁面,但它總是反彈回主頁。添加產品進行比較時存在同樣的問題,但這絕不會添加到要比較的項目列表中。Magento與Nginx和光油 - 302的,餅乾和重定向?

您可以在這裏看到的網站: http://test.autoracks.com

這裏是我的default.vcl:

 # default backend definition. Set this to point to your content server. 
     backend default { 
      .host = "127.0.0.1"; 
      .port = "8080"; 
     } 

     # admin backend with longer timeout values. Set this to the same IP & port as your default server. 
     backend admin { 
      .host = "127.0.0.1"; 
      .port = "8080"; 
      .first_byte_timeout = 18000s; 
      .between_bytes_timeout = 18000s; 
     } 


     # add your Magento server IP to allow purges from the backend 
     acl purge { 
      "localhost"; 
      "127.0.0.1"; 
     } 


     sub vcl_recv { 
      if (req.restarts == 0) { 
       if (req.http.x-forwarded-for) { 
        set req.http.X-Forwarded-For = 
        req.http.X-Forwarded-For ", " client.ip; 
       } else { 
        set req.http.X-Forwarded-For = client.ip; 
       } 
      } 

      if (req.request != "GET" && 
       req.request != "HEAD" && 
       req.request != "PUT" && 
       req.request != "POST" && 
       req.request != "TRACE" && 
       req.request != "OPTIONS" && 
       req.request != "DELETE" && 
       req.request != "PURGE") { 
       /* Non-RFC2616 or CONNECT which is weird. */ 
       return (pipe); 
      } 

      # purge request 
      if (req.request == "PURGE") { 
       if (!client.ip ~ purge) { 
        error 405 "Not allowed."; 
       } 
       purge("obj.http.X-Purge-Host ~ " req.http.X-Purge-Host " && obj.http.X-Purge-URL ~ " req.http.X-Purge-Regex " && obj.http.Content-Type ~ " req.http.X-Purge-Content-Type); 
       error 200 "Purged."; 
      } 



      # switch to admin backend configuration 
      if (req.http.cookie ~ "adminhtml=") { 
       set req.backend = admin; 
      } 

      # we only deal with GET and HEAD by default  
      if (req.request != "GET" && req.request != "HEAD") { 
       return (pass); 
      } 

      # normalize url in case of leading HTTP scheme and domain 
      set req.url = regsub(req.url, "^http[s]?://[^/]+", ""); 

      # static files are always cacheable. remove SSL flag and cookie 
      if (req.url ~ "^/(media|js|skin)/.*\.(png|jpg|jpeg|gif|css|js|swf|ico)$") { 
       unset req.http.Https; 
       unset req.http.Cookie; 
      } 

      # not cacheable by default 
      if (req.http.Authorization || req.http.Https) { 
       return (pass); 
      } 

      # do not cache any page from 
      # - index files 
      # - ... 
      #if (req.url ~ "^/(index)") { 
      # return (pass); 
      #} 

      # as soon as we have a NO_CACHE cookie pass request 
      if (req.http.cookie ~ "NO_CACHE=") { 
       return (pass); 
      } 

      # normalize Aceept-Encoding header 
      # http://varnish.projects.linpro.no/wiki/FAQ/Compression 
      if (req.http.Accept-Encoding) { 
       if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|flv)$") { 
        # No point in compressing these 
        remove req.http.Accept-Encoding; 
       } elsif (req.http.Accept-Encoding ~ "gzip") { 
        set req.http.Accept-Encoding = "gzip"; 
       } elsif (req.http.Accept-Encoding ~ "deflate" && req.http.user-agent !~ "MSIE") { 
        set req.http.Accept-Encoding = "deflate"; 
       } else { 
        # unkown algorithm 
        remove req.http.Accept-Encoding; 
       } 
      } 

      # remove Google gclid parameters 
      set req.url = regsuball(req.url,"\?gclid=[^&]+$",""); # strips when QS = "?gclid=AAA" 
      set req.url = regsuball(req.url,"\?gclid=[^&]+&","?"); # strips when QS = "?gclid=AAA&foo=bar" 
      set req.url = regsuball(req.url,"&gclid=[^&]+",""); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz" 

      return (lookup); 
     } 

     # sub vcl_pipe { 
     #  # Note that only the first request to the backend will have 
     #  # X-Forwarded-For set. If you use X-Forwarded-For and want to 
     #  # have it set for all requests, make sure to have: 
     #  # set bereq.http.connection = "close"; 
     #  # here. It is not set by default as it might break some broken web 
     #  # applications, like IIS with NTLM authentication. 
     #  return (pipe); 
     # } 
     # 
     # sub vcl_pass { 
     #  return (pass); 
     # } 
     # 
     sub vcl_hash { 
      set req.hash += req.url; 
      if (req.http.host) { 
       set req.hash += req.http.host; 
      } else { 
       set req.hash += server.ip; 
      } 
      if (!(req.url ~ "^/(media|js|skin)/.*\.(png|jpg|jpeg|gif|css|js|swf|ico)$")) { 
       call design_exception; 
      } 
      return (hash); 
     } 
     # 
     # sub vcl_hit { 
     #  if (!obj.cacheable) { 
     #   return (pass); 
     #  } 
     #  return (deliver); 
     # } 
     # 
     # sub vcl_miss { 
     #  return (fetch); 
     # } 

     sub vcl_fetch { 
      if (beresp.status == 500) { 
       set beresp.saintmode = 10s; 
       restart; 
      } 
      set beresp.grace = 5m; 

      # add ban-lurker tags to object 
      set beresp.http.X-Purge-URL = req.url; 
      set beresp.http.X-Purge-Host = req.http.host; 

      if (beresp.status == 200 || beresp.status == 301 || beresp.status == 404) { 
       if (beresp.http.Content-Type ~ "text/html" || beresp.http.Content-Type ~ "text/xml") { 
        if ((beresp.http.Set-Cookie ~ "NO_CACHE=") || (beresp.ttl < 1s)) { 
         set beresp.ttl = 0s; 
         return (pass); 
        } 

        # marker for vcl_deliver to reset Age: 
        set beresp.http.magicmarker = "1"; 

        # Don't cache cookies 
        unset beresp.http.set-cookie; 
       } else { 
        # set default TTL value for static content 
        set beresp.ttl = 4h; 
       } 
       return (deliver); 
      } 

      return (pass); 
     } 

     sub vcl_deliver { 
      # debug info 
      if (resp.http.X-Cache-Debug) { 
       if (obj.hits > 0) { 
        set resp.http.X-Cache = "HIT"; 
        set resp.http.X-Cache-Hits = obj.hits; 
       } else { 
        set resp.http.X-Cache = "MISS"; 
       } 
       set resp.http.X-Cache-Expires = resp.http.Expires; 
      } else { 
       # remove Varnish/proxy header 
       remove resp.http.X-Varnish; 
       remove resp.http.Via; 
       remove resp.http.Age; 
       remove resp.http.X-Purge-URL; 
       remove resp.http.X-Purge-Host; 
      } 

      if (resp.http.magicmarker) { 
       # Remove the magic marker 
       unset resp.http.magicmarker; 

       set resp.http.Cache-Control = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"; 
       set resp.http.Pragma = "no-cache"; 
       set resp.http.Expires = "Mon, 31 Mar 2008 10:00:00 GMT"; 
       set resp.http.Age = "0"; 
      } 
     } 

     # sub vcl_error { 
     #  set obj.http.Content-Type = "text/html; charset=utf-8"; 
     #  synthetic {" 
     # <?xml version="1.0" encoding="utf-8"?> 
     # <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
     # "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
     # <html> 
     # <head> 
     #  <title>"} obj.status " " obj.response {"</title> 
     # </head> 
     # <body> 
     #  <h1>Error "} obj.status " " obj.response {"</h1> 
     #  <p>"} obj.response {"</p> 
     #  <h3>Guru Meditation:</h3> 
     #  <p>XID: "} req.xid {"</p> 
     #  <hr> 
     #  <p>Varnish cache server</p> 
     # </body> 
     # </html> 
     # "}; 
     #  return (deliver); 
     # } 

     sub design_exception { 
     } 

我應該,如果我把nginx的前面一切正常。 任何幫助,將不勝感激,我真的想得到這個工作!

謝謝...

+0

您是否使用Phoenix插件來控制緩存區段? – philwinkle

+0

此外,您提到的域名 - http://test.autoracks.com/ - 在http響應中沒有X-Cache標頭......您確定Varnish實際上是否在針對此域名運行? – philwinkle

+0

是的,我正在使用鳳凰擴展,X標題沒有出現,因爲他們關閉了,你會看到他們現在。我已經正確地設置了一切,我可以看到它工作正常(事實上),但我只是處理這兩個小的細節,這些細節很大。如果您在產品頁面上添加產品,則不會發生此問題,但從目錄/類別視圖中添加會觸發此問題....我無法弄清楚爲什麼它的行爲不同? 我也試過這個沒有鳳凰擴展和同樣的問題存在。它只發生在Varnish正在向Nginx傳遞請求時......? – DSDesign

回答

1

當我與一個本地的Apache服務器使用攻城測試清漆測試有時會發出不同的響應URL,比我從測試期待。 我不知道爲什麼我從來沒有目睹(用我的眼睛)一個錯誤的頁面加載,即使我運行它與同一個VCL所有的時間,我正在開發其他的東西。 但我遵循清漆網站上的建議,並安裝了64位操作系統。 我的問題消失了。您使用的是32位系統,您是否使用 ?

2

這個問題位於Magento的核心。由於方法getCurrentUrl使用Nginx端口號(在清漆後面不經常是80)來構建URL。稍後,當重定向URL被解碼時,由於端口號的原因,它將作爲「內部URL」失敗。

解決方案(修改內部消除Magento的核心)是設置Nginx的聽80端口和清漆的一種東西。然後使用iptables將傳入的流量映射到Varnish。

我在上Keeping Magento satisfied behind Varnish博客文章涉及這一點。