Varnish
Cache Service directly give response to client instead of web service
Cache Service is faster
1. Save in memory
2. Save as key-value format, Use O1 search method
3. Save in files
Once you set cache service in system, it must be stable. Otherwise the system will be destroied.
Hit ratio:
1. File hit ratio
2. Byte hit ratio
cache type
A. malloc: memory
B. file: files, restart varnish clean
C. persistent: not recommend
Control cache:
1. no-store: do not cache
2. no-cache: can cache, must verify before response
Varnish: open source; http reserver proxy; load balance
Threads
1. management:
A. read config file;
B. choose suitable storage: memory; file; persistent memory
C. create / read cache files;
D. fork monitor child process
2. child/cache:
A. worker: process user request;
B. accept: receive user request;
Varnish status engines:
A. vcl_recv: check whether the requested resource is cacheable or not
B. vcl_hash: check whether the resource is cached or not
C. vcl_hit: get the resource from cache
D. vcl_miss: can not find resource from cache
E. vcl_fetch: get the resource from backend servers
F. vcl_deliver: send response to client
G. vcl_pass: process the requested resource is not cacheable
H: vcl_error: process the error request
I: vcl_pipe: process the unrecognized requested, create pipe between client and backend servers.
Installation
version 3.x
1. Install package:
yum install varnish-3.0,6...
yum install varnish-libs-3.0.6...
yum install varnish-docs-3.0.6...
2. Tools
check status: /usr/bin/varnishstat
check logic: /usr/bin/varnishtest
3. Change config
vim /etc/sysconfig/varnish
A. Change the listen port
from VARNISH_LISTEN_PORT=6081
to VARNISH_LISTEN_PORT=80
B. by default, cache method is file
if you want to change to memory change add following context and add # for VARNISH_STORAGE="file..."
VARNISH_MALLOC_SIZE=1G
VARNISH_STORAGE="malloc, ${VARNISH_MALLOC_SIZE}"
4. Another config file (backup before change it)
vim /etc/varnish/default.vcl
A. change backend server information
from
backend default {
.host = "127.0.0.1";
.port = "80";
}
to
backend default {
.host = "12.11.0.102";
.port = "80";
}
B. By default, all the requested resource will be cached.
i. In order to find out the cache feature, Add following context into default.vcl
sub vcl_deliver {
if (obj.hits >0){
set resp.http.X-Cache = "HIT Via" + " " +server.hostname;
} else {
set resp.http.X-Cache = "MISS Via" + " " +server.hostname;
}
return (deliver);
}
ii. remove # for vcl_recv to identify the static and dynamic request
5. start the varnish service: service varnish restart
Define & Use server group in default.vcl
backend mobile1 {
.host = "192.168.199.187";
.port = "80";
}
backend mobile2 {
.host = "192.168.199.188";
.port = "80";
}
director mobile random {
{ .backend =mobile1
.weight = 1
}
{ .backend = mobile2
.weight = 1
}
}
backend desktop1 {
.host = "192.168.199.187";
.port = "80";
}
backend desktop2 {
.host = "192.168.199.188";
.port = "80";
}
director desktop random {
{ .backend = desktop1
.weight = 1
}
{ .backend = desktop2
.weight = 1
}
}
sub vcl_recv {
if (req.http.User-Agent ~ "iPad" || req.http.User-Agent ~ "iPhone" || req.http.User-Agent ~ "Android") {
set req.backend = mobile;
} else {
set req.backend = desktop;
}
Comments
Post a Comment