Skip to content

Instantly share code, notes, and snippets.

@nigoroll
Created February 8, 2019 16:36
Show Gist options
  • Save nigoroll/f96d27fd4bf80d20695494f216dac340 to your computer and use it in GitHub Desktop.
Save nigoroll/f96d27fd4bf80d20695494f216dac340 to your computer and use it in GitHub Desktop.
YAVC: VCL as YAML
# slink@uplex.de 20190208:
#
# mockup how builtin.vcl could look in YAML
#
# * passes yamllint
# * YAML structure (schema) is likely to be wrong / not ideal
#
# NOTE: I am _not_ advocating for anything like this, my personal
# opinion is that is über-ugly
#
# the sole purpose is to get a better picture of how it could look like
---
vcl: 4.0
sub vcl_recv:
- if:
condition:
equals:
- req.method
- "PRI"
then:
- return:
synth:
status: 405
- if:
condition:
and:
- not: req.http.host
- equals:
- req.esi_level
- "0"
- matches:
- req.proto
- "^(?i)HTTP/1.1"
then:
- return:
synth:
status: 400
- if:
condition:
and:
- unequal:
- req.method
- "GET"
- unequal:
- req.method
- "HEAD"
- unequal:
- req.method
- "PUT"
- unequal:
- req.method
- "POST"
- unequal:
- req.method
- "TRACE"
- unequal:
- req.method
- "OPTIONS"
- unequal:
- req.method
- "DELETE"
- unequal:
- req.method
- "PATCH"
then:
return: pipe
- if:
condition:
and:
- unequal:
- req.method
- "GET"
- unequal:
- req.method
- "HEAD"
then:
return: pass
- if:
condition:
or:
- req.http.Authorization
- req.http.Cookie
then:
return: pass
- return: hash
sub vcl_pipe:
- return: pipe
sub vcl_pass:
- return: fetch
sub vcl_hash:
- hash_data:
- req.url
- if:
condition:
- req.http.host
then:
- hash_data:
- req.http.host
else:
- hash_data:
- server.ip
- return: lookup
sub vcl_purge:
- return:
synth:
status: 200
reason: "Purged"
sub vcl_hit:
- if:
condition:
greaterorequal:
- obj.ttl
- 0s
then:
return: deliver
- if:
condition:
greater:
- plus:
- obj.ttl
- obj.grace
- 0s
then:
return: deliver
- return: miss
sub vcl_miss:
- return: fetch
sub vcl_deliver:
- return: deliver
sub vcl_synth:
- set:
resp.http.Content-Type: "text/html; charset=utf-8"
resp.http.Retry-After: "5"
resp.body:
plus:
- >
<!DOCTYPE html>
<html>
<head>
<title>
- resp.status
- " "
- resp.reason
- >
</title>
</head>
<body>
- " <h1>Error "
- resp.status
- " "
- resp.reason
- "</h1>\n <p>"
- resp.reason
- >
</p>
<h3>Guru Meditation:</h3>
- " <p>XID: "
- req.xid
- >
</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
- "\n"
- return: deliver
sub vcl_backend_fetch:
- if:
condition:
equals:
- bereq.method
- "GET"
then:
unset: bereq.body
- return: fetch
sub vcl_backend_response:
- if:
condition:
- bereq.uncacheable
then:
return: deliver
else:
- if:
condition:
or:
- lessorequal:
- beresp.ttl
- 0s
- beresp.http.Set-Cookie
- matches:
- beresp.http.Surrogate-control
- "(?i)no-store"
- and:
- not: beresp.http.Surrogate-Control
- matches:
- beresp.http.Cache-Control
- "(?i:no-cache|no-store|private)"
- equals:
- beresp.http.Vary
- "*"
then:
- set:
beresp.ttl: 120s
beresp.uncacheable: true
- return: deliver
sub vcl_backend_error:
- set:
beresp.http.Content-Type: "text/html; charset=utf-8"
beresp.http.Retry-After: "5"
beresp.body:
plus:
- >
<!DOCTYPE html>
<html>
<head>
<title>
- beresp.status
- " "
- beresp.reason
- >
</title>
</head>
<body>
- " <h1>Error "
- beresp.status
- " "
- beresp.reason
- "</h1>\n <p>"
- beresp.reason
- >
</p>
<h3>Guru Meditation:</h3>
- " <p>XID: "
- req.xid
- >
</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
- "\n"
- return: deliver
sub vcl_init:
- return: ok
sub vcl_fini:
- return: ok
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment