├── .travis.yml ├── README.md ├── runtests.sh ├── v4 ├── stale-if-error.vcl └── xff.vcl └── v5 ├── stale-if-error.vcl └── xff.vcl /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: trusty 2 | sudo: required 3 | language: generic 4 | env: 5 | - VER=4.1 6 | - VER=5.0 7 | - VER=5.1 8 | before_install: 9 | - curl -s https://packagecloud.io/install/repositories/varnishcache/varnish${VER/./}/script.deb.sh | sudo bash 10 | - sudo apt-get install varnish=${VER}.* 11 | script: 12 | - ./runtests.sh $VER 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | vcl-snippets 2 | ============ 3 | 4 | [![Build Status](https://travis-ci.org/fgsch/vcl-snippets.svg?branch=master)](https://travis-ci.org/fgsch/vcl-snippets) 5 | 6 | A series of snippets for Varnish Cache divided by major version. 7 | 8 | How to use 9 | ---------- 10 | 11 | Download the file with the required functionality and include it in the configuration using: 12 | 13 | ``` 14 | include ""; 15 | ``` 16 | 17 | Alternatively copy and paste the code into the configuration. 18 | 19 | Please note that Varnish will concatenate the subroutines as they appear in the file so the order *does* matter. 20 | -------------------------------------------------------------------------------- /runtests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Run every snippet through varnishd 4 | 5 | t=$(mktemp) 6 | trap 'rm -f $t' 0 7 | for f in $(echo v${1%%.?}/*.vcl); do 8 | cat >$t <<_EOF 9 | vcl 4.0; 10 | backend default { .host = "127.0.0.1"; } 11 | include "$(pwd)/$f"; 12 | _EOF 13 | echo -n "Snippet $f " 14 | varnishd -n $(pwd) -C -f $t 2>/dev/null 15 | if [ $? -eq 0 ]; then 16 | echo "passed" 17 | else 18 | echo "FAILED" 19 | status=2 20 | fi 21 | done 22 | exit $status 23 | -------------------------------------------------------------------------------- /v4/stale-if-error.vcl: -------------------------------------------------------------------------------- 1 | sub try_stale_if_error { 2 | if (obj.ttl < 0s && obj.ttl + obj.grace > 0s) { 3 | if (req.restarts == 0) { 4 | set req.http.sie-enabled = true; 5 | return (fetch); 6 | } else { 7 | set req.http.sie-abandon = true; 8 | return (deliver); 9 | } 10 | } 11 | } 12 | 13 | sub vcl_backend_fetch { 14 | if (bereq.http.sie-abandon) { 15 | return (abandon); 16 | } 17 | } 18 | 19 | sub vcl_backend_response { 20 | if (beresp.status > 400 && bereq.http.sie-enabled) { 21 | return (abandon); 22 | } 23 | } 24 | 25 | sub vcl_backend_error { 26 | if (bereq.http.sie-enabled) { 27 | return (abandon); 28 | } 29 | } 30 | 31 | sub vcl_synth { 32 | if (resp.status == 503 && req.http.sie-enabled) { 33 | unset req.http.sie-enabled; 34 | return (restart); 35 | } 36 | } 37 | 38 | sub vcl_hit { 39 | call try_stale_if_error; 40 | } 41 | -------------------------------------------------------------------------------- /v4/xff.vcl: -------------------------------------------------------------------------------- 1 | # Remove everything but the first ip address from X-Forwarded-For. 2 | # This normally means removing the Varnish ip address. 3 | sub vcl_recv { 4 | set req.http.X-Forwarded-For = 5 | regsub(req.http.X-Forwarded-For, ",.*", ""); 6 | } 7 | -------------------------------------------------------------------------------- /v5/stale-if-error.vcl: -------------------------------------------------------------------------------- 1 | sub try_stale_if_error { 2 | if (obj.ttl < 0s && obj.ttl + obj.grace > 0s) { 3 | if (req.restarts == 0) { 4 | set req.http.sie-enabled = true; 5 | return (miss); 6 | } else { 7 | set req.http.sie-abandon = true; 8 | return (deliver); 9 | } 10 | } 11 | } 12 | 13 | sub vcl_backend_fetch { 14 | if (bereq.http.sie-abandon) { 15 | return (abandon); 16 | } 17 | } 18 | 19 | sub vcl_backend_response { 20 | if (beresp.status > 400 && bereq.http.sie-enabled) { 21 | return (abandon); 22 | } 23 | } 24 | 25 | sub vcl_backend_error { 26 | if (bereq.http.sie-enabled) { 27 | return (abandon); 28 | } 29 | } 30 | 31 | sub vcl_synth { 32 | if (resp.status == 503 && req.http.sie-enabled) { 33 | unset req.http.sie-enabled; 34 | return (restart); 35 | } 36 | } 37 | 38 | sub vcl_hit { 39 | call try_stale_if_error; 40 | } 41 | -------------------------------------------------------------------------------- /v5/xff.vcl: -------------------------------------------------------------------------------- 1 | ../v4/xff.vcl --------------------------------------------------------------------------------