Question

I need to rewrite domain.com to www.domain.com using varnish. I have already done that for Nginx. But no Idea how to do in varnish.

Let me explain why I want to do that, if my approach is wrong then please correct me. Whenever I hit the site with domain.com I have a cookie with path .domain.com. Whenever I hit the path with www.domain.com it creates a new cookie with path .www.domain.com Now these two cookies for same user is creating session issues.

I am hoping that if my servers only receive request like www.domain.com then there will be no scope of cookie associated with domain.com

So Ideally, Question is, is rewrite a good approach ? If yes then how to do that with varnish, I have already tried with Nginx but no luck.

Any guidance is deeply appreciated. Thanks guys.

Was it helpful?

Solution

Answering the "rewrite" part (please see the comments as that could be easier), you have 2 options:

  1. Perform a client redirect (preferred IMHO) [a]
  2. Rewrite the host internally [b]

See also:

[a]

sub vcl_recv {
  // ...
  if ( req.http.host == "domain.com" ) {
    error 750 "http://www." + req.http.host + req.url;
  }
  // ...
}

sub vcl_error {
  // ...
  if (obj.status == 750) {
    set obj.http.Location = obj.response;
    # Set HTTP 301 for permanent redirect
    set obj.status = 301;
    return(deliver);
  }
  // ...
}

[b]

sub vcl_recv {
  // ...
  if ( req.http.host == "domain.com" ) {
    set req.http.host = "http://www." + req.http.host;
  }
  // ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top