Created
February 19, 2014 11:31
-
-
Save rubendob/9090291 to your computer and use it in GitHub Desktop.
varnish for WP
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 = ""; | |
.port = "80"; | |
.connect_timeout = 300s; | |
.first_byte_timeout = 600s; | |
.between_bytes_timeout = 300s; | |
.max_connections = 512; | |
} | |
acl purge { | |
"localhost"; | |
"127.0.0.1"; | |
} | |
sub vcl_recv { | |
set req.http.X-Full-Uri = req.http.host + req.url; | |
if (req.http.host == "example.com" || | |
req.http.host == "example2.com"){ | |
set req.backend = default; | |
} | |
set req.grace = 1d; | |
remove req.http.X-Forwarded-For; | |
set req.http.X-Forwarded-For = client.ip; | |
if (req.request == "PURGE") { | |
if (!client.ip ~ purge) { | |
error 405 "Method Not Allowed"; | |
} | |
return(lookup); | |
} | |
if (req.request != "GET" && req.request != "HEAD" && req.request != "POST") { | |
return (error); | |
} | |
if (req.request == "POST") { | |
return (pipe); | |
} | |
if (req.http.Authenticate || req.http.Authorization) { | |
return (pass); | |
} | |
if (req.url ~ "\.(7z|bmp|bz2|csv|doc|docx|epub|gif|gz|ico|jpeg|jpg|key|log|mp3|odp|ods|odt|ogg|pdf|png|pps|ppt|pptx|ps|psd|rar|rtf|svg|tar|tbz|tgz|tif|tiff|vcf|xls|xlsx|zip)$") { | |
unset req.http.Cookie; | |
unset req.http.Accept-Encoding; | |
return(lookup); | |
} | |
if (req.url ~ "\.(css|js|sql|txt|xml)$") { | |
unset req.http.Cookie; | |
} | |
if (req.http.Accept-Encoding) { | |
if (req.http.Accept-Encoding ~ "gzip") { | |
set req.http.Accept-Encoding = "gzip"; | |
set req.http.gzip = "1"; | |
} else if (req.http.Accept-Encoding ~ "deflate") { | |
set req.http.Accept-Encoding = "deflate"; | |
set req.http.deflate = "1"; | |
} else { | |
unset req.http.Accept-Encoding; | |
} | |
} | |
if (req.url ~ "/wp-login.php" || req.url ~ "/wp-admin/") { | |
return(pass); | |
} | |
if (req.http.Cookie ~ "wordpress_logged_in_") { | |
return (pass); | |
} | |
if (req.url ~ "/xmlrpc.php"){ | |
return(pipe); | |
} | |
if (req.url ~ "/wp-config.php"){ | |
return(error); | |
} | |
return (lookup); | |
} | |
sub vcl_pipe { | |
return (pipe); | |
} | |
sub vcl_pass { | |
return (pass); | |
} | |
sub vcl_hash { | |
hash_data(req.url); | |
if (req.http.host) { | |
hash_data(req.http.host); | |
} else { | |
hash_data(server.ip); | |
} | |
return (hash); | |
} | |
sub vcl_hit { | |
return (deliver); | |
} | |
sub vcl_miss { | |
return (fetch); | |
} | |
sub vcl_fetch { | |
set beresp.grace = 1d; | |
if (req.url ~ "\.(7z|bmp|bz2|csv|doc|docx|epub|gif|gz|ico|jpeg|jpg|key|log|mp3|odp|ods|odt|ogg|pdf|png|pps|ppt|pptx|ps|psd|rar|rtf|svg|tar|tbz|tgz|tif|tiff|vcf|xls|xlsx|zip)$") { | |
unset beresp.http.Set-Cookie; | |
set beresp.http.Cache-Control = "public, max-age=2592000"; | |
set beresp.ttl = 1m; | |
} else if (req.url ~ "\.(css|js|sql|txt|xml)$") { | |
unset beresp.http.Set-Cookie; | |
unset beresp.http.ETag; | |
set beresp.http.Cache-Control = "public, max-age=2592000"; | |
set beresp.http.Vary = "Accept-Encoding"; | |
set beresp.ttl = 1m; | |
} else { | |
unset beresp.http.ETag; | |
set beresp.http.Vary = "Accept-Encoding"; | |
set beresp.ttl = 1d; | |
} | |
return(deliver); | |
} | |
sub vcl_deliver { | |
if (obj.hits > 0) { | |
set resp.http.X-Cache = "HIT"; | |
} else { | |
set resp.http.X-Cache = "MISS"; | |
} | |
if (resp.http.gzip) { | |
unset resp.http.gzip; | |
set resp.http.Content-Encoding = "gzip"; | |
} | |
if (resp.http.deflate) { | |
unset resp.http.deflate; | |
set resp.http.Content-Encoding = "deflate"; | |
} | |
unset resp.http.Server; | |
unset resp.http.Via; | |
unset resp.http.X-Powered-By; | |
unset resp.http.X-Varnish; | |
return (deliver); | |
} | |
sub vcl_pipe { | |
return (pipe); | |
} | |
sub vcl_pass { | |
return (pass); | |
} | |
sub vcl_hash { | |
hash_data(req.url); | |
if (req.http.host) { | |
hash_data(req.http.host); | |
} else { | |
hash_data(server.ip); | |
} | |
return (hash); | |
} | |
sub vcl_hit { | |
return (deliver); | |
} | |
sub vcl_miss { | |
return (fetch); | |
} | |
sub vcl_fetch { | |
set beresp.grace = 1d; | |
if (req.url ~ "\.(7z|bmp|bz2|csv|doc|docx|epub|gif|gz|ico|jpeg|jpg|key|log|mp3|odp|ods|odt|ogg|pdf|png|pps|ppt|pptx|ps|psd|rar|rtf|svg|tar|tbz|tgz|tif|tiff|vcf|xls|xlsx|zip)$") { | |
unset beresp.http.Set-Cookie; | |
set beresp.http.Cache-Control = "public, max-age=2592000"; | |
set beresp.ttl = 1m; | |
} else if (req.url ~ "\.(css|js|sql|txt|xml)$") { | |
unset beresp.http.Set-Cookie; | |
unset beresp.http.ETag; | |
set beresp.http.Cache-Control = "public, max-age=2592000"; | |
set beresp.http.Vary = "Accept-Encoding"; | |
set beresp.ttl = 1m; | |
} else { | |
unset beresp.http.ETag; | |
set beresp.http.Vary = "Accept-Encoding"; | |
set beresp.ttl = 1d; | |
} | |
return(deliver); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment