varnish 访问问题

南山小樵 发布于 2013/08/19 16:08
阅读 334
收藏 1

首先我介绍一下我的环境,实验环境,一共两台服务器

Varnish缓存服务器  192.168.1.2

Web服务器            192.168.1.5

web服务器上配置了两个虚拟主机,www.xxx.cn 和 ent.xxx.cn , 在hosts 里以配置解析

两个虚拟主机分别通过nginx的proxy_pass 将动态部分转到后端的一个tomcat上(tomcat上有两个工程A和B,www.xxx.cn对应工程A;ent.xxx.cn对应工程B)

目前的问题是这样的

1. 完整配置,如下边贴出来的配置,访问www.xxx.cn没有问题,使用正常;访问ent.xxx.cn 时候,查看varnish 日志,对应的是服务器ent.xxx.cn ,但是点击登录的时候,后端的服务器是www.xxx.cn ,不解?

2. 完成配置,如下边贴出来的配置,访问ent.xxx.cn 时候,使用浏览器的F12查看,有异常,如下。


3. 如果注释掉,关于www的配置,访问ent.xxx.cn ,登录正常。

以下为配置文件:

#定义后端的web服务器及端口
#本来host是填写ip地址的,由于我的服务器后端是虚拟主机,所以在这里配置域名,在hosts里配置解析即可
backend webserver1 {
     .host = "www.xxx.cn";
     .port = "80";
 }
backend webserver2 {
     .host = "ent.xxx.cn";
     .port = "80";
 }

#使用random必须制定权重,roun-bin不能制定权重
#director webserver random {  
# {.backend = webserver1; .weight = 5;  }  
# {.backend = webserver2; .weight = 8;  }  
#}  
#允许以下主机通过purge来清除缓存
 acl purge {
"localhost";
"127.0.0.1";
"192.168.1.0"/24;
}
 sub vcl_recv {
 if (req.request == "PURGE") {  
     if (!client.ip ~ purge) {  
     error 405 "Not allowed.";  
   }  
     else {  
     return (lookup);  
   }  
 }  
#req.restarts请求重启的次数,默认最大值为4
     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;
 	}
     }
#修正客户端的Accept-Encoding头信息,默认选用gzip方式
#防止个别浏览器发送类似 deflate, gzip
#当然后端如果只支持gzip的话.可以只设置gizp
if (req.http.Accept-Encoding) {
	 if (req.http.Accept-Encoding ~ "gzip") {
		 set req.http.Accept-Encoding = "gzip";
}
elsif (req.http.Accept-Encoding ~ "deflate") {
	 set req.http.Accept-Encoding = "deflate";
}
else {
	 remove req.http.Accept-Encoding;
}
}
#如果请求的类型不是GET、HEAD、PUT、POST、TRACE、OPTIONS或DELETE时,则进入 pipe模式。注意这里是“&&”关系
     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);
     }
#如果请求的类型不是GET、HEAD则进入 pipe模式,结合上边,也就说PUT、POST、TRACE、OPTIONS会进入pass模式#
     if (req.request != "GET" && req.request != "HEAD") {
         /* We only deal with GET and HEAD by default */
         return (pass);
     }
	if (req.http.Authorization || req.http.Authenticate)
	{
	  return (pass);
	}

#	if (req.http.host ~ "(www.)?xxx.cn"){
#		set req.http.host = "www.7cbox.cn";
#}
#	if (req.http.host ~ "ent.xxx.cn"){
#		set req.http.host = "ent.7cbox.cn";
#}
	 if (req.url ~ "^/nds/") {
                 return (pass);
     }
	if (req.http.host  ~ "(www.)?xxx.cn") {
	set req.backend = webserver1;
        }
	if (req.http.host  ~ "ent.xxx.cn") {
	set req.backend = webserver2 ;
        }
	if (req.http.Accept-Encoding) {
          if (req.url ~ "\.(jpg|png|gif|jpeg|flv|ico|css|js)" ) {
               remove req.http.Accept-Encoding;
               unset req.http.Cookie;
          } else if (req.http.Accept-Encoding ~ "gzip") {
               set req.http.Accept-Encoding = "gzip";
         } else if (req.http.Accept-Encoding ~ "deflate") {
              set req.http.Accept-Encoding = "deflate";
         } else if (req.http.Accept-Encoding ~ "gzip,deflate") {
              set req.http.Accept-Encoding = "gzip";
	 }
	 else {
             remove req.http.Accept-Encoding;
            }
        }
      //Only cache 4 type browsers as test
#如果使用工具测试的话,需要将一下几行注释掉
#        if(req.http.User-Agent ~ "MSIE|Firefox|SAFARI|Chrome")
#        {
#          return (lookup);
#        }
#       return (pass);
}
 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_miss {
	return (fetch);
	}
 sub vcl_fetch {
	if (req.request == "GET" && req.url ~ "^/cimage(.*)") {  
            unset beresp.http.set-cookie;
       		set beresp.ttl = 300s;  
                set beresp.http.expires=beresp.ttl;
		set beresp.http.cache-control="max-age=600"; // 1 Minutes age

   }  
	if (req.request == "GET" && req.url ~ "\.(png|xsl|xml|pdf|ppt|doc|docx|chm|rar|zip|bmp|jpeg|swf|ico|mp3|mp4|rmvb|ogg|mov|avi|wmv|swf|txt|png|gif|jpg|css|js|html|htm)") {  
        	     set beresp.ttl = 600s;  
                     set beresp.http.expires=beresp.ttl;
                     set beresp.http.cache-control="max-age=600"; // 10 Minutes age
   
   }  
	return (deliver);
 }
 
 sub vcl_deliver {
	 if (obj.hits > 0) {
                set resp.http.X-Cache = "HIT";
        } else {
                set resp.http.X-Cache = "MISS";
        }
	set resp.http.X-Cache-Hits=obj.hits;
	  return (deliver);
 }


求大神指点迷津~

加载中
返回顶部
顶部