Varnish setup
Jump to navigation
Jump to search
Varnish setup
This is a quick guide to cache tiles using varnish.
Newest update to the vcl scripts can be found here https://github.com/klausdk/varnish/tree/master/openstreetmap
DNS setup
You will need a ip dedicated to varnish to run on port 80
Create 3 dns entries :
a.tile.host.tld -> Your ip / host b.tile.host.tld -> Your ip / host c.tile.host.tld -> Your ip / host
Install varnish
Assuming redhat/centos :
rpm --nosignature -i http://repo.varnish-cache.org/redhat/varnish-3.0/el5/noarch/varnish-release-3.0-1.noarch.rpm yum install varnish
Checkout https://www.varnish-cache.org/releases for other ways to install
vcl script for varnish 3.0 (file.vcl)
This script is designed only to cache tiles for openstreetmap.
backend default {
.host = "127.0.0.1";
.port = "80";
}
backend a_tile {
.host = "a.tile.openstreetmap.org";
.port = "80";
}
backend b_tile {
.host = "b.tile.openstreetmap.org";
.port = "80";
}
backend c_tile {
.host = "c.tile.openstreetmap.org";
.port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^a.tile") {
set req.backend = a_tile;
} else if (req.http.host ~ "^b.tile") {
set req.backend = b_tile;
} else if (req.http.host ~ "^c.tile") {
set req.backend = c_tile;
}
remove req.http.cookie;
// Cache everything
if (req.request == "GET") {
return (lookup);
}
}
sub vcl_fetch {
// Cache tiles for 3 weeks
set beresp.ttl = 3w;
// Remove all cookies
remove beresp.http.set-cookie;
remove beresp.http.cookie;
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache_v = "HIT";
} else {
set resp.http.X-Cache_v = "MISS";
}
}
sub vcl_hash {
// Cache using only url as a hash.
// This means if a.tile/1/1/1/tile.png is accessed, b.tile/1/1/1/tile.png will also be fetch from cache
hash_data(req.url);
return (hash);
}
vcl script for varnish 4.0 (file.vcl)
vcl 4.0;
backend default {
.host = "127.0.0.1";
.port = "80";
}
backend a_tile {
.host = "a.tile.openstreetmap.org";
.port = "80";
}
backend b_tile {
.host = "b.tile.openstreetmap.org";
.port = "80";
}
backend c_tile {
.host = "c.tile.openstreetmap.org";
.port = "80";
}
sub vcl_recv {
if (req.http.host ~ "^a.tile") {
set req.backend_hint = a_tile;
} else if (req.http.host ~ "^b.tile") {
set req.backend_hint = b_tile;
} else if (req.http.host ~ "^c.tile") {
set req.backend_hint = c_tile;
} else if (req.http.host ~ "^a.map") {
set req.backend_hint = a_tile;
} else if (req.http.host ~ "^b.map") {
set req.backend_hint = b_tile;
} else if (req.http.host ~ "^c.map") {
set req.backend_hint = c_tile;
}
unset req.http.cookie;
// Cache everything
if (req.method == "GET") {
return (hash);
}
}
sub vcl_backend_response {
// Cache tiles for 3 weeks
set beresp.ttl = 3w;
// Remove all cookies
unset beresp.http.set-cookie;
unset beresp.http.cookie;
}
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache_v = "HIT";
} else {
set resp.http.X-Cache_v = "MISS";
}
}
sub vcl_hash {
// Cache using only url as a hash.
// This means if a.tile/1/1/1/tile.png is accessed, b.tile/1/1/1/tile.png will also be fetch from cache
hash_data(req.url);
return (lookup);
}
Start up
varnishd -a yourip:80 -f /etc/varnish/file.vcl