Skip to content
Eric Rasmussen edited this page Dec 22, 2016 · 5 revisions

Setting up Varnish for local development

We'll run varnish on :80 and apache on :8080

  1. Run:
brew install varnish
  1. Copy https://git.unl.edu/tsteiner2/unl-cms-varnish-config/blob/master/unlcms.vcl to /usr/local/etc/varnish/unlcms.vcl and change the port on at the top to 8080:
vcl 4.0;

import std;

backend default {
       .host = "127.0.0.1";
       .port = "8080";
       .connect_timeout = 600s;
       .first_byte_timeout = 600s;
       .between_bytes_timeout = 600s;
}
  1. Modify /usr/local/etc/varnish/unlcms.vcl and add the following to the appropriate existing subroutines:
acl purge {
   "127.0.0.1";
}

sub vcl_recv {
 # Only allow PURGE requests from IP addresses in the 'purge' ACL.
 if (req.method == "PURGE") {
   if (!client.ip ~ purge) {
     return (synth(405, "Not allowed."));
   }
   return (purge);
 }

 # Only allow BAN requests from IP addresses in the 'purge' ACL.
 if (req.method == "BAN") {
  	# Same ACL check as above:
   if (!client.ip ~ purge) {
     return (synth(403, "Not allowed."));
   }

   # Logic for the ban, using the Cache-Tags header. For more info
   # see https://github.com/geerlingguy/drupal-vm/issues/397.
   if (req.http.Cache-Tags) {
     ban("obj.http.Cache-Tags ~ " + req.http.Cache-Tags);
   }
   else {
     return (synth(403, "Cache-Tags header missing."));
   }

   # Throw a synthetic page so the request won't go to the backend.
   return (synth(200, "Ban added."));
 }
}

sub vcl_backend_response {
 set beresp.http.X-Url = bereq.url;
 set beresp.http.X-Host = bereq.http.host;
}
  1. edit apache conf and change it to:
Listen 8080
  1. Run in the foreground:
sudo /usr/local/sbin/varnishd -n /usr/local/var/varnish -f /usr/local/etc/varnish/unlcms.vcl -s malloc,1G -T 127.0.0.1:2000 -a 0.0.0.0:80 -F
  1. Enjoy

Notes:

Clone this wiki locally