👷 implementing baseURL to allow usage behind load balancers

This commit is contained in:
Michael Straßburger 2016-12-05 15:38:01 +01:00
parent 6e085af7cc
commit 29b3c5f5dc
6 changed files with 33 additions and 11 deletions

View file

@ -66,6 +66,16 @@ The value of ``root`` is used as prefix for all data types.
You can use this to optionally specify on what domains the rendered tiles are accessible. This can be used for basic load-balancing or to bypass browser's limit for the number of connections per domain. You can use this to optionally specify on what domains the rendered tiles are accessible. This can be used for basic load-balancing or to bypass browser's limit for the number of connections per domain.
``baseURL``
-----------
When you run the tile server server behind a load-balancer, the automatic URL generators wont't have the required information to build URLs which point to the correct LB endpoint.
Also, if your load balancer is forwarding a HTTPS connection to the non-HTTPS ``tileserver-gl``, setting the ``baseURL`` is indispensable.
Not compatible with the ``domains`` option.
``formatQuality`` ``formatQuality``
----------------- -----------------

View file

@ -14,7 +14,8 @@ module.exports = function(options, repo, params, id) {
var mbtilesFile = path.resolve(options.paths.mbtiles, params.mbtiles); var mbtilesFile = path.resolve(options.paths.mbtiles, params.mbtiles);
var tileJSON = { var tileJSON = {
'tiles': params.domains || options.domains 'tiles': params.domains || options.domains,
'baseURL': options.baseURL
}; };
repo[id] = tileJSON; repo[id] = tileJSON;

View file

@ -539,7 +539,7 @@ module.exports = function(options, repo, params, id, dataResolver) {
app.get('/rendered.json', function(req, res, next) { app.get('/rendered.json', function(req, res, next) {
var info = clone(tileJSON); var info = clone(tileJSON);
info.tiles = utils.getTileUrls(req, info.tiles, info.tiles = utils.getTileUrls(req, info.tiles,
'styles/' + id + '/rendered', info.format); 'styles/' + id + '/rendered', info.format, info.baseURL);
return res.send(info); return res.send(info);
}); });

View file

@ -12,6 +12,8 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) {
var styleFile = path.join(options.paths.styles, params.style); var styleFile = path.join(options.paths.styles, params.style);
var baseURL = options.baseURL;
var styleJSON = clone(require(styleFile)); var styleJSON = clone(require(styleFile));
Object.keys(styleJSON.sources).forEach(function(name) { Object.keys(styleJSON.sources).forEach(function(name) {
var source = styleJSON.sources[name]; var source = styleJSON.sources[name];
@ -57,8 +59,9 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) {
if (!opt_nokey && req.query.key) { if (!opt_nokey && req.query.key) {
query = '?key=' + req.query.key; query = '?key=' + req.query.key;
} }
var url = baseURL ? baseURL : req.protocol + '://' + req.headers.host;
return url.replace( return url.replace(
'local://', req.protocol + '://' + req.headers.host + '/') + query; 'local://', url + '/') + query;
}; };
var styleJSON_ = clone(styleJSON); var styleJSON_ = clone(styleJSON);

View file

@ -69,6 +69,8 @@ module.exports = function(opts, callback) {
} }
var options = config.options || {}; var options = config.options || {};
var baseURL = config.baseURL;
var paths = options.paths || {}; var paths = options.paths || {};
options.paths = paths; options.paths = paths;
paths.root = path.resolve( paths.root = path.resolve(
@ -165,7 +167,7 @@ module.exports = function(opts, callback) {
version: styleJSON.version, version: styleJSON.version,
name: styleJSON.name, name: styleJSON.name,
id: id, id: id,
url: req.protocol + '://' + req.headers.host + url: (baseURL ? baseURL : req.protocol + '://' + req.headers.host) +
'/styles/' + id + '.json' + query '/styles/' + id + '.json' + query
}); });
}); });
@ -176,7 +178,7 @@ module.exports = function(opts, callback) {
Object.keys(serving[type]).forEach(function(id) { Object.keys(serving[type]).forEach(function(id) {
var info = clone(serving[type][id]); var info = clone(serving[type][id]);
info.tiles = utils.getTileUrls(req, info.tiles, info.tiles = utils.getTileUrls(req, info.tiles,
type + '/' + id, info.format); type + '/' + id, info.format, baseURL);
arr.push(info); arr.push(info);
}); });
return arr; return arr;
@ -224,6 +226,7 @@ module.exports = function(opts, callback) {
serveTemplate('/$', 'index', function(req) { serveTemplate('/$', 'index', function(req) {
var styles = clone(config.styles || {}); var styles = clone(config.styles || {});
Object.keys(styles).forEach(function(id) { Object.keys(styles).forEach(function(id) {
var style = styles[id]; var style = styles[id];
style.name = (serving.styles[id] || serving.rendered[id] || {}).name; style.name = (serving.styles[id] || serving.rendered[id] || {}).name;
@ -247,9 +250,10 @@ module.exports = function(opts, callback) {
base64url('http://' + req.headers.host + base64url('http://' + req.headers.host +
'/styles/' + id + '/rendered.json' + query) + '/wmts'; '/styles/' + id + '/rendered.json' + query) + '/wmts';
var tiles = utils.getTileUrls( var tiles = utils.getTileUrls(
req, style.serving_rendered.tiles, req, style.serving_rendered.tiles,
'styles/' + id + '/rendered', style.serving_rendered.format); 'styles/' + id + '/rendered', style.serving_rendered.format, baseURL);
style.xyz_link = tiles[0]; style.xyz_link = tiles[0];
} }
}); });
@ -277,7 +281,7 @@ module.exports = function(opts, callback) {
'/data/' + id + '.json' + query) + '/wmts'; '/data/' + id + '.json' + query) + '/wmts';
var tiles = utils.getTileUrls( var tiles = utils.getTileUrls(
req, data_.tiles, 'data/' + id, data_.format); req, data_.tiles, 'data/' + id, data_.format, baseURL);
data_.xyz_link = tiles[0]; data_.xyz_link = tiles[0];
} }
if (data_.filesize) { if (data_.filesize) {

View file

@ -6,20 +6,24 @@ var async = require('async'),
var glyphCompose = require('glyph-pbf-composite'); var glyphCompose = require('glyph-pbf-composite');
module.exports.getTileUrls = function(req, domains, path, format) { module.exports.getTileUrls = function(req, domains, path, format, baseURL) {
var key = req.query.key;
var query = (key && key.length > 0) ? ('?key=' + key) : '';
if (baseURL) {
return [baseURL + path + '/{z}/{x}/{y}.' + format + query]
}
if (domains) { if (domains) {
if (domains.constructor === String && domains.length > 0) { if (domains.constructor === String && domains.length > 0) {
domains = domains.split(','); domains = domains.split(',');
} }
} }
if (!domains || domains.length == 0) { if (!domains || domains.length == 0) {
domains = [req.headers.host]; domains = [req.headers.host];
} }
var key = req.query.key;
var query = (key && key.length > 0) ? ('?key=' + key) : '';
var uris = []; var uris = [];
domains.forEach(function(domain) { domains.forEach(function(domain) {
uris.push(req.protocol + '://' + domain + '/' + path + uris.push(req.protocol + '://' + domain + '/' + path +