public

Redirecting Bad Referers with Varnish

I hate all that crap in my referrals in my analytics software!  So finally I got pissed and updated my Varnish Cache .vcl to show those jerks... well... something less

9 years ago

Latest Post Maximizing DevOps Efficiency: Best Practices, KPIs, and Realtime Feedback by Mike Moore public

I hate all that crap in my referrals in my analytics software!  So finally I got pissed and updated my Varnish Cache .vcl to show those jerks... well... something less than pleasant!

Check it out (also note this is VCL 4.0):

sub bad_referrals { 
     # Just add the text in the referring domain you want to match, and voila! 
     if ( req.http.referer ~ "hulfingtonpost" || 
          req.http.referer ~ "forum.topic57969834.darodar" || 
          req.http.referer ~ "ilovevitaly" || 
          req.http.referer ~ "priceg" || 
          req.http.referer ~ "blackhatworth.com" 
     ) { 
          set req.http.host = "www.ihateyousomuchomgroflcats.com"; 
          return(synth(750, "All your referer are belong to us.")); 
     } 
} 

sub vcl_recv { 
     # Piss off bad referrers 
     call bad_referrals; 
     # ADD THE REST HERE # 
} 

sub vcl_synth { 
     # Bad referrers are gonna see something they really don't want to see! 
     if (resp.status == 750) { 
         set resp.status = 301; 
         set resp.http.Location = "http://allyourreferrerarebelongtome.com/"; 
         return(deliver); 
     } 
}

Tada! Add your bad referrers and send them straight somewhere they're really not gonna wanna go!

Mike Moore

Published 9 years ago