Web-server – Varnish 4 not caching

apache-2.2centos6varnishweb-server

I am new at working with varnish. I installed it, I think i configured it correctly. In order to test this is what i did:

I created a test page that only has the string "test".
I went to the page and it has these headers:

Accept-Ranges:bytes
Age:0
Cache-Control:max-age=120
Connection:keep-alive
Content-Length:6
Content-Type:text/html; charset=UTF-8
Date:Tue, 12 May 2015 19:35:34 GMT
Expires:Tue, 12 May 2015 19:37:34 GMT
Server:Apache/2.2.15 (CentOS)
Via:1.1 varnish-v4
X-Powered-By:PHP/5.3.3
X-Varnish:32829

I change the text in the file to "test2"
I go to the page and it shows "test2". I believe it should be showing "test" if it was caching correctly.

I don't have cookies set up or anything, just that. My vcl is very simple:

vcl 4.0;

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

sub vcl_recv {
    # Happens before we check if we have this in cache already.
    #
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
}

sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    #
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.


    #This will set up "grace mode".
    set beresp.ttl = 10s;
    set beresp.grace = 1h;


}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    #
    # You can do accounting or modifying the final object here.
}

Any idea? Thanks

Best Answer

Chances are good that are sending cookies in your request. Varnish doesn't cache anything with cookies in them. This from the builtin.vcl for Varnish 4:

47  sub vcl_recv {
48        if (req.method == "PRI") {
49            /* We do not support SPDY or HTTP/2.0 */
50            return (synth(405));
51        }
52        if (req.method != "GET" &&
53          req.method != "HEAD" &&
54          req.method != "PUT" &&
55          req.method != "POST" &&
56          req.method != "TRACE" &&
57          req.method != "OPTIONS" &&
58          req.method != "DELETE") {
59            /* Non-RFC2616 or CONNECT which is weird. */
60            return (pipe);
61        }
62    
63        if (req.method != "GET" && req.method != "HEAD") {
64            /* We only deal with GET and HEAD by default */
65            return (pass);
66        }
67        if (req.http.Authorization || req.http.Cookie) {
68            /* Not cacheable by default */
69            return (pass);
70        }
71        return (hash);
72    }

You need to remove unwanted cookies in your VCL, as shown in this example from the Varnish website

Removing Set-Cookie from the backend (for a particular path)

In this case, we remove both the Cookie header and the Set-Cookie header for objects under a predefined path. This is quite common for images and similar static content.

sub vcl_recv {
    if (req.url ~ "^/images") {
        unset req.http.cookie;
    }
}

sub vcl_backend_response {
    if (req.url ~ "^/images") {
        unset beresp.http.set-cookie;
    }
}

If you test with curl -i URL, you won't send any cookies, and you if you repeat that more than a second later, you should get an Age header greater than 0.