centos6.3 varnish3.03 get the wrong backend

Posted by Sola.Shawn on Server Fault See other posts from Server Fault or by Sola.Shawn
Published on 2012-11-29T05:01:12Z Indexed on 2012/11/29 11:09 UTC
Read the original article Hit count: 301

Filed under:

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?

© Server Fault or respective owner

Related posts about varnish