标签:varnish
varnish是一款高性能的开源HTTP加速器.
varnish与一般服务器软件类似,分为master(management)进程和child(worker,主要做cache的工作)进程。master进程读入命令,进行一些初始化,然后fork并监控child进程。child进程分配若干线程进行工作,主要包括一些管理线程和很多woker线程。
针对文件缓存部分,master读入存储配置(-s file[,path[,size[,granularity]]] ),调用合适的存储类型,然后创建/读入相应大小的缓存大文件。接着,master初始化管理该存储空间的结构体。这些变量都是全局变量,在fork以后会被child进程所继承(包括文件描述符)。
在child进程主线程初始化过程中,将前面打开的存储大文件整个mmap到内存中(如果超出系统的虚拟内存,mmap失败,进程会减少原来的配置mmap大小,然后继续mmap),此时创建并初始化空闲存储结构体,挂到存储管理结构体,以待分配。
接着,真正的工作开始,Varnish的某个负责接受新HTTP连接的线 程开始等待用户,如果有新的HTTP连接过来,它总负责接收,然后叫醒某个等待中的线程,并把具体的处理过程交给它。Worker线程读入HTTP请求的 URI,查找已有的object,如果命中则直接返回并回复用户。如果没有命中,则需要将所请求的内容,从后端服务器中取过来,存到缓存中,然后再回复。
1.server3:
yum install varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm -y
vim /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80
vim /etc/varnish/default.vcl
backend default { .host = "127.0.0.1"; .port = "8080"; }
vim /etc/httpd/conf/httpd.conf
Listen 8080
/etc/init.d/httpd start
/etc/init.d/varnish start
netstat -antlp
cd /var/www/html
vim index.html
server3.example.com server3.example.com
curl -I 172.25.85.3/index.html
在网页中打开:172.25.85.3
之后修改index.html:server2.server2.server2
刷新网页上的内容,等一段时间修改后的内容才显示 ##会有缓存
在index.html中的内容修改之后,网页上的内容未被刷新之前:
curl -I 172.25.85.3/index.html
修改成功之后Age=0
curl -dump 172.25.85.3
curl -dump 172.25.85.3/index.html
2. server3:
cd /etc/varnish
vim default.vcl
backend default { .host = "127.0.0.1"; .port = "8080"; }
sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT from westos cache"; } else { set resp.http.X-Cache = "MISS from westos cache"; } return (deliver); }
/etc/init.d/varnish restart
curl -I 172.25.85.3/index.html
修改:
cd /var/www/html
echo server2.server2 > index.html
curl -I 172.25.85.3/index.html
curl 172.25.85.3/index.html
### 通过 varnishadm 手动清除缓存
varnishadm ban.url .*$ #清除所有
varnishadm ban.url /index.html #清除 index.html 页面缓存
清除了缓存中的内容就可以很快更新index.html中修改的内容
3.定义多个不同域名站点的后端服务器:
在真机上加上server3的域名解析:
172.25.85.3 server3.example.com www.linux.org www.unix.org www.westos.org westos.org bbs.westos.org
server3:
vim /etc/varnish/default.vcl
backend default { .host = "127.0.0.1"; .port = "8080"; } backend server4 { .host = "172.25.85.4"; .port = "80"; } sub vcl_recv { if (req.http.host ~ "^(www.)?westos.org") { set req.http.host = "www.westos.org"; set req.backend = default; } elsif (req.http.host ~ "^bbs.westos.org") { set req.backend = server4; } else {error 404 "westos cache"; } } sub vcl_deliver { if (obj.hits > 0) { set resp.http.X-Cache = "HIT from westos cache"; } else { set resp.http.X-Cache = "MISS from westos cache"; } return (deliver); }
/etc/init.d/varnish reload
server3-www.westos.org > /var/www/html/index.html
server4:
server4-bbs.westos.org > /var/www/html/index.html
检测:
http://www.westos.org/
http://bbs.westos.org/
4. 把多个后端聚合为一个组,并检测后端健康状况
server3:
vim /etc/varnish/default.vcl
增加:
backend default { .host = "127.0.0.1"; .port = "8080"; } backend server4 { .host = "172.25.85.4"; .port = "80"; } director westoslb round-robin { { .backend = default; } { .backend = server4; } } sub vcl_recv { if (req.http.host ~ "^(www.)?westos.org") { set req.http.host = "www.westos.org"; set req.backend = westoslb; } elsif (req.http.host ~ "^bbs.westos.org") { set req.backend = server4; } else {error 404 "westos cache"; } }
server4:
vim /var/www/html/index.html
<VirtualHost *:80> DocumentRoot /var/www/html ServerName www.westos.org alias westos.org </VirtualHost> <VirtualHost *:80> DocumentRoot /bbs ServerName bbs.westos.org </VirtualHost>
/etc/init.d/httpd start
mkdir /bbs
vim /bbs/index.html
bbs.westos.org
vim /var/www/html/index.html
server4-www.westos.org
server3:
/etc/init.d/varnish reload
vim /etc/varnish/default.vcl
sub vcl_recv { if (req.http.host ~ "^(www.)?westos.org") { set req.http.host = "www.westos.org"; set req.backend = westoslb; return (pass); } elsif (req.http.host ~ "^bbs.westos.org") { set req.backend = server4; } else {error 404 "westos cache"; } }
/etc/init.d/varnish reload
http://www.westos.org/
server3-www.westos.org he server4-www.westos.org 交替出现
关闭server3上的 httpd ,只出现server4-www.westos.org
5.banzip:
server3:
yum install unzip
unzip bansys.zip -d /var/www/html/
cd /var/www/html/bansys/
yum install php -y
mv * ..
cd ..
rm -fr bansys/
vim /var/www/html/config.php
注释一部分
//可定义多个主机列表 $var_group1 = array( ‘host‘ => array(‘172.25.85.3‘), ‘port‘ => ‘6082‘, ); //这个使用了mysql读取数据 # $var_group3 = array( # ‘host‘ => $varnish_host, # ‘port‘ => ‘6082‘, # ); //varnish群组定义 //对主机列表进行绑定 $VAR_CLUSTER = array( ‘www.westos.org‘ => $var_group3, ); //varnish版本 //2.x和3.x推送命令不一样 $VAR_VERSION = "3"; ?> ~
/etc/init.d/httpd restart
http://172.25.85.3:8080/
vim /etc/varnish/default.vcl
acl westos { "127.0.0.1"; "172.25.85.0"/24; } backend default { .host = "127.0.0.1"; .port = "8080"; } director westoslb round-robin { { .backend = default; } { .backend = server4; } } sub vcl_recv { if (req.request == "BAN") { if (!client.ip ~ westos) { error 405 "Not allowed."; } ban("req.url ~ " + req.url); error 200 "ban added"; } if (req.http.host ~ "^(www.)?westos.org") { set req.http.host = "www.westos.org"; set req.backend = westoslb; #return (pass); } elsif (req.http.host ~ "^bbs.westos.org") { set req.backend = server4; } else {error 404 "westos cache"; } }
/etc/init.d/varnish reload
http://www.westos.org/index.html
只出现server3-www.westos.org
http://172.25.85.3:8080/
标签:varnish
原文地址:http://11713145.blog.51cto.com/11703145/1834539