I'm running a map tile server behind an nginx proxy. The tile calculation is expensive and that's why I cache the response for 24 hours. I've configured nginx to serve a stale response even when the cache is expired and at the same time updating the cache in the background.
My config is this:
location ~* ^/tiles/(streets|satellite-overlay)/(.*)$ {
proxy_pass http://127.0.0.1:5000/styles/$1/$2;
# configure server-side cache
proxy_cache tiles;
proxy_cache_valid 200 1d;
proxy_buffering on;
# whilst tileserver recalculates the new tile, serve the old tile instead
proxy_cache_background_update on;
# only one request per tile
proxy_cache_lock on;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
add_header X-Cached $upstream_cache_status;
# cache client-side for 24 hours
add_header "Cache-Control" "public, max-age=86400";
}
So a request to /tiles/streets/1/2/3.png
is being correctly proxied to http://127.0.0.1:5000/styles/streets/1/2/3.png
.
However, after the 24 cache period, when the cache is supposed to be updated in the background, the following URL is requested from the tile server: http://127.0.0.1:5000/styles/streets//
. This leads to a 404 and the cache is never updated.
It appears that the contents of the $1
and $2
variables is being lost somewhere.
Can someone spot a configuration error? Is it possible to use these variables in proxy_pass
in conjuction with proxy_cache_background_update
?
This looks like an nginx bug to me. However, as a possible workaround, you can try using named captures:
I remember some occasions when I needed to use named captures because positional captures did not work properly.
However, I haven't faced a similar issue, so I'm not sure if this helps in this case.