Varnish实现负载均衡
Varnish作为反向代理,位于Web服务器前面,可作为Web前端缓存
最简单的一种架构,如下图(略去防火墙、路由器等网络设备),即所有网站请求都经过Varnish缓存后,指向后端服务器

(图一)
实际上,一般网站会有多个域名(包括子域名),对应后台不同的服务器,比如,
http://www.xxx.com 官网产品及信息展示
https://passport.xxx.com 会员身份验证
http://image.xxx.com 图片静态资源
每个域名对应的后端服务器可能是多台,可将合并为一组
系统架构如下图二(略去防火墙、路由器等网络设备)

客户端请求到了Varnish服务器端
1、可以根据不同客户端请求的域名,来选择不同的路径,如有的是需要缓存,有的不需要缓存
2、每个域名对应着后端服务器组
3、每个组中的服务器,可以按随机方式选择
4、设置每个组中单个服务器的权重值(.weight),权重越高被选中概率就高
Varnish主要作为反向代理,可不是做路由的,虽然Varnish具有很好的性能,路由功能不建议在 Varnish中处理
单一域名 配置多台后端服务器,这样较为合理
下面是代码示例
backend passport { .host = "172.1.1.8"; .port = "80"; } backend cacheserver1 { .host = "172.1.1.1"; .port = "80"; } backend cacheserver2 { .host ="172.1.1.2"; .port = "80"; } backend imagecacheserver1 { .host = "172.1.1.3"; .port = "80"; } backend imagecacheserver2 { .host ="172.1.1.4"; .port = "80"; } //define director (Load Balancer) director wwwserver round-robin { { .backend = cacheserver1; .weight = 5; } { .backend = cacheserver2; .weight = 7; } } director imageserver round-robin { { .backend = imagecacheserver1; .weight = 5; } { .backend = imagecacheserver2; .weight = 3; } } ... sub vcl_recv { if (req.http.host ~ "(?i)^passport.xxx.com$") { set req.backend= passport; } else if (req.http.host ~ "(?i)^(www.)?xxx.com$") { set req.http.host = "www.xxx.com"; set req.backend = wwwserver; } elsif (req.http.host ~ "(?i)^image.xxxx.com$") { set req.backend = imageserver; } else { error 404 "Unknown virtual host"; } return (pass); } ...

浙公网安备 33010602011771号