2. Varnish
Varnish is an HTTP accelerator. (tutorial)
Install it in front of your web application and it will speed it
up significantly.
Varnish stores data in virtual memory and leaves the task
of deciding what is stored in memory and what gets paged
out to disk to the operating system.
6. VCL
director # group several backend into a group
director node_director round-robin {
{
.backend = default;
}
{
.backend = node_server_1;
}
{
.backend = node_server_2;
}
}
7. VCL
vcl_recv is called at the beginning of a request
● decide whether or not to serve the request
● how to serve
● which backend to use
sub vcl_recv {
if (req.request != "GET" && req.request != "HEAD") { # only deal with GET and HEAD
return (pass);
# pass to backend, not cache
}
}
vcl_fetch is called after a document has been retrieved from the backend
sub vcl_fetch {
set beresp.ttl = 15m; # cache time
return (deliver); # deliver the cached object to the client
}
8. VCL -- Grace mode
Grace mode solves two problems:
● it serves stale content to avoid request pile-up.
● it serves stale content if the backend is not healthy.
sub vcl_fetch {
set beresp.grace = 6h; # 设置保存旧数据6小时
}
sub vcl_recv {
if (req.backend.healthy) {
set req.grace = 15m; # 允许读取15分钟前的数据
} else {
set req.grace = 5h;
}
}
9. VCL -- Saint mode
Saint mode
enables you to discard a certain page from one backend server and either try another server or serve
stale content from cache.
sub vcl_fetch {
if (beresp.status >= 500) {
set beresp.saintmode = 3m; # Varnish will not ask that server for URL for 3 mins
return(restart);
}
}
10. limitations on grace- and saint mode
How to fixed?
1. Declare a backend that is always sick
backend fake { # Fake back-end which is always
sick.
.host = "192.168.200.1";
.port = "9999";
}
1. Set a magic marker in vcl_error && Restart the transaction
sub vcl_error {
if ( obj.status == 503 && req.restarts < 5 ) {
set obj.http.X-Restarts = req.restarts;
if ( req.restarts == 0 ) { # if restarts is 0, set a
magic marker
set req.http.magicmarker = "fake";
}
return(restart); # restart the transaction
}
VCL -- hack
11. limitations on grace- and saint mode
How to fixed?
3. Note the magic marker in vcl_recv and set the backend to the sick one
sub vcl_recv {
if ( req.http.magicmarker && req.http.magicmarker == "fake" ) {
unset req.http.magicmarker;
set req.backend = fake; # tell Varnish to use the sick backend, Varnish will serve stale
data
} else {
set req.backend = node_director;
}
}
3. If the object exists in cache (graced) - it will be used. Otherwise, you will hit vcl_error again. (Thus the
check of req.restarts in step 2).
VCL -- hack
12. VCL -- Purges
Purge: remove the cache object
$.ajax({type:'PURGE', url:'http://www.example.com/abc',
success:function(data){console.log(data);}});
acl purge {
"localhost";
"192.168.10.0"/24;
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return (lookup);
}
}
sub vcl_hit {
if (req.request == "PURGE") {
purge;
error 200 "Purged.";
}
return (deliver);
}
sub vcl_miss {
if (req.request == "PURGE") {
purge;
error 200 "Not Cached.
Purged.";
}
return (fetch);
}