|
| 1 | +vcl 4.0; |
| 2 | +import querystring; |
| 3 | + |
| 4 | +backend default { |
| 5 | + .host = "BACKEND_HOST"; |
| 6 | + .port = "BACKEND_PORT"; |
| 7 | +} |
| 8 | + |
| 9 | +sub vcl_recv { |
| 10 | + |
| 11 | + #Allow for cache to be by passed for dev |
| 12 | + if ( req.url ~ "^/(.*)\?no-cache" ) { |
| 13 | + return (pass); |
| 14 | + } |
| 15 | + |
| 16 | + #unset all cookies regardless of what they are. We current don't need any cookies for explore |
| 17 | + unset req.http.cookie; |
| 18 | +} |
| 19 | + |
| 20 | +# The data on which the hashing will take place |
| 21 | +sub vcl_hash { |
| 22 | + # Called after vcl_recv to create a hash value for the request. This is used as a key |
| 23 | + # to look up the object in Varnish. |
| 24 | + |
| 25 | + hash_data(req.url); |
| 26 | + |
| 27 | + if (req.http.host) { |
| 28 | + hash_data(req.http.host); |
| 29 | + } else { |
| 30 | + hash_data(server.ip); |
| 31 | + } |
| 32 | + |
| 33 | + # hash cookies for requests that have them |
| 34 | + if (req.http.Cookie) { |
| 35 | + hash_data(req.http.Cookie); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +sub vcl_hit { |
| 40 | + # Called when a cache lookup is successful. |
| 41 | + |
| 42 | + if (obj.ttl >= 0s) { |
| 43 | + # A pure unadultered hit, deliver it |
| 44 | + return (deliver); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +sub vcl_miss { |
| 51 | + # Called after a cache lookup if the requested document was not found in the cache. Its purpose |
| 52 | + # is to decide whether or not to attempt to retrieve the document from the backend, and which |
| 53 | + # backend to use. |
| 54 | + |
| 55 | + return (fetch); |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +sub vcl_backend_response { |
| 61 | + |
| 62 | + # Pause ESI request and remove Surrogate-Control header |
| 63 | + if (beresp.http.Surrogate-Control ~ "ESI/1.0") { |
| 64 | + unset beresp.http.Surrogate-Control; |
| 65 | + set beresp.do_esi = true; |
| 66 | + } |
| 67 | + |
| 68 | + # Enable cache for all static files |
| 69 | + # The same argument as the static caches from above: monitor your cache size, if you get data nuked out of it, consider giving up the static file cache. |
| 70 | + # Before you blindly enable this, have a read here: https://ma.ttias.be/stop-caching-static-files/ |
| 71 | + if (bereq.url ~ "^[^?]*\.(7z|avi|bmp|bz2|css|csv|doc|docx|eot|flac|flv|gif|gz|ico|jpeg|jpg|js|less|mka|mkv|mov|mp3|mp4|mpeg|mpg|odt|otf|ogg|ogm|opus|pdf|png|ppt|pptx|rar|rtf|svg|svgz|swf|tar|tbz|tgz|ttf|txt|txz|wav|webm|webp|woff|woff2|xls|xlsx|xml|xz|zip)(\?.*)?$") { |
| 72 | + unset beresp.http.set-cookie; |
| 73 | + } |
| 74 | + |
| 75 | + # Large static files are delivered directly to the end-user without |
| 76 | + # waiting for Varnish to fully read the file first. |
| 77 | + # Varnish 4 fully supports Streaming, so use streaming here to avoid locking. |
| 78 | + if (bereq.url ~ "^[^?]*\.(7z|avi|bz2|flac|flv|gz|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|rar|tar|tgz|tbz|txz|wav|webm|xz|zip)(\?.*)?$") { |
| 79 | + unset beresp.http.set-cookie; |
| 80 | + set beresp.do_stream = true; # Check memory usage it'll grow in fetch_chunksize blocks (128k by default) if the backend doesn't send a Content-Length header, so only enable it for big objects |
| 81 | + } |
| 82 | + |
| 83 | + # Sometimes, a 301 or 302 redirect formed via Apache's mod_rewrite can mess with the HTTP port that is being passed along. |
| 84 | + # This often happens with simple rewrite rules in a scenario where Varnish runs on :80 and Apache on :8080 on the same box. |
| 85 | + # A redirect can then often redirect the end-user to a URL on :8080, where it should be :80. |
| 86 | + # This may need finetuning on your setup. |
| 87 | + # |
| 88 | + # To prevent accidental replace, we only filter the 301/302 redirects for now. |
| 89 | + if (beresp.status == 301 || beresp.status == 302) { |
| 90 | + set beresp.http.Location = regsub(beresp.http.Location, ":[0-9]+", ""); |
| 91 | + } |
| 92 | + |
| 93 | + # Set 2min cache if unset for static files |
| 94 | + if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") { |
| 95 | + set beresp.ttl = 120s; # Important, you shouldn't rely on this, SET YOUR HEADERS in the backend |
| 96 | + set beresp.uncacheable = true; |
| 97 | + return (deliver); |
| 98 | + } |
| 99 | + |
| 100 | + # Don't cache 50x responses |
| 101 | + if (beresp.status == 500 || beresp.status == 502 || beresp.status == 503 || beresp.status == 504) { |
| 102 | + return (abandon); |
| 103 | + } |
| 104 | + |
| 105 | + # Allow stale content, in case the backend goes down. |
| 106 | + # make Varnish keep all objects for 6 hours beyond their TTL |
| 107 | + set beresp.grace = 6h; |
| 108 | + |
| 109 | + return (deliver); |
| 110 | +} |
| 111 | + |
| 112 | +# The routine when we deliver the HTTP request to the user |
| 113 | +# Last chance to modify headers that are sent to the client |
| 114 | +sub vcl_deliver { |
| 115 | + # Called before a cached object is delivered to the client. |
| 116 | + |
| 117 | + # Remove some headers: PHP version |
| 118 | + unset resp.http.X-Powered-By; |
| 119 | + |
| 120 | + # Remove some headers: Apache version & OS |
| 121 | + unset resp.http.Server; |
| 122 | + unset resp.http.X-Drupal-Cache; |
| 123 | + unset resp.http.X-Varnish; |
| 124 | + unset resp.http.Via; |
| 125 | + unset resp.http.Link; |
| 126 | + unset resp.http.X-Generator; |
| 127 | + |
| 128 | + return (deliver); |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | +sub vcl_fini { |
| 133 | + # Called when VCL is discarded only after all requests have exited the VCL. |
| 134 | + # Typically used to clean up VMODs. |
| 135 | + |
| 136 | + return (ok); |
| 137 | +} |
0 commit comments