Created
February 12, 2016 16:06
-
-
Save KristianLyng/81d8f6f627357786fe41 to your computer and use it in GitHub Desktop.
Minimal Varnish VCL suitable for most needs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This is a perfectly good template to get started with Varnish. | |
| # Well behaved sites will get pretty decent results from this, and it | |
| # should work for the vast majority of web sites out there. | |
| # This bit is mandatory. It will say "4.0" even for Varnish 4.1. | |
| # (don't ask) | |
| vcl 4.0; | |
| # This is where your content lives. Adjust it to point at your web server. | |
| backend foo { | |
| .host = "127.0.0.1"; | |
| .port = "8080"; | |
| } | |
| # The only thing stopping Varnish from caching properly by default in most | |
| # cases is the presence of cookies. Strip them, and voila, cache works. | |
| sub vcl_recv { | |
| # We leave cookies for content under "/user". | |
| if (req.url !~ "^/user") { | |
| unset req.http.cookie; | |
| } | |
| } | |
| # That's all you need, but you might want to start adjusting cache duration | |
| # too! You can do that by emitting "Cache-Control: s-maxage=123" from your | |
| # backend server, telling Varnish to cache for 123 seconds. That requires 0 | |
| # configuration, but the following snippet removes "s-maxage" from the | |
| # response before it is sent to the client, so as not to confuse other | |
| # proxy servers between you and the client. | |
| sub strip_smaxage { | |
| # Remove white space | |
| set beresp.http.cache-control = regsuball(beresp.http.cache-control, " ",""); | |
| # strip s-maxage - Varnish has already used it | |
| set beresp.http.cache-control = regsub(beresp.http.cache-control, "s-maxage=[0-9]+\b",""); | |
| # Strip extra commas | |
| set beresp.http.cache-control = regsub(beresp.http.cache-control, "(^,|,$|,,)", ""); | |
| } | |
| # This just calls the above function at the appropriate time. | |
| sub vcl_backend_response { | |
| call strip_smaxage; | |
| } | |
| # You can read more about control Varnish through headers at | |
| # https://varnishfoo.info/chapter-2.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment