一、简介及工作流程图
Varnish 是一款高性能且开源的反向代理服务器和 HTTP 加速器,其采用全新的软件体系机构,和现在的硬件体系紧密配合,与传统的 squid 相比,varnish 具有性能更高、速度更快、管理更加方便等诸多优点,
varnish主要运行两个进程:Management进程和Child进程(也叫Cache进程)。
详细内容请参看官网
官方提供工作流程图: https://www.varnish-cache.org/
二、实验拓扑图
三、安装配置
[root@90sec src]# yum -y install varnish-docs-3.0.5-1.el6.x86_64.rpm varnish-3.0.5-1.el6.x86_64.rpm varnish-libs-3.0.5-1.el6.x86_64.rpm
1、查看生成了那些配置文件
[root@90sec src]# rpm -ql varnish
[root@90sec /]# vim /etc/sysconfig/varnish
VARNISH_LISTEN_PORT=80 #默认监听为6081
VARNISH_ADMIN_LISTEN_ADDRESS=127.0.0.1 #管理的IP地址
VARNISH_ADMIN_LISTEN_PORT=6082 #管理端口
VARNISH_MIN_THREADS=50 #最小线程数量
VARNISH_MAX_THREADS=1000 #最大线程数量
VARNISH_THREAD_TIMEOUT=120 #线程超时时间
VARNISH_STORAGE_SIZE=1G #设置文件缓存大小变量
VARNISH_MEMORY_SIZE=80M #设置内存缓存大小变量
VARNISH_STORAGE="malloc,${VARNISH_MEMORY_SIZE}" #设置缓存位置为内存
2、启动Varnish
[root@90sec /]# service varnish start
Starting Varnish Cache: [ OK ]
[root@90sec /]# ss -anltp | grep varnish
LISTEN 0 128 :::80 :::* users:(("varnishd",2500,8))
LISTEN 0 128 *:80 *:* users:(("varnishd",2500,7))
LISTEN 0 10 127.0.0.1:6082 *:* users:(("varnishd",2499,6))
3、Varnishadm使用
[root@90sec /]# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082
200
-----------------------------
Varnish Cache CLI 1.0
-----------------------------
Linux,2.6.32-431.el6.x86_64,x86_64,-smalloc,-smalloc,-hcritbit
varnish-3.0.5 revision 1a89b1f
Type 'help' for command list.
Type 'quit' to close CLI session.
varnish> help
200
help [command]
ping [timestamp]
auth response
quit #退出
banner
status #查看状态
start #开启
stop #关闭
vcl.load <configname> <filename> #加载一个VCL文件
vcl.inline <configname> <quoted_VCLstring>
vcl.use <configname> #使用VCL文件
vcl.discard <configname>
vcl.list #查看VCL文件列表
vcl.show <configname> #查看VCL文件配置
param.show [-l] [<param>]
param.set <param> <value>
panic.show
panic.clear
storage.list
backend.list
backend.set_health matcher state
ban.url <regexp>
ban <field> <operator> <arg> [&& <field> <oper> <arg>]...
ban.list
4、修改主配置文件
[root@90sec ~]# vim /etc/varnish/default.vcl
import directors;
probe backend_healthcheck {
.url = "/health.html";
.window = 8;
.threshold = 2;
.interval = 3s;
}
backend web1 {
.host = "172.16.36.130";
.port = "80";
.probe = backend_healthcheck;
}
backend web2 {
.host = "172.16.36.131";
.port = "80";
.probe = backend_healthcheck;
}
backend APP1 {
.host = "172.16.36.130";
.port = "8080";
.probe = backend_healthcheck;
}
backend app2 {
.host = "172.16.36.131";
.port = "8080";
.probe = backend_healthcheck;
}
sub vcl_init {
new web_cluster = directors.random();
web_cluster.add_backend(web1,1.0);
web_cluster.add_backend(web2,1.0);
new img_cluster = directors.random();
app_cluster.add_backend(app1,1.0);
app_cluster.add_backend(app2,1.0);
}
acl purgers {
"127.0.0.1";
"172.16.0.0"/16:
}
sub vcl_recv {
if (req.method == "GET" && req.http.cookie) {
return(hash);
}
if (req.url ~ "test.html") {
return(pass);
}
if (req.method == "PURGE") {
if (!client.ip ~ purgers) {
return(synth(405,"Method not allowed"));
}
return(hash);
}
if (req.http.X-Forward-For) {
set req.http.X-Forward-For = req.http.X-Forward-For + "," + client.ip;
} else {
set req.http.X-Forward-For = client.ip;
}
if (req.http.host ~ "(?i)^(www.)?lnmmp.com$") {
set req.http.host = "www.lnmmp.com";
set req.backend_hint = web_cluster.backend();
} elsif (req.http.host ~ "(?i)^images.lnmmp.com$") {
set req.backend_hint = img_cluster.backend();
}
return(hash);
}
sub vcl_hit {
if (req.method == "PURGE") {
purge;
return(synth(200,"Purged"));
}
}
sub vcl_miss {
if (req.method == "PURGE") {
purge;
return(synth(404,"Not in cache"));
}
}
sub vcl_pass {
if (req.method == "PURGE") {
return(synth(502,"PURGE on a passed object"));
}
}
sub vcl_backend_response {
if (bereq.url ~ "\.(jpg|jpeg|gif|png)$") {
set beresp.ttl = 7200s;
}
if (bereq.url ~ "\.(html|css|js)$") {
set beresp.ttl = 1200s;
}
if (beresp.http.Set-Cookie) {
return(deliver);
}
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT from " + server.ip;
} else {
set resp.http.X-Cache = "MISS";
}
}
后面继续更新