Saif Bechan
Saif Bechan

Reputation: 17121

A regular expression to find domains that will be cached by Varnish

I ask this question here because I think this is more a regex question than an actual varnish question.

What I basically want to do, is to define a list of domains that varnish will cache. There is already an answer given to this, but I want to use a different approach.

Varnish: cache only specific domain

Now the code that is used in this answer is the following:

sub vcl_recv {
   # dont cache foo.com or bar.com - optional www
   if (req.http.host ~ "(www)?\.(foo|bar)\.com") {
     pass;
   }
   # cache foobar.com - optional www
   if ( req.http.host ~ "(www)?\.foobar\.com" ) {
     lookup;
   }
}

Now what I want is a little different. I have names with different TLD, but I only want to have the non-www version of the domain cached.

So only mydomain.com, or myotherdomain.nl, or yadomain.net

Any other subdomain can be passed to the backend.

Upvotes: 0

Views: 665

Answers (1)

Josha Inglis
Josha Inglis

Reputation: 1048

If I'm reading your question correctly you have a list with multiple versions of the same website, so both www.foobar.com AND/OR foobar.com and you want to match ONLY foobar.com

If so, you need to back reference with (?<!www). So a search that would only match domains without the preceding www would be (?<!www\.)((?:[^.\s]+)\.(?:com|net|nl))

Hope that helps

EDIT

(?<!www\.)((?:[^.\s]+)\.(?:\w{2,3})) if domains are unknown

Upvotes: 5

Related Questions