# 磁盘缓存配置 # levels=1:2 两级目录结构,避免单目录文件过多 # keys_zone=20m 内存索引 20MB,约可索引 160,000 个瓦片 # max_size=10g 磁盘缓存上限 10GB(约覆盖全国 z0-z14 级别) # inactive=30d 30天未访问的瓦片自动清理 proxy_cache_path /var/cache/nginx/tiles levels=1:2 keys_zone=tiles_cache:20m max_size=10g inactive=30d use_temp_path=off; server { listen 80; # 使用 Docker 内置 DNS 解析上游域名 resolver 127.0.0.11 valid=300s; resolver_timeout 5s; location /tiles/ { # 去掉 /tiles/ 前缀,补全底图样式路径后转发 rewrite ^/tiles/(.*)$ /light_all/$1 break; proxy_pass https://a.basemaps.cartocdn.com; proxy_ssl_server_name on; # 缓存配置 proxy_cache tiles_cache; proxy_cache_key "$uri"; proxy_cache_valid 200 30d; proxy_cache_lock on; # 同一瓦片并发请求只放行一个去上游 proxy_cache_lock_timeout 5s; # 上游故障时继续提供过期缓存,避免地图空白 proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; proxy_cache_background_update on; # 超时设置 proxy_connect_timeout 5s; proxy_read_timeout 15s; # 响应头 add_header Access-Control-Allow-Origin *; add_header X-Cache-Status $upstream_cache_status; expires 30d; } # 健康检查 location /health { return 200 "ok\n"; add_header Content-Type text/plain; } }