Last active
December 31, 2015 05:49
-
-
Save seyhunak/7943277 to your computer and use it in GitHub Desktop.
Varnish VCL
This file contains 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 = "8080"; | |
.connect_timeout = 600s; | |
.first_byte_timeout = 600s; | |
.between_bytes_timeout = 600s; | |
} | |
director default round-robin { | |
{ .backend = server1; } | |
} | |
sub vcl_recv { | |
if (req.http.Cookie) { | |
set req.http.Cookie = ";" + req.http.Cookie; | |
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";"); | |
set req.http.Cookie = regsuball(req.http.Cookie, ";(varnishLogged)=", "; \1="); | |
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", ""); | |
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", ""); | |
if (req.http.Cookie == "") { | |
unset req.http.Cookie; | |
} | |
} | |
if (req.http.Accept-Encoding) { | |
if (req.http.Accept-Encoding ~ "gzip") { | |
set req.http.Accept-Encoding = "gzip"; | |
} else if (req.http.Accept-Encoding ~ "deflate") { | |
set req.http.Accept-Encoding = "deflate"; | |
} else { | |
unset req.http.Accept-Encoding; | |
} | |
} | |
set req.grace = 3h; | |
set req.http.X-Forwarded-For = client.ip; | |
if (req.request != "GET" && | |
req.request != "HEAD" && | |
req.request != "PUT" && | |
req.request != "POST" && | |
req.request != "TRACE" && | |
req.request != "PURGE" && | |
req.request != "OPTIONS" && | |
req.request != "DELETE") { | |
return (pipe); | |
} | |
if (req.http.Cache-Control ~ "no-cache") { | |
return (pass); | |
} | |
if ( req.url ~ "/admin" ) { | |
set req.http.X-request-matched = "admin"; | |
return (pass); | |
} | |
if (req.http.Cookie ~ "(varnishLogged)=") { | |
set req.http.X-request-matched = "cookieFound"; | |
return (pass); | |
} | |
if (req.http.Authorization) { | |
return (pass); | |
} | |
return (lookup); | |
} | |
sub vcl_hash { | |
if (req.url ~ "\?") { | |
set req.url=regsuball(req.url,"&(_|utm_term|utm_content|utm_source|utm_medium|utm_campaign)=([A-z0-_\-.]+)",""); | |
set req.url=regsuball(req.url,"\?(_|utm_term|utm_content|utm_source|utm_medium|utm_campaign)=([A-z0-_\-.]+)","?"); | |
set req.url=regsub(req.url,"\?&","?"); | |
set req.url=regsub(req.url,"\?$",""); | |
} | |
hash_data(req.url); | |
} | |
sub vcl_fetch { | |
set beresp.grace = 3h; | |
if (beresp.status >= 400) { | |
set beresp.ttl = 5s; | |
set beresp.grace = 0s; | |
} | |
if (beresp.status != 200) { | |
return (hit_for_pass); | |
} | |
if (req.http.Cookie ~ "varnishLogged") { | |
set beresp.http.X-Cacheable = "no-cookieFound"; | |
return (hit_for_pass); | |
} elsif (beresp.http.Cache-Control ~ "private") { | |
set beresp.http.X-Cacheable = "no-cache-control=private"; | |
return (hit_for_pass); | |
} else { | |
if (beresp.http.Set-Cookie == "") { | |
remove beresp.http.Set-Cookie; | |
} | |
} | |
set beresp.http.Vary = "Accept-Encoding"; | |
} | |
sub vcl_deliver { | |
set resp.http.X-Served-By = server.hostname; | |
if (obj.hits > 0) { | |
set resp.http.X-Cache-Result = "HIT"; | |
set resp.http.X-Cache-Hits = obj.hits; | |
} else { | |
set resp.http.X-Cache-Result = "MISS"; | |
} | |
} | |
sub vcl_hit { | |
if (req.request == "PURGE") { | |
purge; | |
error 200 "Purged"; | |
} | |
} | |
sub vcl_miss { | |
if (req.request == "PURGE") { | |
purge; | |
error 200 "Not in cache"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment