条件有限,大部分都是虚拟机。
逻辑结构如下:
域名解析到负载均衡服务器上,由nginx处理web访问请求,按照3:1的比例(自己随便定的,也就是大致看看效果)分担到后面的cache服务器上。
用varnish做的cache,为什么用varnish,也就是图个新鲜,另外就是觉得VCL很不错,很好,很强大。但VCL复杂以后varnish的效能会下降多少,没有对比压力测试的情况下那还很难说。如果说VCL不能写的复杂,那varnish的优点也许就没那么明显了,可能就只剩下安装配置简单这个优点了。从个人来说,我觉得varnish的性能不好说有大的跨越,毕竟也看到说有在重负载下崩掉的情况。
软件安装就不说了,没什么太大的花头,无非就是configure+make+make install。
nginx配置如下:
user www www;
worker_processes 10;
error_log logs/error.log;
pid var/nginx.pid;
events {
use epoll;
worker_connections 4096;
}
http {
include conf/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
upstream 2tutu {
server 192.168.0.2:80 weight=3;
server 192.168.0.3:80;
}
server {
listen 80;
server_name www.zzhome.com;
location / {
proxy_pass http://www.zzhome;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_emp_file_write_size 64k;
#root html;
#index index.html index.htm;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
两台缓存上的varnish的配置都一样(其实下面配置里的两个后端是写着顺便玩一下VCL判虚拟主机的,实际本次测试只有一个后端):
backend test1 {
set backend.host = "192.168.0.11";
set backend.port = "http";
}
backend test2 {
set backend.host = "192.168.0.12";
set backend.port = "http";
}
acl purge {
"localhost";
}
sub vcl_recv {
if (req.http.host ~ "www.zzhome.com") {
set req.backend = test2;
}
else
{
if(req.http.host ~ "test1.zzhome.com"){
set req.backend = test1;
}
else{
error 200 "No cahce for this domain";
}
}
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
else{
lookup;
}
}
if (req.request != "GET" && req.request != "HEAD") {
pipe;
}
if (req.http.Expect) {
pipe;
}
if (req.http.Authenticate ){
pass;
}
if (req.http.Cache-Control ~ "no-cache") {
pass;
}
if(req.url ~ "\.asp"){
pass;
}
lookup;
}
sub vcl_pipe {
pipe;
}
sub vcl_pass {
pass;
}
sub vcl_hash {
set req.hash += req.url;
set req.hash += req.http.host;
hash;
}
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
if (!obj.cacheable) {
pass;
}
deliver;
}
sub vcl_miss {
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
fetch;
}
sub vcl_fetch {
if (!obj.valid) {
error;
}
if (!obj.cacheable) {
pass;
}
if (obj.http.Pragma ~ "no-cache" || obj.http.Cache-Control ~ "no-cache" || obj.http.Cache-Control ~ "private") {
pass;
}
if (obj.ttl < 180s) {
set obj.ttl = 180s;
}
insert;
}
sub vcl_deliver {
deliver;
}
sub vcl_timeout {
discard;
}
sub vcl_discard {
discard;
}
在测试过程中,查看两个varnish上的日志,有明显差别,基本上负载3:1成立。
测试过程中,关闭一个varnish,测试负载均衡后端故障的情况,页面访问几乎不受影响,看nginx日志,的确没有连上有“故障”的varnish,能够及时转到正常的那台缓存上。“故障”恢复后,负载3:1继续成立。

