Last Updated: May 15, 2019
·
10.11K
· bartlomiejdanek

Nginx reverse proxy cache for OpenStreetMap

When you want to cache OpenStreetMaps in your Nginx, you need:

  • create cache and tmp directories, run mkdir -p /var/www/cache/tmp
  • nginx proxy for OpenStreetMap tiles

Nginx server configuration:

 server {
  proxy_cache_path  /var/www/cache levels=1:2 keys_zone=openstreetmap-backend-cache:8m max_size=500000m inactive=1000d;
  proxy_temp_path   /var/www/cache/tmp;

  upstream openstreetmap_backend {
    server  a.tile.openstreetmap.org;
    server  b.tile.openstreetmap.org;
    server  c.tile.openstreetmap.org;
  }

  server {
    listen 80;
    server_name tile.example.com;

    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X_FORWARDED_PROTO http;
      proxy_set_header Host $http_host;
      proxy_cache openstreetmap-backend-cache;
      proxy_cache_valid  200 302  365d;
      proxy_cache_valid  404      1m;
      proxy_redirect off;
      if (!-f $request_filename) {
        proxy_pass http://openstreetmap_backend;
        break;
      }
    }
  }
}