Varnish not caching right

varnish

I'm trying to configure varnish (following by: default_varnish3.vcl_.txt)

[alexus@wcmisdlin02 ~]$ rpm -q varnish
varnish-3.0.3-1.el5.centos.x86_64
[alexus@wcmisdlin02 ~]$ 

I'm making a very first hit through my browser (page loads fine), then I do it again through curl on my local machine:

[alexus@wcmisdlin02 ~]$ curl -I alexustest:6081
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
X-Powered-By: PHP/5.3.3
Cache-Control: public, max-age=1800
Last-Modified: Thu, 11 Apr 2013 19:27:15 +0000
Expires: Sun, 11 Mar 1984 12:00:00 GMT
Vary: Cookie,Accept-Encoding
Content-Type: text/html; charset=utf-8
Date: Thu, 11 Apr 2013 19:27:16 GMT
X-Varnish: 1355259954
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Served-By: wcmisdlin02.uftmasterad.org
X-Cache: MISS

it shows me X-Cache: MISS, when it really should have been a HIT (since this is second hit), so I do it again (3rd time and again from my Linux workstation).

[alexus@wcmisdlin02 ~]$ curl -I alexustest:6081
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Red Hat)
X-Powered-By: PHP/5.3.3
Cache-Control: public, max-age=1800
Last-Modified: Thu, 11 Apr 2013 19:27:15 +0000
Expires: Sun, 11 Mar 1984 12:00:00 GMT
Vary: Cookie,Accept-Encoding
Content-Type: text/html; charset=utf-8
Date: Thu, 11 Apr 2013 19:27:19 GMT
X-Varnish: 1355259955 1355259954
Age: 3
Via: 1.1 varnish
Connection: keep-alive
X-Served-By: wcmisdlin02.uftmasterad.org
X-Cache: HIT
X-Cache-Hits: 1

[alexus@wcmisdlin02 ~]$ 

and now it's a HIT, it sounds like it's caching per client or something. As I said I'm using default_varnish3.vcl_.txt, so you can see my configuration as that, I only changed backend information to match my environment and also added following code (to produce HIT/MISS):

[root@wcmisdlin02 varnish]# cat vcl_deliver/resp.vcl 
set resp.http.X-Served-By = server.hostname;
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"; 
}
[root@wcmisdlin02 varnish]# 

* UPDATE *

varnishncsa's:

10.52.208.221 - - [11/Apr/2013:15:45:35 -0400] "HEAD http://alexustest:6081/ HTTP/1.1" 200 0 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.13.1.0 zlib/1.2.3 libidn/1.18 libssh2/1.2.2"
10.100.0.35 - - [11/Apr/2013:15:45:38 -0400] "HEAD http://alexustest:6081/ HTTP/1.1" 200 0 "-" "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5"

Best Answer

first hit through my browser (page loads fine), then I do it again through curl

But you told the webcache you were going to serve different results based on the cookies and the content encoding:

Vary: Cookie,Accept-Encoding

Did you delete all the cookies in your browser before before the first request? Also how did you make the request from the browser - a reload has very different behaviour from a refresh.

(BTW if you're going to vary your by cookie and you're using sessions then you should make your cache controlprovate - otherwise you're making Varnish work hard for no benefit, indeed you could actually slow your site down).

Related Topic