]> git.sesse.net Git - remoteglot/blob - default.vcl
Another small display fix.
[remoteglot] / default.vcl
1 // Varnish configuration snippets.
2
3 vcl 4.0;
4
5 # You can have multiple ones; see vcl_recv.
6 backend analysis {
7     .host = "127.0.0.1";
8     .port = "5000";
9 }
10
11 sub vcl_recv {
12     if (req.restarts == 0) {
13         if (req.http.x-forwarded-for) {
14             set req.http.X-Forwarded-For =
15                 req.http.X-Forwarded-For + ", " + client.ip;
16         } else {
17             set req.http.X-Forwarded-For = client.ip;
18         }
19     }
20     if (req.http.host ~ "analysis\.sesse\.net$" && req.url ~ "^/analysis\.pl") {
21         set req.backend_hint = analysis;
22         # Ignored by the backend; just to identify it in vcl_backend_response.
23         set req.http.x-analysis-backend = "backend1";
24         return (hash);
25     }
26     # You can check on e.g. /analysis2\.pl here if you have multiple
27     # backends; just remember to set x-analysis-backend to something unique.
28 }
29
30 sub vcl_deliver { 
31     if (resp.http.x-analysis) {
32         set resp.http.Date = now;
33         unset resp.http.X-Varnish;
34         unset resp.http.Via;
35         unset resp.http.Age;
36         unset resp.http.X-Powered-By;
37     }
38     unset resp.http.x-analysis;
39     unset resp.http.x-analysis-backend;
40 }
41
42 sub vcl_hash {
43     hash_data(regsub(req.url, "unique=.*$", ""));
44     if (req.http.host) {
45         hash_data(req.http.host);
46     } else {
47         hash_data(server.ip);
48     }
49     return (lookup);
50 }
51
52 sub vcl_backend_response {
53     if (bereq.http.host ~ "analysis") {
54         set beresp.ttl = 1m;
55         if (beresp.http.content-type ~ "text" || beresp.http.content-type ~ "json") {
56              set beresp.do_gzip = true;
57         }
58         if (bereq.url ~ "^/hash/") {
59              set beresp.ttl = 5s;
60              set beresp.http.x-analysis = 1;
61              set beresp.http.x-analysis-backend = bereq.http.x-analysis-backend;
62              return (deliver);
63         }
64         if (beresp.http.content-type ~ "json") {
65              set beresp.http.x-analysis = 1;
66              set beresp.http.x-analysis-backend = bereq.http.x-analysis-backend;
67              ban ( "obj.http.x-analysis == 1 && " +
68                    "obj.http.x-analysis-backend == " + bereq.http.x-analysis-backend + " && " +
69                    "obj.http.x-rglm != " + beresp.http.x-rglm );
70         }
71         return (deliver);
72     }
73 }