Nginx上的Varnish/Symfony3缓存 - 高速缓存前的0 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'标头,您应该尝试删除它。 –

Varnish不会缓存设置cookie的页面,并且在您提供的响应标题中有一个Set-Cookie

为了解决这个问题,你可以遵循两条路径:

  • 继续避免缓存的Set-Cookie这些页面;
  • 从您想要缓存的页面中删除所有Set-Cookie;
  • 或者把所有的操作与你想要缓存的页面分开,并通过AJAX调用它。