Skip to content

Instantly share code, notes, and snippets.

@ijin
Last active December 31, 2015 05:29
Show Gist options
  • Save ijin/7941009 to your computer and use it in GitHub Desktop.
Save ijin/7941009 to your computer and use it in GitHub Desktop.
Server Festa 2013 Autumn backend Varnish default.vcl
import std;
import memcached;
import curl;
import parsereq;
import throttle;
import header;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
backend wppost {
.host = "base";
.port = "8081";
}
sub vcl_recv {
parsereq.init();
if (req.url ~ "^/(wp-content/themes/|wp-includes/js)") {
unset req.http.cookie;
}
if (req.url ~ "^/ban" ) {
error 200 "ban!";
}
if (req.url ~ "/wp-comments-post.php.*" && req.request == "POST") {
set req.backend = wppost;
unset req.http.cookie;
return (pass);
}
if (req.url ~ "^/\?page_id=.*") {
}
if (req.url ~ "^/\?p=.*") {
unset req.http.cookie;
}
if (req.url ~ "^/redirect" ) {
}
if (req.restarts == 0) {
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For + ", " + client.ip;
} else {
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 != "OPTIONS" &&
req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}
return (lookup);
}
sub vcl_fetch {
set beresp.http.x-url = req.url;
if (req.url ~ "^/?p=.*") {
unset beresp.http.set-cookie;
set beresp.ttl = 120s;
}
if (req.url ~ "^/(ban)" ) {
# set req.backend = default;
set beresp.ttl = 0s;
}
if (req.url ~ "/wp-comments-post.php.*" && req.request == "POST") {
unset beresp.http.set-cookie;
header.append(beresp.http.Set-Cookie, "comment_author_26f1ea38dbef3c2503ce207774eb83dd=Cloud+Server+Festa+Bench; expires=Sun, 02-Nov-2014 11:41:03 GMT; Max-Age=30000000; path=/");
header.append(beresp.http.Set-Cookie, "comment_author_email_26f1ea38dbef3c2503ce207774eb83dd=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/");
header.append(beresp.http.Set-Cookie, "comment_author_url_26f1ea38dbef3c2503ce207774eb83dd=http%3A%2F%2Fserverfesta.info%2F; expires=Sun, 02-Nov-2014 11:41:03 GMT; Max-Age=30000000; path=/");
set beresp.ttl = 0s;
}
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Vary == "*") {
/*
* Mark as "Hit-For-Pass" for the next 2 minutes
*/
set beresp.ttl = 120 s;
return (hit_for_pass);
}
return (deliver);
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
if (req.url ~ "^/ban" ) {
set resp.http.ban = "v1";
ban("obj.http.x-url == /");
ban("obj.http.x-url ~ /\?page_id=");
ban("obj.http.x-url ~ /\?p=");
}
if (req.url ~ "/wp-comments-post.php.*" && req.request == "POST") {
set resp.http.page = parsereq.post_header("comment_post_ID");
set resp.http.x-insert = "http://localhost:8080/insert?" + "comment_post_ID=" +
resp.http.page + "&comment_author=" + parsereq.post_header("author") +
"&comment_author_email=EMAIL" + "&comment_author_url=" + parsereq.post_header("url") +
"&comment_author_IP=127.0.0.1" + "&comment_content=" + parsereq.post_header("comment") +
"&comment_karma=0" + "&comment_approved=1" + "&comment_agent=varnish" + "&comment_type=0" +
"&comment_parent=0" + "&user_id=0";
###memcached.servers("base");
###set resp.http.count = memcached.incr(resp.http.page, 1);
ban("obj.http.x-url == /");
ban("obj.http.x-url ~ /\?page_id=");
ban("obj.http.x-url ~ /\?p=");
curl.get("http://svfes02/ban");
curl.get("http://svfes03/ban");
curl.get("http://svfes04/ban");
unset resp.http.x-insert;
set resp.http.Content-Type = "text/html";
set resp.http.Cache-Control = "no-cache, must-revalidate, max-age=0";
set resp.http.Pragma = "no-cache";
set resp.http.Expires = "Wed, 11 Jan 1984 05:00:00 GMT";
set resp.status = 302;
set resp.response = "Found";
}
return (deliver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment