Skip to content

Instantly share code, notes, and snippets.

@philipn
Created June 10, 2012 22:29
Show Gist options
  • Save philipn/2907544 to your computer and use it in GitHub Desktop.
Save philipn/2907544 to your computer and use it in GitHub Desktop.
Cache cloudmade titles in localwiki
You'll want to replace 'localwiki.org' with your hostname here.
----------------------
Add to localsettings.py:
OLWIDGET_CUSTOM_LAYER_TYPES = {
'cachedcloudmade': """OpenLayers.Layer.CachedCloudMade('CachedCloudMade',
['//map-a.localwiki.org/tile/ca694687020d468283a545db191bcb81/35165/256/',
'//map-b.localwiki.org/tile/ca694687020d468283a545db191bcb81/35165/256/',
'//map-c.localwiki.org/tile/ca694687020d468283a545db191bcb81/35165/256/'])""",
}
--------------
Add to templates/olwidget/multi_layer_map.html:
diff --git a/sapling/templates/olwidget/multi_layer_map.html b/sapling/templates/olwidget/multi_layer_map.html
index 5631fc6..aa3746c 100644
--- a/sapling/templates/olwidget/multi_layer_map.html
+++ b/sapling/templates/olwidget/multi_layer_map.html
@@ -3,6 +3,15 @@
<div id="{{ id }}"></div>
{{ layer_html|join:"" }}
+{% comment %}
+ We need to explicitly include this JS file because we're using a custom
+ base layer *and* we are using a custom class we defined, CachedCloudMade.
+
+ We can remove this line once we switch to a real TMS/XYZ server -- which
+ will probably happen when we create our own custom tiles from OSM data.
+{% endcomment %}
+<script type="text/javascript" src="{% static "olwidget/js/sapling_cloudmade.js" %}"></script>
+
<script type="text/javascript">
SaplingMap.init_openlayers();
OpenLayers.ImgPath = '{% static "img/openlayers/themes/dark/" %}';
--------------------
Then we have an apache host set up for 'map.localwiki.org'. The apache config proxies request to a varnish instance. We have to proxy because varnish doesn't do SSL. If you don't use ssl you can just point to varnish directly and have it listen on a public port. If your site is running with SSL, though, you'll want the tiles to be SSL or you may see a warning:
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/localwiki.org/localwiki.org.crt
SSLCertificateKeyFile /etc/apache2/ssl/localwiki.org/localwiki.org.key
SSLCACertificateFile /etc/apache2/ssl/localwiki.org/intermediate.crt
Header set Strict-Transport-Security "max-age=1800"
ServerName map.localwiki.org
ServerAlias map-a.localwiki.org map.localwiki.org
ServerAlias map-b.localwiki.org map.localwiki.org
ServerAlias map-c.localwiki.org map.localwiki.org
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options -Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyRequests off
#####################################
# Route /tile/ to cloudmade cache.
#####################################
# :6081 is our varnish instance that's caching cloudmade tiles
<Location "/tile/">
SetEnv proxy-nokeepalive 1
SetEnv force-proxy-request-1.0
ProxyPass http://127.0.0.1:6081/
ProxyPassReverse http://127.0.0.1:6081/
</Location>
</VirtualHost>
-----------------
And here's our /etc/varnish/default.vcl:
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
backend default {
#.host = "b.tile.cloudmade.com";
.host = "205.251.203.16";
.port = "80";
}
#
# Below is a commented-out copy of the default VCL logic. If you
# redefine any of these subroutines, the built-in logic will be
# appended to your code.
#
sub vcl_recv {
# Hardcode Host header to point to cloudmade tile server.
set req.http.host = "b.tile.cloudmade.com";
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For ", " client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
return (lookup);
}
#
if (!beresp.cacheable) {
return (pass);
}
/* Remove Expires from backend, it's not long enough */
unset beresp.http.expires;
/* Set the clients TTL on this object */
set beresp.http.cache-control = "max-age=900";
/* Set how long Varnish will keep it */
set beresp.ttl = 20w;
/* marker for vcl_deliver to reset Age: */
set beresp.http.magicmarker = "1";
return (deliver);
}
sub vcl_deliver {
if (resp.http.magicmarker) {
/* Remove the magic marker */
unset resp.http.magicmarker;
/* By definition we have a fresh object */
set resp.http.age = "0";
}
return (deliver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment