]> Sergey Matveev's repositories - public-inbox.git/blob - examples/varnish-4.vcl
5fc202ed4f36bfa3b66ce3fe8d8e3266c97d2a3f
[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         # this is where public-inbox-httpd listens
14         .host = "127.0.0.1";
15         .port = "280";
16 }
17
18 sub vcl_recv {
19         /* pipe POST and any other weird methods directly to backend */
20         if (req.method != "GET" && req.method != "HEAD") {
21                 return (pipe);
22         }
23         if (req.http.Authorization || req.http.Cookie) {
24                 /* Not cacheable by default */
25                 return (pass);
26         }
27         return (hash);
28 }
29
30 sub vcl_pipe {
31         # By default Connection: close is set on all piped requests by varnish,
32         # but public-inbox-httpd supports persistent connections well :)
33         unset bereq.http.connection;
34         return (pipe);
35 }
36
37 sub vcl_hash {
38         hash_data(req.url);
39         if (req.http.host) {
40                 hash_data(req.http.host);
41         } else {
42                 hash_data(server.ip);
43         }
44         /* we generate fully-qualified URLs for Atom feeds and redirects */
45         if (req.http.X-Forwarded-Proto) {
46                 hash_data(req.http.X-Forwarded-Proto);
47         }
48         return (lookup);
49 }
50
51 sub vcl_backend_response {
52         set beresp.grace = 60s;
53         set beresp.do_stream = true;
54         if (beresp.ttl <= 0s ||
55                 /* no point in caching stuff git already stores on disk */
56                 beresp.http.Content-Type ~ "application/x-git" ||
57                 beresp.http.Set-Cookie ||
58                 beresp.http.Vary == "*") {
59                 /* Mark as "Hit-For-Pass" for the next 2 minutes */
60                 set beresp.ttl = 120 s;
61                 set beresp.uncacheable = true;
62                 return (deliver);
63         } else {
64                 /* short TTL for up-to-dateness, our PSGI is not that slow */
65                 set beresp.ttl = 10s;
66         }
67         return (deliver);
68 }