Caching cookies with Varnish 4

Raymond Gonzalez raygo22 at
Wed May 13 17:33:34 CEST 2015

I want to be able to cache pages that have a cookie. Let's say I have a
cookie called "testcookie" that I set up with php.

If testcookie = 1 I want a page (cached), if testcookie = 2 it will have a
different page ( i want to cache it) , and like that N times. This is my
current varnish vcl


vcl 4.0;

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

sub vcl_hash {
hash_data(regsub(req.http.Cookie,"^.*?testvarnish=([^;]*);*.*$" , "\1"));

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
    # 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.


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!

Raymond Gonzalez
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the varnish-misc mailing list