« Back to Index

[Varnish VCL] use vcl to store provided query string and path into HTTP response headers

View original Gist on GitHub

Tags: #tags: vcl, varnish

Varnish VCL replace store query string and path into HTTP response headers.cs

vcl 4.0;

backend default { 
  .host = "www.vclfiddle.net"; 
  .port = "80"; 
}

sub vcl_recv {
  if (req.url ~ "(foo/bar)") {
    set req.http.X-QS = regsub(req.url, "^[^?]+\?", ""); // good=hey&bad1=true&bad2=boop
    set req.http.X-Path = regsub(req.url, "\?.+$", "");  // /foo/bar
  }
}

/*
curl http://www.vclfiddle.net/foo/bar?good=hey&bad1=true&bad2=boop --header 'User-Agent: vclFiddle'
http://vclfiddle.net/161102-3c995c5/44
*/