VCL language
Poul-Henning Kamp
phk at phk.freebsd.dk
Thu Mar 30 08:40:26 CEST 2006
In message <1543.193.213.34.102.1143676134.squirrel at denise.vg.no>, "Anders Berg" writes:
I think a large part of the solution is to come up with some clear
examples to show people how to do things.
Is this an improvement ?
1 backend default {
2 set backend.ip = 10.0.0.1;
3 }
4
5 proc main {
6 if (req.request != "GET" && req.request != "HEAD") {
7 pass_through;
8 }
9 lookup_object;
10 if (obj.valid) {
11 finish;
12 }
13 fetch;
14 if (obj.url ~ "[.]hbs$") {
15 obj.ttl = 5m;
16 obj.cacheable = true;
17 } elif (obj.url ~ "[.]jpg$" || obj.url ~ "[.]gif$") {
18 obj.ttl = 1w;
19 obj.cacheable = true;
20 } elif (obj.url ~ "/poor_written") {
21 obj.ttl = 5m;
22 obj.cacheable = true;
23 }
24 if (obj.cacheable) {
25 insert;
26 }
27 }
14,17,20: a '.' in a regexp means "any character", putting it in [.]
is a readable way to escape it. Because we use strings
enclosed in double-quotes, the backslash-escape would be
"\\.jpg$". The trailing '$' is to avoid false matches.
Of course, we could also add a
if (obj.suffix = ".hbs")
if we wanted to make this easier/faster.
15,16: We could let the "obj.cacheable = true" be implicit when
obj.ttl is set, I can't really see why we shouldn't ?
17: Another way to write it could be:
if (obj.content_type ~ "image/") {
So with those changes it would look like this:
1 backend default {
2 set backend.ip = 10.0.0.1;
3 }
4
5 proc main {
6 if (req.request != "GET" && req.request != "HEAD") {
7 pass_through;
8 }
9 lookup_object;
10 if (obj.valid) {
11 finish;
12 }
13 fetch;
14 if (obj.suffix = ".hbs") {
15 obj.ttl = 5m;
16 } if (obj.content_type ~ "image/") {
17 obj.ttl = 1w;
18 } elif (obj.url ~ "/poor_written") {
19 obj.ttl = 5m;
20 }
21 if (obj.cacheable) {
22 insert;
23 }
24 }
--
Poul-Henning Kamp | UNIX since Zilog Zeus 3.20
phk at FreeBSD.ORG | TCP/IP since RFC 956
FreeBSD committer | BSD since 4.3-tahoe
Never attribute to malice what can adequately be explained by incompetence.
More information about the varnish-dev
mailing list