Varnish年龄是0(代理通过nginx到节点应用程序)

我想添加清漆到现有的networking堆栈,但我似乎无法得到我的网页的caching版本。 我想知道我可能做错了。

这个configuration工作,但我没有得到从清漆回来的caching响应:

1. Nginx is listening on port 443, request proxied to Varnish 2. Varnish is listening on port 80, request proxied to Node App 3. Successful Response 

我试过了:

curl -kIL https://www.example.com

 HTTP/1.1 200 OK Cache-Control: public, max-age=30 Set-Cookie: locale=en-ca; Path=/ Set-Cookie: locale=en-ca; Path=/ Content-Type: text/html; charset=utf-8 Content-Length: 161861 ETag: W/"27845-EcqEhuo8dduXo4rrAF4EfS6igbQ" Vary: Accept-Encoding Date: Wed, 18 Oct 2017 23:57:39 GMT X-Varnish: 33476 Age: 0 Via: 1.1 varnish-v4 Accept-Ranges: bytes Connection: keep-alive 

对于每个后续请求, Age:始终为0

VCL供参考:

 vcl 4.0; import std; backend example { .host = "www.example.com"; .port = "7600"; } backend api { .host = "api"; .port = "8080"; } sub vcl_recv { # Remove the port from Host set req.http.Host = regsub(req.http.Host, ":[0-9]+", ""); if (req.http.host ~ "www.example.com") { set req.backend_hint = example; std.log("hit example.com"); } if (req.http.host ~ "^api") { set req.backend_hint = api; std.log("hit api"); } if (req.http.User-Agent ~ "Amazon Route 53") { return (pass); } } 

你没有得到caching的原因是应用程序响应中存在Set-Cookie头:

 Set-Cookie: locale=en-ca; Path=/ Set-Cookie: locale=en-ca; Path=/ 

如果你想继续使用Varnish,并确保它是caching的东西,你应该更新你的应用程序来设置cookie只在必要的时候,而不是每个页面。

对于多语言网站,最好依靠Accept-Language:请求标头并发送Vary而不是使用cookies。