-
Notifications
You must be signed in to change notification settings - Fork 40
/
wordpress-example.vcl
124 lines (104 loc) · 3.37 KB
/
wordpress-example.vcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
backend default {
.host = "127.0.0.1";
.port = "8080";
}
import std;
include "lib/xforward.vcl";
include "lib/cloudflare.vcl";
include "lib/purge.vcl";
include "lib/bigfiles.vcl"; # Varnish 3.0.3+
#include "lib/bigfiles_pipe.vcl"; # Varnish 3.0.2
include "lib/static.vcl";
acl cloudflare {
# set this ip to your Railgun IP (if applicable)
# "1.2.3.4";
}
acl purge {
"localhost";
"127.0.0.1";
}
# Pick just one of the following:
# (or don't use either of these if your application is "adaptive")
# include "lib/mobile_cache.vcl";
# include "lib/mobile_pass.vcl";
### WordPress-specific config ###
# This config was initially derived from the work of Donncha Ó Caoimh:
# http://ocaoimh.ie/2011/08/09/speed-up-wordpress-with-apache-and-varnish/
sub vcl_recv {
# pipe on weird http methods
if (req.request !~ "^GET|HEAD|PUT|POST|TRACE|OPTIONS|DELETE$") {
return(pipe);
}
### Check for reasons to bypass the cache!
# never cache anything except GET/HEAD
if (req.request != "GET" && req.request != "HEAD") {
return(pass);
}
# don't cache logged-in users or authors
if (req.http.Cookie ~ "wp-postpass_|wordpress_logged_in_|comment_author|PHPSESSID") {
return(pass);
}
# don't cache ajax requests
if (req.http.X-Requested-With == "XMLHttpRequest") {
return(pass);
}
# don't cache these special pages
if (req.url ~ "nocache|wp-admin|wp-(comments-post|login|activate|mail)\.php|bb-admin|server-status|control\.php|bb-login\.php|bb-reset-password\.php|register\.php") {
return(pass);
}
### looks like we might actually cache it!
# fix up the request
set req.grace = 2m;
set req.url = regsub(req.url, "\?replytocom=.*$", "");
# Remove has_js, Google Analytics __*, and wooTracker cookies.
set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(__[a-z]+|has_js|wooTracker)=[^;]*", "");
set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");
if (req.http.Cookie ~ "^\s*$") {
unset req.http.Cookie;
}
return(lookup);
}
sub vcl_hash {
# Add the browser cookie only if a WordPress cookie found.
if (req.http.Cookie ~ "wp-postpass_|wordpress_logged_in_|comment_author|PHPSESSID") {
hash_data(req.http.Cookie);
}
}
sub vcl_fetch {
# make sure grace is at least 2 minutes
if (beresp.grace < 2m) {
set beresp.grace = 2m;
}
# catch obvious reasons we can't cache
if (beresp.http.Set-Cookie) {
set beresp.ttl = 0s;
}
# Varnish determined the object was not cacheable
if (beresp.ttl <= 0s) {
set beresp.http.X-Cacheable = "NO:Not Cacheable";
return(hit_for_pass);
# You don't wish to cache content for logged in users
} else if (req.http.Cookie ~ "wp-postpass_|wordpress_logged_in_|comment_author|PHPSESSID") {
set beresp.http.X-Cacheable = "NO:Got Session";
return(hit_for_pass);
# You are respecting the Cache-Control=private header from the backend
} else if (beresp.http.Cache-Control ~ "private") {
set beresp.http.X-Cacheable = "NO:Cache-Control=private";
return(hit_for_pass);
# You are extending the lifetime of the object artificially
} else if (beresp.ttl < 300s) {
set beresp.ttl = 300s;
set beresp.grace = 300s;
set beresp.http.X-Cacheable = "YES:Forced";
# Varnish determined the object was cacheable
} else {
set beresp.http.X-Cacheable = "YES";
}
# Avoid caching error responses
if (beresp.status == 404 || beresp.status >= 500) {
set beresp.ttl = 0s;
set beresp.grace = 15s;
}
# Deliver the content
return(deliver);
}