aboutsummaryrefslogtreecommitdiffstats
path: root/web/etc/varnish/default.vcl
blob: 8462e0118eea3029fe46f3b3e5d9c07374bd50d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.

# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

acl einstein {
    "localhost";    # myself
    "185.12.59.12"; # and everyone on the local network
    "2a02:ed02:1337::12";
}

sub vcl_recv {
	if (req.url ~ "nightMode") {
		set req.url = regsub(req.url, "nightMode","");
		set req.url = req.url + "?nightMode";
	}
    # Happens before we check if we have this in cache already.
    # 
    # Typically you clean up the request here, removing cookies you don't need,
    # rewriting the request, etc.
if (req.restarts == 0) {
  if (req.http.X-Forwarded-For) {
    set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
  } else {
    set req.http.X-Forwarded-For = client.ip;
  }
}

	if (client.ip ~ einstein){
		set req.http.x-einstein = "true";
	} else {
		set req.http.x-einstein = "false";
	}

    if (req.http.host ~ "stream") {
	    return (pass);
    }

    if (req.http.host ~ "nms-public"){
	    return (pass);
    }

     if (req.method != "GET" &&
       req.method != "HEAD" &&
       req.method != "PUT" &&
       req.method != "POST" &&
       req.method != "TRACE" &&
       req.method != "OPTIONS" &&
       req.method != "DELETE") {
         /* Non-RFC2616 or CONNECT which is weird. */
         return (pipe);
     }
 
     if (req.method != "GET" && req.method != "HEAD") {
         /* We only deal with GET and HEAD by default */
         return (pass);
     }
    
     unset req.http.Cookie;
     if (req.http.Cookie) {
         /* Not cacheable by default */
         return (pass);
     }

     return (hash);
 }
sub vcl_hash {
    hash_data(req.http.x-einstein);
    hash_data(req.http.authorization);
}
sub vcl_backend_response {
    # Happens after we have read the response headers from the backend.
    # 
    # Here you clean the response headers, removing silly Set-Cookie headers
    # and other mistakes your backend does.
    if (!(bereq.http.host ~ "stream")) {
	    if (beresp.status == 200) {
		    set beresp.ttl = 2s;
	    } else {
		    set beresp.ttl = 0s;
	    }
		    if(bereq.url ~ "port-state.pl" && beresp.status == 200) {
			set beresp.ttl = 30s;
			}
	    if (beresp.status == 500) {
		    return (retry);
	    }
    }

}

sub vcl_deliver {
    # Happens when we have all the pieces we need, and are about to send the
    # response to the client.
    # 
    # You can do accounting or modifying the final object here.
}
sub vcl_backend_error {
     set beresp.http.Content-Type = "text/html; charset=utf-8";
     set beresp.http.Retry-After = "5";
     synthetic( {"<!DOCTYPE html>
 <html>
   <head>
     <title>"} + beresp.status + " " + beresp.reason + {"</title>
     <meta http-equiv="refresh" content="1">
   </head>
   <body>
     <h1>Error "} + beresp.status + " " + beresp.reason + {"</h1>
     <p>"} + beresp.reason + {"</p>
     <h3>Guru Meditation:</h3>
     <p>XID: "} + bereq.xid + {"</p>
     <hr>
     <p>Totally not a Varnish cache server errror</p>
   </body>
 </html>
 "} );
     return (deliver);
 }