1# This is the VCL configuration Varnish will automatically append to your VCL
2# file during compilation/loading. See the vcl(7) man page for details on syntax
3# and semantics.
4# New users is recommended to use the example.vcl file as a starting point.
5
6vcl 4.0;
7
8backend foo { .host = "192.168.1.1"; }
9
10probe blatti { .url = "foo"; }
11probe fooy {
12	.url = "beh";
13
14}
15
16acl foo {
17	"192.168.1.1";
18	"192.168.0.0"/24;
19	! "192.168.0.1";
20}
21
22include "foo.vcl";
23
24import std;
25
26sub vcl_init {
27	new b = director.foo();
28}
29
30sub vcl_recv {
31	ban(req.url ~ "foo");
32	rollback();
33}
34sub vcl_recv {
35    if (req.method == "PRI") {
36	/* We do not support SPDY or HTTP/2.0 */
37	return (synth(405));
38    }
39    if (req.method != "GET" &&
40      req.method != "HEAD" &&
41      req.method != "PUT" &&
42      req.method != "POST" &&
43      req.method != "TRACE" &&
44      req.method != "OPTIONS" &&
45      req.method != "DELETE") {
46        /* Non-RFC2616 or CONNECT which is weird. */
47        return (pipe);
48    }
49
50    if (req.method != "GET" && req.method != "HEAD") {
51        /* We only deal with GET and HEAD by default */
52        return (pass);
53    }
54    if (req.http.Authorization || req.http.Cookie) {
55        /* Not cacheable by default */
56        return (pass);
57    }
58    return (hash);
59}
60
61sub vcl_pipe {
62    # By default Connection: close is set on all piped requests, to stop
63    # connection reuse from sending future requests directly to the
64    # (potentially) wrong backend. If you do want this to happen, you can undo
65    # it here.
66    # unset bereq.http.connection;
67    return (pipe);
68}
69
70sub vcl_pass {
71    return (fetch);
72}
73
74sub vcl_hash {
75    hash_data(req.url);
76    if (req.http.host) {
77        hash_data(req.http.host);
78    } else {
79        hash_data(server.ip);
80    }
81    return (lookup);
82}
83
84sub vcl_purge {
85    return (synth(200, "Purged"));
86}
87
88sub vcl_hit {
89    if (obj.ttl >= 0s) {
90        // A pure unadultered hit, deliver it
91        return (deliver);
92    }
93    if (obj.ttl + obj.grace > 0s) {
94        // Object is in grace, deliver it
95        // Automatically triggers a background fetch
96        return (deliver);
97    }
98    // fetch & deliver once we get the result
99    return (miss);
100}
101
102sub vcl_miss {
103    return (fetch);
104}
105
106sub vcl_deliver {
107    set resp.http.x-storage = storage.s0.free;
108    return (deliver);
109}
110
111/*
112 * We can come here "invisibly" with the following errors: 413, 417 & 503
113 */
114sub vcl_synth {
115    set resp.http.Content-Type = "text/html; charset=utf-8";
116    set resp.http.Retry-After = "5";
117    synthetic( {"<!DOCTYPE html>
118<html>
119  <head>
120    <title>"} + resp.status + " " + resp.reason + {"</title>
121  </head>
122  <body>
123    <h1>Error "} + resp.status + " " + resp.reason + {"</h1>
124    <p>"} + resp.reason + {"</p>
125    <h3>Guru Meditation:</h3>
126    <p>XID: "} + req.xid + {"</p>
127    <hr>
128    <p>Varnish cache server</p>
129  </body>
130</html>
131"} );
132    return (deliver);
133}
134
135#######################################################################
136# Backend Fetch
137
138sub vcl_backend_fetch {
139    return (fetch);
140}
141
142sub vcl_backend_response {
143    if (beresp.ttl <= 0s ||
144      beresp.http.Set-Cookie ||
145      beresp.http.Surrogate-control ~ "no-store" ||
146      (!beresp.http.Surrogate-Control &&
147        beresp.http.Cache-Control ~ "no-cache|no-store|private") ||
148      beresp.http.Vary == "*") {
149        /*
150        * Mark as "Hit-For-Pass" for the next 2 minutes
151        */
152        set beresp.ttl = 120s;
153        set beresp.uncacheable = true;
154    }
155    return (deliver);
156}
157
158sub vcl_backend_error {
159    set beresp.http.Content-Type = "text/html; charset=utf-8";
160    set beresp.http.Retry-After = "5";
161    synthetic( {"<!DOCTYPE html>
162<html>
163  <head>
164    <title>"} + beresp.status + " " + beresp.reason + {"</title>
165  </head>
166  <body>
167    <h1>Error "} + beresp.status + " " + beresp.reason + {"</h1>
168    <p>"} + beresp.reason + {"</p>
169    <h3>Guru Meditation:</h3>
170    <p>XID: "} + bereq.xid + {"</p>
171    <hr>
172    <p>Varnish cache server</p>
173  </body>
174</html>
175"} );
176    return (deliver);
177}
178
179#######################################################################
180# Housekeeping
181
182sub vcl_init {
183}
184
185sub vcl_fini {
186    return (ok);
187}
188