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

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.

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.

3. Using | to Match Multiple Options

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

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.

 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.

 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!

Ryan Frankel

Questions or Comments? Ask Ryan!

Ask a question and Ryan will respond to you. We strive to provide the best advice on the net and we are here to help you in any way we can.

  • Ryan,

    Thanks for help!

    • frankel0

      No problem Fahad!

  • dfseGoat

    Is this up to date for varnish 4.0? Very hard to know which of the guides out there are current since varnish changed regex engines

    • frankel0

      This should be up-to-date and ready to rock for Varnish 4.0. If you have any trouble let us know!

  • Rehaan

    Is this correct?? Shouldn’t it also match /blog/aa ?? Thanks…

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

    • Chris Davies

      You’re looking section 4, Wildcard Matching? As an RE, “^/blog/.+” would match “/blog/a” or “/blog/aa”, but not “/blog/”. It looks like this is a typo in the article itself.

  • Wendy Wang

    Hi Ryan, Thanks for the great article! I want to know about: if create a page in the wp-content/theme/, the next step is to create the page in admin area. Is the page should be created in wp-admin? If yes, is this page will be same page copied from wp-content/theme page? Thanks.

    • frankel0

      Hey Wendy…was this question from this Varnish article?

      As far as your question goes…if you create a page template in /themes/yourtheme/ then you would create a Page in the admin area to actually use that template.

      * For example:
      In /themes/yourtheme/mynewpage.php
      /*
      * Template Name: My New Template
      */

      Then in the Admin area, when you go to create a new Page there should be the option to use “My New Template”

      – – –

      I hope that helps.

      • Wendy Wang

        Thank you for the quick response!!

  • Hardik Prajapati

    For query string purge, i end up using “req.url ~ image.jpg/?x=10”. what if want to ban url having & and , (comma)…? Is there any list have to escaped for ? because requirement is to ban query string individually. Means I have to handle all the cases require for query string.