├── .gitignore ├── IDEAS.md ├── README.md ├── all-vhosts.vcl ├── catch-all.vcl ├── default.vcl └── sites-enabled ├── a.example.com.vcl ├── b.example.com.vcl ├── c.example.com.vcl └── d.example.com.vcl /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea -------------------------------------------------------------------------------- /IDEAS.md: -------------------------------------------------------------------------------- 1 | ## Ideas for later 2 | 3 | * Generate VCL per host from simple flat config file, i.e. 4 | 5 | ``` 6 | error_html: /path/to/file.html 7 | ``` 8 | 9 | Would produce: 10 | 11 | ```cpp 12 | sub vcl_error { 13 | set obj.http.Content-Type = "text/html; charset=utf-8"; 14 | synthetic std.fileread("/path/to/file.html"); 15 | return (deliver); 16 | } 17 | ``` 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Varnish Virtual Hosts 2 | 3 | This is Varnish boilerplate configuration. 4 | Its primary purpose is to illustrate the clean way of managing multiple websites in Varnish. 5 | 6 | For each website where you need to implement specific caching rules, add a VCL file under ```sites-enabled``` directory. 7 | You can copy paste from the existing ones, or rename them to match your websites' domain names. 8 | 9 | Read original post on the approach [here](https://www.getpagespeed.com/server-setup/varnish/varnish-virtual-hosts). 10 | 11 | -------------------------------------------------------------------------------- /all-vhosts.vcl: -------------------------------------------------------------------------------- 1 | include "sites-enabled/a.example.com.vcl"; 2 | include "sites-enabled/b.example.com.vcl"; 3 | include "sites-enabled/c.example.com.vcl"; 4 | include "sites-enabled/d.example.com.vcl"; 5 | -------------------------------------------------------------------------------- /catch-all.vcl: -------------------------------------------------------------------------------- 1 | # You may want to include your PURGE handler in this file! 2 | 3 | # This function is used when a request is received from our backend 4 | sub vcl_backend_response { 5 | # Parse ESI request and remove Surrogate-Control header 6 | if (beresp.http.Surrogate-Control ~ "ESI/1.0") { 7 | unset beresp.http.Surrogate-Control; 8 | set beresp.do_esi = true; 9 | } 10 | 11 | # Remove some headers we never want to see 12 | unset beresp.http.Server; 13 | unset beresp.http.X-Powered-By; 14 | } -------------------------------------------------------------------------------- /default.vcl: -------------------------------------------------------------------------------- 1 | vcl 4.0; 2 | 3 | # This is our default backend. 4 | # Most common: nginx listens on port 8080 of the same machine. 5 | backend default { 6 | .host = "127.0.0.1"; 7 | .port = "8080"; 8 | } 9 | 10 | # The following routines will apply to every website: 11 | sub vcl_recv { 12 | # Normalize the header, remove the port (in case you're testing this on various TCP ports) 13 | set req.http.host = regsub(req.http.host, ":[0-9]+", ""); 14 | 15 | # Backend can vary by host. Can be changed in virtual hosts' vcl_recv() 16 | set req.backend_hint = default; 17 | } 18 | 19 | # Include all website specific configuration: 20 | include "all-vhosts.vcl"; 21 | 22 | # Include routines which will apply to every website unless "return" statement 23 | # was used in in website specific configuration: 24 | include "catch-all.vcl"; 25 | -------------------------------------------------------------------------------- /sites-enabled/a.example.com.vcl: -------------------------------------------------------------------------------- 1 | # Varnish configuration file for one.example.com 2 | # Website type: Wordpress 3 | # Description: Illustrates application of website specific cache strategy 4 | 5 | sub vcl_recv { 6 | if (req.http.host == "a.example.com" || req.http.host == "www.a.example.com") { 7 | # Ignore any cookies on the frontend of the website (highly cacheable). 8 | # It is recommended to use Disqus commenting plugin with this cache strategy. 9 | if (req.url !~ "^/wp-(login|admin)") { 10 | unset req.http.cookie; 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /sites-enabled/b.example.com.vcl: -------------------------------------------------------------------------------- 1 | # Varnish configuration file for two.example.com 2 | # Website type: Trac daemon running on port 3050 3 | # Description: Example of putting backend definition into a "virtual host" configuration for Varnish 4 | 5 | backend trac { 6 | .host = "127.0.0.1"; 7 | .port = "3050"; 8 | } 9 | 10 | sub vcl_recv { 11 | if (req.http.host == "b.example.com" || req.http.host == "www.b.example.com") { 12 | set req.backend_hint = trac; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /sites-enabled/c.example.com.vcl: -------------------------------------------------------------------------------- 1 | # Varnish configuration file for three.example.com 2 | # Website type: Wordpress with WooCommerce plugin 3 | # Description: Illustrates application of website specific cache strategy 4 | 5 | sub vcl_recv { 6 | if (req.http.host == "c.example.com" || req.http.host == "www.c.example.com") { 7 | # Bypass cache for checkout pages: 8 | if (req.url ~ "/(cart|my-account|checkout|addons|/?add-to-cart=)") { 9 | return (pass); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /sites-enabled/d.example.com.vcl: -------------------------------------------------------------------------------- 1 | # Varnish configuration file for d.example.com 2 | # Website type: Wordpress with native comment system 3 | # Description: Illustrates application of website specific cache strategy 4 | 5 | sub vcl_recv { 6 | if (req.http.host == "d.example.com" || req.http.host == "www.d.example.com") { 7 | # It is recommended to use Disqus commenting plugin and use "a" example instead. 8 | if (req.http.Cookie !~ "comment_author_|wordpress_(?!test_cookie)|wp-postpass_|woocommerce|wp-resetpass") { 9 | # no Wordpress cookies, nuke the rest! 10 | unset req.http.cookie; 11 | } 12 | } 13 | } 14 | --------------------------------------------------------------------------------