2016-09-29 103 views
0

我已經在我的Symfony版本上通過Docker設置了清漆,但無法使緩存正常工作。無論我嘗試什麼,年齡似乎都停留在0。Nginx上的Varnish/Symfony3緩存 - 高速緩存前的0 0

從閱讀這裏可能是關於cookie設置或可能過期的標題,但我不能爲我的生活工作了發生了什麼事情。

響應頭:

Age:0 
Cache-Control:max-age=64000, public, s-maxage=64000 
Connection:keep-alive 
Content-Type:text/html; charset=UTF-8 
Date:Thu, 29 Sep 2016 20:21:43 GMT 
ETag:"cb3ed8f6672c4be1148c4f7d12c20789" 
Server:nginx 
Set-Cookie:device_view=full; expires=Sat, 29-Oct-2016 20:21:43 GMT; Max-Age=2592000; path=/; HttpOnly 
Transfer-Encoding:chunked 
Vary:Accept-Encoding, Accept-Language 
Via:1.1 varnish-v4 
X-Cache-Debug:1 
X-Cache-Debug:1 
X-Debug-Token:503eda 
X-Debug-Token-Link:http://192.168.99.100/app_dev.php/_profiler/503eda 
X-Powered-By:PHP/7.0.11 
X-Varnish:65542 

請求標頭:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
Accept-Encoding:gzip, deflate, sdch 
Accept-Language:en-US,en;q=0.8 
Cache-Control:no-cache 
Connection:keep-alive 
Cookie:customcookie=1; _gat=1; PHPSESSID=1fd8cd59c8d8d0185310ec0cdb06fce7; _ga=GA1.1.706323429.1474891952; hl=en; device_view=full 
Host:192.168.99.100 
Pragma:no-cache 
Upgrade-Insecure-Requests:1 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 

當前VCL文件:

vcl 4.0; 

backend default { 
    .host = "192.168.99.100"; 
    .port = "8080"; 
} 

sub vcl_recv { 
    unset req.http.Forwarded; 

    if (req.http.X-Forwarded-Proto == "https") { 
     set req.http.X-Forwarded-Port = "443"; 
    } else { 
     set req.http.X-Forwarded-Port = "80"; 
    } 

    // Remove all cookies except the session ID. 
    if (req.http.Cookie) { 
     set req.http.Cookie = ";" + req.http.Cookie; 
     set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";"); 
     set req.http.Cookie = regsuball(req.http.Cookie, ";(PHPSESSID)=", "; \1="); 
     set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", ""); 
     set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", ""); 

     if (req.http.Cookie == "") { 
      // If there are no more cookies, remove the header to get page cached. 
      unset req.http.Cookie; 
     } 
    } 
} 

更改爲下面沒有任何效果:

sub vcl_recv { 
    unset req.http.Forwarded; 
    unset req.http.Cookie; 
} 

我猜這會很簡單,但不能得到這個工作。任何幫助表示讚賞

+0

當你直接請求(不含清漆)時,你可以添加你的nginx的響應標題嗎?另外,您在請求中使用'Pragma:no-cache'標頭,您應該嘗試刪除它。 –

回答

0

Varnish不會緩存設置cookie的頁面,並且在您提供的響應標題中有一個Set-Cookie

爲了解決這個問題,你可以遵循兩條路徑:

  • 繼續避免緩存的Set-Cookie這些頁面;
  • 從您想要緩存的頁面中刪除所有Set-Cookie;
  • 或者把所有的操作與你想要緩存的頁面分開,並通過AJAX調用它。