forked from pkhamre/wp-varnish
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wordpress.vcl
172 lines (151 loc) · 5.13 KB
/
wordpress.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
backend default {
.host = "127.0.0.1";
.port = "8080";
.connect_timeout = 300s;
.first_byte_timeout = 300s;
.between_bytes_timeout = 300s;
}
acl purge {
"localhost";
"127.0.0.1";
}
# xforward.vcl -- X-Forwarded-For HTTP Headers
# This should generally be loaded first to make sure that the headers
# get set appropriately for all requests. Note that when using this
# you MUST NOT fall through to the VCL default handler for vcl_recv
# since that will run the code again, resulting in the client.ip
# being added twice.
sub vcl_recv {
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;
}
}
}
# static.vcl -- Static File Caching for Varnish
sub vcl_recv {
if (req.request ~ "^(GET|HEAD)$" && req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)(\?.*)?$") {
if (req.url ~ "nocache") {
return(pass);
}
set req.url = regsub(req.url, "\?.*$", "");
unset req.http.Cookie;
set req.grace = 2m;
return(lookup);
}
}
sub vcl_fetch {
if (req.request ~ "^(GET|HEAD)$" && req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$") {
unset beresp.http.set-cookie;
set beresp.ttl = 24h;
set beresp.grace = 2m;
}
}
### 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 {
if (req.request == "BAN") {
if(!client.ip ~ purge) {
error 405 "Not allowed.";
}
ban("req.url ~ "+req.url+" && req.http.host == "+req.http.host);
error 200 "Banned.";
}
# 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 preview cache WP/GF
if (req.url ~ "preview=true" || req.url ~ "gf_page=preview") {
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 {
# Uncomment to make the default cache "time to live" is 24 hours, handy
# but it may cache stale pages unless purged.
# By default Varnish will use the headers sent to it by Apache (the backend server)
# to figure out the correct TTL.
set beresp.ttl = 24h;
# 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);
}
#Comment this out if you don't want to see weather there was a HIT or MISS in the headers
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}