5 Basic Tips to Using Regular Expressions (RegEx) in Varnish

Varnish Regex

Even though Varnish is an extremely popular tool out of the box, you will quickly find you will need to customize it for the intricacies of your site.

Whether it be passing search box results, allowing certain cookies, or returning various header responses, the use of regular expressions is prominent in VCL programming.

Varnish does have some quirkiness with its implementation of regular expressions, so we will cover the top five tips.

In Varnish, you can use regular expressions much like any other language. The most common usage is the vcl_recv function to match URLs. In general, you need to escape ,. ?, &, but not /. This can be a point of confusion for some folks, so I will provide a few examples.

Varnish uses Perl-compatible regular expressions, so once you get through these basics, I recommend that you check out the main page for PCRE. Honestly, though, in most cases you will only need simple regular expressions in your VCL.

In addition, to develop and test your regular expressions, I suggest you use a tool such as https://regex101.com/. This can dramatically speed up your development and reduce the number of errors.

1. Exactly Equal and Approximately Equal

The use of == and ~ determine whether to match the string exactly or match if the string appears anywhere in the term. Note that the == is a strict comparison and does not use regular expressions, while ~ always uses regular expressions.

# This would match only the page that was /blog/
req.url == '/blog/'

# This would match /blog/ and anything under that directory. 
#   I.e. /blog/varnish-article/, /blog/wordpress-article/, /how/this-is-a-blog/ etc
req.url ~ 'blog/'

2. Use ^ to Begin a String and $ to End It

Sometimes you only want to match at the beginning or end of a term (or both). Varnish makes this pretty straightforward as well.

# To match at the beginning of a string use ^
req.url ~ "^/blog/" #Matches /blog/article/ but not /article/blog/
# To match at the end of a string use $
req.url ~ "/blog/$" #Matches /article/blog/ but not /blog/article/

3. Using | to Match Multiple Options

Using an OR (|) can provide a powerful method for combining many lines into one.

# You could replace:
if ( 
  req.url ~ "jpg"
 || req.url ~ "png"
 || req.url ~ "css"
 || req.url ~ "js"
){...}

# With:
if ( req.url ~ "(jpg|png|css|js)" ) {...}

4. Wildcard Matching

As with most regular expressions, it can be useful to use ?, *, and + to match characters or strings. This is fully supported by Varnish, too.

req.url ~ "^/blog/*$" 
   # Matches /blog/ with anything all the way to the end of the string
   # Similar to req.url ~ "^/blog/"

req.url ~ "^/blog/.+" 
   # Match /blog/a but not /blog/ or /blog/aa

req.url ~ "^/blog/.?" 
   # Match /blog/ and /blog/a but not /blog/aa

5. Substitution via Regular Expression (regsub and regsuball)

There is no doubt that you will eventually need to replace some text in a term. Varnish has a function called regsub (and its cousin regsuball) that search for a string in a term and replace it with another string.

req.url = regsub( req.url, "/?*", "") 
   # This would strip any query string from the URL

req.http.host =reqsub( req.http.host, "www.", "domain." ) 
   # Would replace www. with domain., 
   # I.E. www.hostingadvice.com would become domain.hostingadvice.com

Conclusion

Varnish makes the use of regular expressions quite simple, and with a little practice, this will become second nature to you. Of course, feel free to leave any questions below, and we will try to answer them as best as we can!

Advertiser Disclosure

HostingAdvice.com is a free online resource that offers valuable content and comparison services to users. To keep this resource 100% free, we receive compensation from many of the offers listed on the site. Along with key review factors, this compensation may impact how and where products appear across the site (including, for example, the order in which they appear). HostingAdvice.com does not include the entire universe of available offers. Editorial opinions expressed on the site are strictly our own and are not provided, endorsed, or approved by advertisers.

Our Editorial Review Policy

Our site is committed to publishing independent, accurate content guided by strict editorial guidelines. Before articles and reviews are published on our site, they undergo a thorough review process performed by a team of independent editors and subject-matter experts to ensure the content’s accuracy, timeliness, and impartiality. Our editorial team is separate and independent of our site’s advertisers, and the opinions they express on our site are their own. To read more about our team members and their editorial backgrounds, please visit our site’s About page.