0

I install varnish3.03 with yum! I got a problem with it my varnish config bellow:**

#
#backend weibo {
    .host = "192.168.1.178";
    .port = "8080";
    .connect_timeout=20s;
    .first_byte_timeout=20s;
    .between_bytes_timeout=20s;
}

#backend smth {
    .host = "192.168.1.115";
    .port = "8080";
    .connect_timeout=20s;
    .first_byte_timeout=20s;
    .between_bytes_timeout=20s;
}

#sub vcl_recv {
     if (req.restarts == 0) {
        if (req.http.x-forwarded-for) {
            set req.http.X-Forwarded-For =
                req.http.X-Forwarded-For + ", " + client.ip;
        } else {
            set req.http.X-Forwarded-For = client.ip;
        }
     }
     if (req.request != "GET" &&
       req.request != "HEAD" &&
       req.request != "PUT" &&
       req.request != "POST" &&
       req.request != "TRACE" &&
       req.request != "OPTIONS" &&
       req.request != "DELETE") {
#         /* Non-RFC2616 or CONNECT which is weird. */
         return(pipe);
     }
     if (req.request != "GET" && req.request != "HEAD") {
#         /* We only deal with GET and HEAD by default */
         return(pass);
     }
     if (req.http.Authorization || req.http.Cookie) {
         /* Not cacheable by default */
         return(pass);
     }

     if (req.http.host ~ "^(hk.)?weibo.com"){
            set req.http.host = "hk.weibo.com";
            set req.backend = weibo;
                 }
     elseif (req.http.host ~ "^(www.)?newsmth.net"){
            set req.http.host = "www.newsmth.net";
            set req.backend = smth;
                 }
     else {
            error 404 "Unknown virtual host";
     }
         return(lookup);
 } 

##sub vcl_pipe {
     return(pipe);
 }

#sub vcl_pass {
     return(pass);
 }

#sub vcl_hash {
     hash_data(req.url);
     if(req.http.host) {
         hash_data(req.http.host);
     } else {
         hash_data(server.ip);
     }
     return(hash);
 }

#sub vcl_hit {
     if(req.http.Cache-Control~"no-cache"||req.http.Cache-Control~"max-age=0"||req.http.Pragma~"no-cache"){
                set obj.ttl=0s;
                return (restart);
        }
     return(deliver);
 }

#sub vcl_miss {
     return(fetch);
 }

#sub vcl_fetch {
     if (beresp.ttl <= 120s ||
        beresp.http.Set-Cookie ||
         beresp.http.Vary == "*") {
                /*
                 * Mark as "Hit-For-Pass" for the next 2 minutes
                 */
                set beresp.ttl = 10s;
                return (hit_for_pass);
     }
     return(deliver);
 }

#sub vcl_deliver {
     return(deliver);
 }
#sub vcl_init {
        return(ok);
 }

#sub vcl_fini {
        return(ok);
 }

and my Win7's hosts file add bellow:

192.168.1.178 www.newsmth.net
192.168.1.178 hk.weibo.com

start varnish

varnishd -f /etc/varnish/dd.vcl -s malloc,100M -a 0.0.0.0:8000 -T 0.0.0.0:3500<br>

but when I access the "hk.weibo.com:8000" it fine, and got:

Hello,I am hk.weibo.com!

but when access http://www.newsmth.net:8000/, got:

Hello,I am hk.weibo.com! <br>

My question is why it isn't "Hello,I am www.newsmth.net!"?

varnish fetched the content from the wrong backend. Does anyone know how to fix this?

1 Answer 1

0

My guess is that it hits the following block first, because you have a cookie set in your browser;

if (req.http.Authorization || req.http.Cookie) {
     /* Not cacheable by default */
     return(pass);
}

and thus uses the default backend.

You need to evaluate which backend to use before you return.

4
  • nice,Thanks! You are right! when I change it like this: if (req.http.Authorization) { /* Not cacheable by default */ return(pass); It's fine now !
    – Sola.Shawn
    Nov 29, 2012 at 6:50
  • but i don't understand why set a cookie in browser and it will uses the default backend? why!anyone give me some Proposal?
    – Sola.Shawn
    Nov 29, 2012 at 7:09
  • The Varnish-configuration is like a program. vcl_recv is a function. When you hit return () it's done evaluating recv.. If you haven't done set backend before it hits, it will use the default backend.
    – Kvisle
    Nov 29, 2012 at 8:04
  • My proposal would be to evaluate which backend to use in the beginning of vcl_recv.
    – Kvisle
    Nov 29, 2012 at 8:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .