0

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.

SAM
  • 641
  • 2
  • 16
  • 30
  • If your Nginx send HTTP redirects Varnish should relay them without a problem, so this should just work without a need to do anything special with Varnish. What happens instead? Do you redefine `vcl_hash` in your configuration? Did you clear cache before testing? – Ludwik Trammer Nov 04 '13 at 19:58
  • And about the cookie: you can just always explicitly set cookie domain to `.domain.com` while saving the cookie, this will work while setting it on `www.domain.com` also. – Ludwik Trammer Nov 04 '13 at 20:01
  • @LudwikTrammer Thats exactly I did. I had to explicitly set the `req.http.host` to `www.domain.com` Thanks – SAM Nov 06 '13 at 04:27
  • 1
    If you are replaying to the second part of my comment - I meant while setting the cookie - in PHP, Django, Rails, Node or whatever you use as your backend. You can explicitly set cookie domain to `.domain.com`, even when you are setting it from `www.domain.com` - that would give you consistent cookie across subdomains, without the need for redirects at all. – Ludwik Trammer Nov 06 '13 at 08:18
  • @LudwikTrammer Sure I got that. But there is some bug in varnish VCL. Which is written by somebody else. Its manipulating the cookie. Ideally, varnish should not modify the cookie. I had to hard code the Host name in VCL. – SAM Nov 07 '13 at 10:15

1 Answers1

2

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;
  }
  // ...
}
NITEMAN
  • 1,236
  • 10
  • 11
  • Thanks. I had to explicitly set the `req.http.host` to `www.domain.com` Moreover I was looking for rewrite as there is no direct mechanism of redirect in varnish. – SAM Nov 06 '13 at 04:29