Created
October 24, 2013 20:32
-
-
Save zodman/7144484 to your computer and use it in GitHub Desktop.
varnish 1.3.x for django bypassing cache on admin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
backend default { | |
.host = "127.0.0.1"; | |
.port = "81"; | |
} | |
sub vcl_recv { | |
# if find purge in the url go there and purge | |
if( req.url ~ "purge" ) { | |
return (lookup); | |
} | |
#remove from the url | |
set req.url = regsuball(req.url,"\?gclid=[^&]+$",""); # strips when QS = "?gclid=AAA" | |
set req.url = regsuball(req.url,"\?gclid=[^&]+&","?"); # strips when QS = "?gclid=AAA&foo=bar" | |
set req.url = regsuball(req.url,"&gclid=[^&]+",""); # strips when QS = "?foo=bar&gclid=AAA" or QS = "?foo=bar&gclid=AAA&bar=baz" | |
if ( req.url ~ "admin" ) { | |
return (pass); | |
} else { | |
remove req.http.cookie; | |
unset req.http.cookie; | |
} | |
} | |
sub vcl_deliver { | |
#checking if is hit or not | |
if (obj.hits > 0) { | |
set resp.http.X-Cache-Hits = obj.hits; | |
set resp.http.X-Cache = "HIT"; | |
} else { | |
set resp.http.X-Cache = "MISS"; | |
} | |
} | |
sub vcl_fetch { | |
# it is django admin not cache | |
if (req.url ~ "admin") { | |
set beresp.http.X-Cacheable = "NO: admin yeah!"; | |
return(hit_for_pass); | |
} | |
# Varnish determined the object was not cacheable | |
if (beresp.ttl <= 0s) { | |
set beresp.http.X-Cacheable = "NO:Not Cacheable"; | |
# You don't wish to cache content for logged in users | |
} elsif (req.url ~ "admin") { | |
set beresp.http.X-Cacheable = "NO: admin"; | |
return(hit_for_pass); | |
# You are respecting the Cache-Control=private header from the backend | |
} elsif (beresp.http.Cache-Control ~ "private") { | |
set beresp.http.X-Cacheable = "NO:Cache-Control=private"; | |
return(hit_for_pass); | |
# Varnish determined the object was cacheable | |
} else { | |
set beresp.http.X-Cacheable = "YES"; | |
} | |
return(deliver); | |
} | |
sub vcl_miss { | |
if( req.url ~ "purge" ) { | |
purge; | |
error 200 "MISS:Cleaning cache and varnish purge, mensaje auto generado"; | |
} | |
} | |
sub vcl_hit { | |
if( req.url ~ "purge" ) { | |
purge; | |
error 200 "HIT:Cleaning cache and varnish purge, mensaje auto generado"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment