summaryrefslogtreecommitdiffstats
path: root/roles/varnish/templates/proxy.vcl.j2
blob: c261e4e4189cfab2f91b64ca76ba4561cae15e67 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
vcl 4.0;

import directors;

backend wiki {
    .host = "localhost";
    .port = "10001";
    .first_byte_timeout = 120s;
}

backend mirrorlists {
    .host = "localhost";
    .port = "10002";
}

backend pkgdb {
    .host = "localhost";
    .port = "10003";
    .first_byte_timeout = 160s;
}

backend fas01 {
    .host = "fas01";
    .port = "http";
    .probe = {
        .url = "/accounts/";
        .interval = 5s;
        .timeout = 5s;
        .window = 5;
        .threshold = 5;
    }
}

backend fas02 {
    .host = "fas02";
    .port = "http";
    .probe = {
        .url = "/accounts/";
        .interval = 5s;
        .timeout = 5s;
        .window = 5;
        .threshold = 5;
    }
}

backend fas03 {
    .host = "fas03";
    .port = "http";
    .probe = {
        .url = "/accounts/";
        .interval = 5s;
        .timeout = 5s;
        .window = 5;
        .threshold = 5;
    }
}

sub vcl_init {
  new fas = directors.round_robin();
  fas.add_backend(fas01);
  fas.add_backend(fas02);
  fas.add_backend(fas03);
}

backend nuancier {
    .host = "localhost";
    .port = "10035";
}

backend voting {
    .host = "localhost";
    .port = "10007";
    .first_byte_timeout = 160s;
}

backend mirrormanager {
    .host = "localhost";
    .port = "10008";
}

backend bodhi {
    .host = "localhost";
    .port = "10009";
}

backend freemedia {
    .host = "localhost";
    .port = "10011";
}

backend packages {
    .host = "localhost";
    .port = "10016";
}

backend tagger {
    .host = "localhost";
    .port = "10017";
}

backend askbot {
    .host = "localhost";
    .port = "10021";
}

backend blockerbugs {
    .host = "localhost";
    .port = "10022";
}

backend fedocal {
    .host = "localhost";
    .port = "10023";
}

backend kerneltest {
    .host = "localhost";
    .port = "10038";
}

backend paste {
    .host = "localhost";
    .port = "10027";
}

backend koschei {
    .host = "localhost";
    .port = "10040";
}


acl purge {
    "192.168.1.129"; // wiki01.vpn
    "192.168.1.130"; // wiki02.vpn
    "10.5.126.60"; // wiki01.stg
    "10.5.126.63"; // wiki01
    "10.5.126.73"; // wiki02
    "10.5.126.23"; // lockbox01
    "192.168.1.58"; //lockbox01.vpn
}

