地图类服务nginx 代理

0 阅读1分钟

引言

地图类服务通常有token 之类的关键信息 我们需要通过nginx 代理在浏览器中隐藏这些信息

nginx 配置

浏览器对同一域名的并发请求数有限制(通常6个),用多个子域名可以并行加载更多瓦片

在 server 层加入

upstream tianditu {
  server t0.tianditu.gov.cn:443;
  server t1.tianditu.gov.cn:443;
  server t2.tianditu.gov.cn:443;
  server t3.tianditu.gov.cn:443;
  server t4.tianditu.gov.cn:443;
  server t5.tianditu.gov.cn:443;
  server t6.tianditu.gov.cn:443;
  server t7.tianditu.gov.cn:443;
}

proxy_cache_path /tmp/tianditu_cache levels=1:2 keys_zone=tianditu_cache:10m max_size=1g inactive=7d;

location 配置

 location /xxx/ {
    set $args "$args&tk=xxx";
    proxy_pass https://tianditu/;
    proxy_ssl_server_name on;        # 必须 on 配合proxy_set_header  TLS 握手时 SNI 信息
    proxy_set_header Host t0.tianditu.gov.cn;  # 固定真实域名
    proxy_ssl_verify off;             # 关闭证书验证
    proxy_set_header Connection "";   # 配合 keepalive 必须加
    proxy_http_version 1.1;           # 配合 keepalive 必须加

    proxy_cache tianditu_cache;
    proxy_cache_valid 200 7d;          # 200响应缓存7天
    proxy_cache_key "$uri$args";       # 用路径+参数作为缓存key
    add_header X-Cache-Status $upstream_cache_status;  # 调试用,看是否命中缓
  }

总结

针对多子域名,需要隐藏token 的服务都可以用类似的处理