Created
June 3, 2016 07:51
-
-
Save ThijsFeryn/aabd6d7ed425fb6614279e836d9848c5 to your computer and use it in GitHub Desktop.
Typical Wordpress VCL for Varnish 4
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
vcl 4.0; | |
import std; | |
backend default { | |
.host = "127.0.0.1"; | |
.port = "8080"; | |
.max_connections = 300; | |
.first_byte_timeout = 100s; | |
.connect_timeout = 10s; | |
.between_bytes_timeout = 5s; | |
} | |
acl purge { | |
"192.168.33.10"; | |
"localhost"; | |
"127.0.0.1"; | |
"::1"; | |
} | |
sub purge_regex { | |
ban("obj.http.X-Req-URL ~ " + req.url + " && obj.http.X-Req-Host == " + req.http.host); | |
} | |
sub purge_exact { | |
ban("obj.http.X-Req-URL == " + req.url + " && obj.http.X-Req-Host == " + req.http.host); | |
} | |
sub purge_page { | |
set req.url = regsub(req.url, "\?.*$", ""); | |
ban("obj.http.X-Req-URL-Base == " + req.url + " && obj.http.X-Req-Host == " + req.http.host); | |
} | |
sub vcl_recv { | |
set req.http.Host = regsub(req.http.Host, ":[0-9]+", ""); | |
set req.url = std.querysort(req.url); | |
if (req.method == "PURGE") { | |
if (!client.ip ~ purge) { | |
return (synth(405, "Not allowed.")); | |
} | |
if (req.http.X-Purge-Method) { | |
if (req.http.X-Purge-Method ~ "(?i)regex") { | |
call purge_regex; | |
} elsif (req.http.X-Purge-Method ~ "(?i)exact") { | |
call purge_exact; | |
} else { | |
call purge_page; | |
} | |
} else { | |
if (req.url ~ "\.\*" || req.url ~ "^\^" || req.url ~ "\$$" || req.url ~ "\\[.?*+^$|()]") { | |
call purge_regex; | |
} elsif (req.url ~ "\?") { | |
call purge_exact; | |
} else { | |
call purge_page; | |
} | |
} | |
return (synth(200, "Purged.")); | |
} | |
if (req.method != "GET" && | |
req.method != "HEAD" && | |
req.method != "PUT" && | |
req.method != "POST" && | |
req.method != "TRACE" && | |
req.method != "OPTIONS" && | |
req.method != "PATCH" && | |
req.method != "DELETE") { | |
return (pipe); | |
} | |
if (req.method != "GET" && req.method != "HEAD") { | |
return (pass); | |
} | |
if (req.url ~ "(\?|&)(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=") { | |
set req.url = regsuball(req.url, "&(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", ""); | |
set req.url = regsuball(req.url, "\?(utm_source|utm_medium|utm_campaign|utm_content|gclid|cx|ie|cof|siteurl)=([A-z0-9_\-\.%25]+)", "?"); | |
set req.url = regsub(req.url, "\?&", "?"); | |
set req.url = regsub(req.url, "\?$", ""); | |
} | |
if (req.url ~ "\#") { | |
set req.url = regsub(req.url, "\#.*$", ""); | |
} | |
if (req.url ~ "\?$") { | |
set req.url = regsub(req.url, "\?$", ""); | |
} | |
set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", ""); | |
set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", ""); | |
set req.http.Cookie = regsuball(req.http.Cookie, "_ga=[^;]+(; )?", ""); | |
set req.http.Cookie = regsuball(req.http.Cookie, "_gat=[^;]+(; )?", ""); | |
set req.http.Cookie = regsuball(req.http.Cookie, "utmctr=[^;]+(; )?", ""); | |
set req.http.Cookie = regsuball(req.http.Cookie, "utmcmd.=[^;]+(; )?", ""); | |
set req.http.Cookie = regsuball(req.http.Cookie, "utmccn.=[^;]+(; )?", ""); | |
set req.http.Cookie = regsuball(req.http.Cookie, "__gads=[^;]+(; )?", ""); | |
set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", ""); | |
set req.http.Cookie = regsuball(req.http.Cookie, "__atuv.=[^;]+(; )?", ""); | |
set req.http.Cookie = regsuball(req.http.Cookie, "^;\s*", ""); | |
if (req.http.cookie ~ "^\s*$") { | |
unset req.http.cookie; | |
} | |
if (req.url ~ "^[^?]*\.(7z|avi|bmp|bz2|css|csv|doc|docx|eot|flac|flv|gif|gz|ico|jpeg|jpg|js|less|mka|mkv|mov|mp3|mp4|mpeg|mpg|odt|otf|ogg|ogm|opus|pdf|png|ppt|pptx|rar|rtf|svg|svgz|swf|tar|tbz|tgz|ttf|txt|txz|wav|webm|webp|woff|woff2|xls|xlsx|xml|xz|zip)(\?.*)?$") { | |
unset req.http.Cookie; | |
return (hash); | |
} | |
if (req.url ~ "wp-(login|admin)" || req.url ~ "preview=true") { | |
return (pass); | |
} | |
if (req.http.Cookie ~ "wordpress_" || req.http.Cookie ~ "comment_") { | |
return (pass); | |
} | |
if (req.http.Authorization) { | |
return (pass); | |
} | |
return (hash); | |
} | |
sub vcl_hash { | |
if (req.http.Accept-Encoding) { | |
hash_data(req.http.Accept-Encoding); | |
} | |
} | |
sub vcl_backend_response { | |
set beresp.http.X-Req-Host = bereq.http.host; | |
set beresp.http.X-Req-URL = bereq.url; | |
set beresp.http.X-Req-URL-Base = regsub(bereq.url, "\?.*$", ""); | |
if (bereq.url ~ "^[^?]*\.(7z|avi|bmp|bz2|css|csv|doc|docx|eot|flac|flv|gif|gz|ico|jpeg|jpg|js|less|mka|mkv|mov|mp3|mp4|mpeg|mpg|odt|otf|ogg|ogm|opus|pdf|png|ppt|pptx|rar|rtf|svg|svgz|swf|tar|tbz|tgz|ttf|txt|txz|wav|webm|webp|woff|woff2|xls|xlsx|xml|xz|zip)(\?.*)?$") { | |
unset beresp.http.set-cookie; | |
} | |
if (bereq.url ~ "^[^?]*\.(7z|avi|bz2|flac|flv|gz|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|rar|tar|tgz|tbz|txz|wav|webm|xz|zip)(\?.*)?$") { | |
set beresp.do_stream = true; | |
set beresp.do_gzip = false; | |
} | |
if (bereq.url ~ "wp-(login|admin)" || bereq.url ~ "preview=true") { | |
set beresp.uncacheable = true; | |
set beresp.ttl = 120s; | |
return (deliver); | |
} | |
if (!(bereq.url ~ "(wp-login|wp-admin|preview=true)")) { | |
unset beresp.http.set-cookie; | |
} | |
if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") { | |
set beresp.ttl = 120s; | |
set beresp.uncacheable = true; | |
return (deliver); | |
} | |
set beresp.grace = 6h; | |
return (deliver); | |
} | |
sub vcl_deliver { | |
if (obj.hits > 0) { | |
set resp.http.X-Cache = "HIT"; | |
} else { | |
set resp.http.X-Cache = "MISS"; | |
} | |
set resp.http.X-Cache-Hits = obj.hits; | |
unset resp.http.X-Req-Host; | |
unset resp.http.X-Req-URL; | |
unset resp.http.X-Req-URL-Base; | |
unset resp.http.X-Powered-By; | |
unset resp.http.Server; | |
unset resp.http.X-Varnish; | |
unset resp.http.Via; | |
unset resp.http.Link; | |
unset resp.http.X-Generator; | |
return (deliver); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi sir, thanks.
i use your config for my wordpress, but i get message 503 fecth failed.
can you help me ?