sub vcl_synth {
    set resp.http.Content-Type = "text/html; charset=utf-8";
    set resp.http.Retry-After = "5";
    synthetic( {"<!DOCTYPE html>
<html>
  <head>
    <title>"} + resp.status + " " + resp.reason + {"</title>
  </head>
  <body>
    <h1>Error "} + resp.status + " " + resp.reason + {"</h1>
    <p>"} + resp.reason + {"</p>
    <h3>Guru Meditation:</h3>
    <p>XID: "} + req.xid + {"</p>
    <hr>
    <p>Varnish cache server on {{ inventory_hostname }}</p>
  </body>
</html>
"} );
    return (deliver);
}

# CAUTIN: Make very sure that for every unset req.http.cookie, you
# also set a req.http.clear-cookies = "yes", and in *no other case*.
# This is done to prevent the case where Varnish drops the cookies
# because this is the efficient thing to do to allow more caching,
# but then the backend sends a new session cookies back, overwriting
# the one the browser had already.
sub vcl_recv {
    if (req.method == "PURGE") {
        if (!client.ip ~ purge) {
            return (synth(405, "Not allowed"));
        }
        return(purge);
    }

    if (req.url ~ "^/wiki/") {
        set req.backend_hint = wiki;
    }
    if (req.url ~ "^/w/") {
        set req.backend_hint = wiki;
        if (req.url ~ "^/w/skins/") {
            unset req.http.cookie;
            set req.http.clear-cookies = "yes";
            set req.url = regsub(req.url, "\?.*", "");
        }
    }
    if (req.url ~ "^/mirrorlist/") {
        set req.backend_hint = mirrorlists;
    }
    if (req.url ~ "^/pkgdb") {
        set req.backend_hint = pkgdb;
        if (req.url ~ "^/pkgdb/static/") {
            unset req.http.cookie;
            set req.http.clear-cookies = "yes";
            set req.url = regsub(req.url, "\?.*", "");
        }
    }
    if (req.url ~ "^/accounts/") {
        set req.backend_hint = fas.backend();
        if (req.url ~ "^/accounts/static/") {
            unset req.http.cookie;
            set req.http.clear-cookies = "yes";
            set req.url = regsub(req.url, "\?.*", "");
        }
    }
    if (req.url ~ "^/voting/") {
        set req.backend_hint = voting;
        if (req.url ~ "^/voting/static/") {
            unset req.http.cookie;
            set req.http.clear-cookies = "yes";
            set req.url = regsub(req.url, "\?.*", "");
        }
    }
    if (req.url ~ "^/mirrormanager") {
        set req.backend_hint = mirrormanager;
        if (req.url ~ "^/mirrormanager/static/") {
            unset req.http.cookie;
            set req.http.clear-cookies = "yes";
            set req.url = regsub(req.url, "\?.*", "");
        }
        if (req.url ~ "^/mirrormanager/mirrors") {
            unset req.http.cookie;
            set req.http.clear-cookies = "yes";
            set req.url = regsub(req.url, "\?.*", "");
        }
    }
    if (req.url ~ "^/updates/") {
        set req.backend_hint = bodhi;
        if (req.url ~ "^/updates/static/") {
            unset req.http.cookie;
            set req.http.clear-cookies = "yes";
            set req.url = regsub(req.url, "\?.*", "");
        }
    }
    if (req.url ~ "^/freemedia/") {
        set req.backend_hint = freemedia;
    }
    if (req.url ~ "^/tagger/") {
        set req.backend_hint = tagger;
        if (req.url ~ "^/tagger/ui/static/") {
            unset req.http.cookie;
            set req.http.clear-cookies = "yes";
            set req.url = regsub(req.url, "\?.*", "");
        }
    }
    if (req.http.X-Forwarded-Server ~ "^paste.fedoraproject.org") {
        set req.backend_hint = paste;
        if (req.url ~ "^/skins/") {
            unset req.http.cookie;
            set req.http.clear-cookies = "yes";
            set req.url = regsub(req.url, "\?.*", "");
        }
        if (req.url ~ "^/addons/") {
            unset req.http.cookie;
            set req.http.clear-cookies = "yes";
            set req.url = regsub(req.url, "\?.*", "");
        }
    }
    if (req.http.X-Forwarded-Server ~ "^ask.fedoraproject.org") {
        set req.backend_hint = askbot;
        if (req.url ~ "^/m/") {
            unset req.http.cookie;
            set req.http.clear-cookies = "yes";
            set req.url = regsub(req.url, "\?.*", "");
        }
    }
    if (req.http.X-Forwarded-Server ~ "^qa.fedoraproject.org") {
        if (req.url ~ "^/blockerbugs") {
             set req.backend_hint = blockerbugs;
            if (req.url ~ "^/blockerbugs/static/") {
                unset req.http.cookie;
                set req.http.clear-cookies = "yes";
                set req.url = regsub(req.url, "\?.*", "");
            }
        }
    }
    if (req.http.X-Forwarded-Server ~ "^apps.fedoraproject.org" || req.http.X-Forwarded-Server ~ "^apps.stg.fedoraproject.org") { 
        if (req.url ~ "^/koschei") {
            set req.backend_hint = koschei;
            if (req.url ~ "^/koschei/static/") {
                unset req.http.cookie;
                set req.http.clear-cookies = "yes";
                set req.url = regsub(req.url, "\?.*", "");
            }
        }
        if (req.url ~ "^/kerneltest") {
            set req.backend_hint = kerneltest;
            if (req.url ~ "^/kerneltest/static/") {
                unset req.http.cookie;
                set req.http.clear-cookies = "yes";
                set req.url = regsub(req.url, "\?.*", "");
            }
        }
        if (req.url ~ "^/calendar") {
            set req.backend_hint = fedocal;
            if (req.url ~ "^/calendar/static/") {
                unset req.http.cookie;
                set req.http.clear-cookies = "yes";
                set req.url = regsub(req.url, "\?.*", "");
            }
        }
        if (req.url ~ "^/nuancier") {
            set req.backend_hint = nuancier;
            if (req.url ~ "^/nuancier/static/") {
                unset req.http.cookie;
                set req.http.clear-cookies = "yes";
                set req.url = regsub(req.url, "\?.*", "");
            }
            if (req.url ~ "^/nuancier/cache/") {
                unset req.http.cookie;
                set req.http.clear-cookies = "yes";
                set req.url = regsub(req.url, "\?.*", "");
            }
        }
        if (req.url ~ "^/packages/") {
            set req.backend_hint = packages;
            if (req.url ~ "^/packages/_res/") {
                unset req.http.cookie;
                set req.http.clear-cookies = "yes";
                set req.url = regsub(req.url, "\?.*", "");
            }
            if (req.url ~ "^/packages/css/") {
                unset req.http.cookie;
                set req.http.clear-cookies = "yes";
                set req.url = regsub(req.url, "\?.*", "");
            }
            if (req.url ~ "^/packages/images/") {
                unset req.http.cookie;
                set req.http.clear-cookies = "yes";
                set req.url = regsub(req.url, "\?.*", "");
            }
            if (req.url ~ "^/packages/js/") {
                unset req.http.cookie;
                set req.http.clear-cookies = "yes";
                set req.url = regsub(req.url, "\?.*", "");
            }
        }
    }

    # Pass any requests with the "If-None-Match" header directly.
    if (req.http.If-None-Match) {
        return (pass);
    }

    # Force lookup if the request is a no-cache request from the client.
#    if (req.http.Cache-Control ~ "no-cache") {
#        purge_url(req.url);
#    }
#    if (req.http.Accept-Encoding) {
#        if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
#            # No point in compressing these
#            remove req.http.Accept-Encoding;
#        } elsif (req.http.Accept-Encoding ~ "gzip") {
#            # This is currently a bug with ipv6, so we need to nuke it.
#            remove req.http.Accept-Encoding;
#        } elsif (req.http.Accept-Encoding ~ "deflate") {
#            set req.http.Accept-Encoding = "deflate";
#        } else {
#            # unknown algorithm
#            remove req.http.Accept-Encoding;
#        }
#    }
}

# When requesting application icons, don't allow cherrypy to set cookies
#sub vcl_backend_fetch {
#    if (req.url ~ "^/pkgdb/appicon/show/") {
#        unset beresp.http.set-cookie;
#    }
#}


# Make sure mirrormanager/mirrors doesn't set any cookies
# (Setting cookies would make varnish store a HIT-FOR-PASS
#  making it always fetch from backend)
sub vcl_backend_response {
    if (bereq.url ~ "^/mirrormanager/mirrors") {
        unset beresp.http.set-cookie;
        set beresp.ttl = 6h;
    }
    if (bereq.url ~ "^/mirrormanager/static/") {
        set beresp.ttl = 6h;
    }
}

sub vcl_deliver {
    if (req.http.clear-cookies ~ "yes") {
        # If we stored that we cleared request cookies, we also make sure
        # we are not sending any response cookies.
        # NOTE: this is suboptimal, since clearing them at this time means
        # varnish has already cached that this endpoint returns cookies and
        # will not cache it. This is really just a failsafe to make sure we
        # don't send empty cookies back.
        unset resp.http.set-cookie;
    }
}

sub vcl_pipe {
        set req.http.connection = "close";
}

sub vcl_hit {
        if (req.method == "PURGE") {
            ban(req.url);
            return (synth(200, "Purged"));
        }
 
        if (!obj.ttl > 0s) {
            return (pass);
        }
}
 
sub vcl_miss {
        if (req.method == "PURGE")  {
            return (synth(200, "Not in cache"));
        }
}