Setting Up MapProxy with Docker and Serving Cached Tiles via Nginx
MapProxy is a powerful open-source proxy for geospatial data that allows for efficient caching and serving of map tiles. Combining MapProxy with Docker and Nginx can provide a scalable and easily manageable solution for serving cached map tiles. This guide will walk you through the process of setting up MapProxy using Docker and configuring Nginx to serve cached tiles.
Prerequisites:
- Docker installed on your system
- Basic understanding of Docker
- Basic understanding of Nginx
If you don't meet this prerequisites yet I recommend to have a look at the following guides first:
Step 1: Set Up MapProxy with Docker
Start by creating a Docker Compose file docker-compose.yml
with the following content:
yamlCopy codeservices: mapproxy: image: mxdcodes/mapproxy-docker:latest container_name: mapproxy restart: always volumes: - /data/containers/mapproxy/config:/mapproxy/config - /data/containers/mapproxy/cache_data:/mapproxy/cache_data
Save the file and run:
bashCopy codedocker-compose up -d
This will pull the MapProxy Docker image (https://github.com/dietrichmax/mapproxy-docker) and start the container. MapProxy will be accessible with http://localhost:80
because in the docker image is already Nginx which is used as proxy for MapProxy.
You can store config files in /data/containers/mapproxy/config
which will be available for MapProxy.
If you want to create a example configuration from Mapproxy you can do it with:
bashCopy codedocker exec -ti mapproxy mapproxy-util create -t base-config /mapproxy/config
Afterwards you should see the following if you open localhost:80
Sorry, somehow the image is not available :(
Step 3: Caching the Tiles with MapProxy
So far you have set up Nginx and MapProxy with Docker and MapProxy will cache by default all served tiles with the default configuration.
Here’s the translation of your text:
Generally, there are two ways to use cache:
- Server Side Caching and
- Client Side Caching
For the server side part I strongly recommend to have a look at the documentation at https://mapproxy.github.io/mapproxy/latest/caches.html. There you will find everything about how you can configure cache with MapProxy.
Additionally you can set Cache-Control
and expires
headers with the global paramater expires_hours
.
expires_hours
The number of hours a Tile is valid. TMS clients like web browsers will cache the tile for this time. Clients will try to refresh the tiles after that time. MapProxy supports the ETag and Last-Modified headers and will respond with the appropriate HTTP ‘304 Not modified’ response if the tile was not changed.
Nginx in the docker image will pass these headers to the client so the client will also know how long a tile should be valid. Thats possible with the following Nginx configuration in case you are curious.
confCopy codelocation / { .... # Cache add_header Cache-Control $upstream_http_cache_control; expires $upstream_http_expires; }
E.g. I am serving tiles from MapProxy which visualize my locations where I have ever been to. So I don't want to cache the tiles infinite because they change probably every day. However to reduce server load I decided to keep the tiles valid for one hour with the following global
configuration:
yamlCopy codeglobals: # cache options tiles: expires_hours: 168
If you restart the MapProxy container after setting the configuration and open the URL of your MapProxy service you will find the according headers:
Sorry, somehow the image is not available :(
By following these steps, you've successfully set up MapProxy with Docker and configured Nginx to serve cached map tiles. This scalable solution allows for efficient geospatial data delivery with the added benefit of easy container management. Adjust the configurations based on your specific requirements and integrate this setup into your mapping projects.
If you decide to to take use of MultiMapProxy(scroll down to MultiMapProxy) you can just create more configuration files for MapProxy in /data/containers/mapproxy/config:/mapproxy/config
.
Table of contents
First published January 28, 2024
Have you published a response to this? Send me a webmention by letting me know the URL.
Found no Webmentions yet. Be the first!
About The Author
Geospatial Developer
Hi, I'm Max (he/him). I am a geospatial developer, author and cyclist from Rosenheim, Germany. Support me
Continue Reading
docker-staticmaps: A selfhosted Static Maps API
A containerized web version for staticmaps with express.A Guide to Location Tracking and Visualization with OwnTracks, Node.js, PostgreSQL, GeoServer, MapProxy, Nginx and OpenLayers
Inspired by Aaron Parecki and who he has been tracking his location since 2008 with an iPhone app and a server side tracking API i decided to go for a similar approach. I wanted to track my position constantly with my Android smartphone and use the data to display a map with all locations i have ever been to.Optimizing images for Next.js sites with imgproxy and docker
How to transform and optimize images with imgproxy hosted with docker for your Next.js application.