]> Sergey Matveev's repositories - public-inbox.git/blob - examples/varnish-4.vcl
examples: add varnish-4.vcl
[public-inbox.git] / examples / varnish-4.vcl
1 # Example VCL for Varnish 4.0 with public-inbox WWW code
2 # This is based on what shipped for 3.x a long time ago (I think)
3 # and I'm hardly an expert in VCL (nor should we expect anybody
4 # who maintains a public-inbox HTTP interface to be).
5 #
6 # It seems to work for providing some protection from traffic
7 # bursts; but perhaps the public-inbox WWW interface can someday
8 # provide enough out-of-the-box performance that configuration
9 # of an extra component is pointless.
10
11 vcl 4.0;
12 backend default {
13         .host = "127.0.0.1";
14         .port = "280";
15 }
16
17 sub vcl_recv {
18         if (req.restarts == 0) {
19                 if (req.http.x-forwarded-for) {
20                         set req.http.X-Forwarded-For =
21                         req.http.X-Forwarded-For + ", " + client.ip;
22                 } else {
23                         set req.http.X-Forwarded-For = client.ip;
24                 }
25         }
26         if (req.method != "GET" &&
27                         req.method != "HEAD" &&
28                         req.method != "PUT" &&
29                         req.method != "POST" &&
30                         req.method != "TRACE" &&
31                         req.method != "OPTIONS" &&
32                         req.method != "DELETE") {
33                 /* Non-RFC2616 or CONNECT which is weird. */
34                 return (pipe);
35         }
36         if (req.method != "GET" && req.method != "HEAD") {
37                 /* We only deal with GET and HEAD by default */
38                 return (pass);
39         }
40         if (req.http.Authorization || req.http.Cookie) {
41                 /* Not cacheable by default */
42                 return (pass);
43         }
44         return (hash);
45 }
46
47 sub vcl_hash {
48         hash_data(req.url);
49         if (req.http.host) {
50                 hash_data(req.http.host);
51         } else {
52                 hash_data(server.ip);
53         }
54         if (req.http.X-Forwarded-Proto) {
55                 hash_data(req.http.X-Forwarded-Proto);
56         }
57         return (lookup);
58 }
59
60 sub vcl_backend_response {
61         set beresp.grace = 60s;
62         set beresp.do_stream = true;
63         if (beresp.ttl <= 0s ||
64                 beresp.http.Set-Cookie ||
65                 beresp.http.Vary == "*") {
66                 /* Mark as "Hit-For-Pass" for the next 2 minutes */
67                 set beresp.ttl = 120 s;
68                 set beresp.uncacheable = true;
69                 return (deliver);
70         } else {
71                 set beresp.ttl = 10s;
72         }
73         return (deliver);
74 }