Nginx代理天地图:国企内网访问外网地图服务的高效配置指南
随着国家对网络安全越来越重视,很多国企服务器不允许访问外网,只允许在其中一台开放某几个IP和端口访问外网。如果用到天地图服务,需要在这台服务器安装一个nginx进行中转,并申请开放天地图IP地址的443端口。
Nginx配置
在服务器上,nginx添加以下配置可以代理访问天地图。
http {
upstream tianditu_server {
server t0.tianditu.gov.cn:443 weight=1 max_fails=2 fail_timeout=30s;
server t1.tianditu.gov.cn:443 weight=1 max_fails=2 fail_timeout=30s;
server t2.tianditu.gov.cn:443 weight=1 max_fails=2 fail_timeout=30s;
server t3.tianditu.gov.cn:443 weight=1 max_fails=2 fail_timeout=30s;
server t4.tianditu.gov.cn:443 weight=1 max_fails=2 fail_timeout=30s;
server t5.tianditu.gov.cn:443 weight=1 max_fails=2 fail_timeout=30s;
server t6.tianditu.gov.cn:443 weight=1 max_fails=2 fail_timeout=30s;
server t7.tianditu.gov.cn:443 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen *:80;
server_name localhost;
location /tianditu/ {
proxy_pass https://tianditu_server/;
proxy_set_header Host t6.tianditu.gov.cn;
proxy_ssl_session_reuse off;
proxy_ssl_server_name on;
proxy_ssl_name $proxy_host;
proxy_ssl_protocols TLSv1.2;
}
}
}
使用方法
原始地址:https://t0.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX=13&TILEROW=3198&TILECOL=6762&tk=a76b9ea6e49fb0eecdb1ed34d1e75930
代理后访问地址:https://ip:port/tianditu/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX=13&TILEROW=3198&TILECOL=6762&tk=a76b9ea6e49fb0eecdb1ed34d1e75930

常见问题
1、配置代理后,使用代理请求天地图,地图上不显示底图。
解决方法:(1)先用一个瓦片测试一下配置的代理,确认是否能正常获取到瓦片。例如:
http://localhost:80/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX=14&TILEROW=6752&TILECOL=12899&tk=天地图tk
(2)如果瓦片不能显示,则是nginx配置问题。在nginx的bin目录执行以下命令,验证nginx配置是否正确。因为nginx配置如果是错误的,nginx -s reload不会生效。
nginx -t
(3)如果nginx -t不报错,执行nginx -s reload后,仍然获取不到瓦片,则把任务管理器中的nginx进程都结束,重新双击nginx.exe启动nginx即可。