如果浏览器所在环境不能访问外网,又要实现地图功能,而且服务器能上外网的话,那么就可以通过代理实现内网环境访问百度地图。
值得注意的是此种代理方案并不能解决所有的百度地图的功能,但对于基本的地图功能还是基本能满足的
通过代理实现有两种方案:一是手动下载百度js,并替换里面的域名为代理服务的地址,此种方式最简单,但不利于后期更新维护;而是通过第三方的过滤模块来修改js里面的域名为代理服务的地址,此种方式虽然省去了手动修改的弊病,但由于引入第三方模块,nginx需要重新编译,可靠性也得不到保障。
查找相关资料知道nginx有http_sub_module模块可以修改服务器返回的内容,但只能修改一次, 不支持多次替换,而网上有第三方模块ngx_http_substitutions_filter_module则可以多次修改替换,遗憾的是ngx_http_substitutions_filter_module已经很久没更新了
本文决定以第一种方式进行实现,第二种方式理论上更好,但实现较麻烦复杂,可以自行尝试。
http://api.map.baidu.com/getscript 获得的域名如下:
map.baidu.com
sapi.map.baidu.com
api.map.baidu.com
its.map.baidu.com
lbsyun.baidu.com
loc.map.baidu.com
webmap0.map.bdimg.com
mapclick.map.baidu.com(ping不通)
wuxian.baidu.com(ping不通)
static.tieba.baidu.com
or.map.bdimg.com
j.map.baidu.com(ping不通)
shangetu0.map.bdimg.com
shangetu1.map.bdimg.com
shangetu2.map.bdimg.com
shangetu3.map.bdimg.com
shangetu4.map.bdimg.com
online0.map.bdimg.com
online1.map.bdimg.com
online2.map.bdimg.com
online3.map.bdimg.com
online4.map.bdimg.com
ss0.baidu.com
ss0.bdstatic.com
d0.map.baidu.com
d1.map.baidu.com
d2.map.baidu.com
d3.map.baidu.com
gss0.bdstatic.com
gsp0.baidu.com
g0.api.map.baidu.com
g1.api.map.baidu.com
g2.api.map.baidu.com
g3.api.map.baidu.com
pcsv0.map.bdimg.com
pcsv1.map.bdimg.com
pcsv2.map.bdimg.com
api0.map.bdimg.com
api1.map.bdimg.com
api2.map.bdimg.com
以上ping不通的不能加到nginx配置里,否则类似错误:
nginx: [emerg] host not found in upstream “j.map.bdimg.com” in /etc/nginx/conf.d/nginx-bdmap.conf:36
server {
listen 8480;
server_name localhost;
location /api.map.baidu.com/ {
proxy_pass http://api.map.baidu.com/;
}
location /sapi.map.baidu.com/ {
proxy_pass http://sapi.map.baidu.com/;
}
location /map.baidu.com/ {
proxy_pass http://map.baidu.com/;
}
location /its.map.baidu.com/ {
proxy_pass http://its.map.baidu.com/;
}
location /lbsyun.baidu.com/ {
proxy_pass http://lbsyun.baidu.com/;
}
location /loc.map.baidu.com/ {
proxy_pass http://loc.map.baidu.com/;
}
location /webmap0.map.bdimg.com/ {
proxy_pass http://webmap0.map.bdimg.com/;
}
location /static.tieba.baidu.com/ {
proxy_pass http://static.tieba.baidu.com/;
}
location /or.map.bdimg.com/ {
proxy_pass http://or.map.bdimg.com/;
}
location /ss0.bdstatic.com/ {
proxy_pass http://ss0.bdstatic.com/;
}
location /ss0.baidu.com/ {
proxy_pass http://ss0.baidu.com/;
}
location /d0.map.baidu.com/ {
proxy_pass http://d0.map.baidu.com/;
}
location /d1.map.baidu.com/ {
proxy_pass http://d1.map.baidu.com/;
}
location /d2.map.baidu.com/ {
proxy_pass http://d2.map.baidu.com/;
}
location /d3.map.baidu.com/ {
proxy_pass http://d3.map.baidu.com/;
}
location /online0.map.bdimg.com/ {
proxy_pass http://online0.map.bdimg.com/;
}
location /online1.map.bdimg.com/ {
proxy_pass http://online1.map.bdimg.com/;
}
location /online2.map.bdimg.com/ {
proxy_pass http://online2.map.bdimg.com/;
}
location /online3.map.bdimg.com/ {
proxy_pass http://online3.map.bdimg.com/;
}
location /online4.map.bdimg.com/ {
proxy_pass http://online4.map.bdimg.com/;
}
location /shangetu0.map.bdimg.com/ {
proxy_pass http://shangetu0.map.bdimg.com/;
}
location /shangetu1.map.bdimg.com/ {
proxy_pass http://shangetu1.map.bdimg.com/;
}
location /shangetu2.map.bdimg.com/ {
proxy_pass http://shangetu2.map.bdimg.com/;
}
location /shangetu3.map.bdimg.com/ {
proxy_pass http://shangetu3.map.bdimg.com/;
}
location /shangetu4.map.bdimg.com/ {
proxy_pass http://shangetu4.map.bdimg.com/;
}
location /gss0.bdstatic.com/ {
proxy_pass http://gss0.bdstatic.com/;
}
location /gsp0.baidu.com/ {
proxy_pass http://gsp0.baidu.com/;
}
location /g0.api.map.baidu.com/ {
proxy_pass http://g0.api.map.baidu.com/;
}
location /g1.api.map.baidu.com/ {
proxy_pass http://g1.api.map.baidu.com/;
}
location /g2.api.map.baidu.com/ {
proxy_pass http://g2.api.map.baidu.com/;
}
location /g3.api.map.baidu.com/ {
proxy_pass http://g3.api.map.baidu.com/;
}
location /pcsv0.map.bdimg.com/ {
proxy_pass http://pcsv0.map.bdimg.com/;
}
location /pcsv1.map.bdimg.com/ {
proxy_pass http://pcsv1.map.bdimg.com/;
}
location /pcsv2.map.bdimg.com/ {
proxy_pass http://pcsv2.map.bdimg.com/;
}
location /api0.map.bdimg.com/ {
proxy_pass http://api0.map.bdimg.com/;
}
location /api1.map.bdimg.com/ {
proxy_pass http://api1.map.bdimg.com/;
}
location /api2.map.bdimg.com/ {
proxy_pass http://api2.map.bdimg.com/;
}
}
root@034b8504e2f7:/# nginx -V
nginx version: nginx/1.15.8
built by gcc 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)
built with OpenSSL 1.1.0j 20 Nov 2018
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.15.8/debian/debuild-base/nginx-1.15.8=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-specs=/usr/share/dpkg/no-pie-link.specs -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
企业内网反向代理百度地图服务 原 - 云+社区 - 腾讯云
https://cloud.tencent.com/developer/article/1185304
nginx ngx_http_sub_module使用 - iuwai - 博客园
https://www.cnblogs.com/iuwai/p/4432084.html
nginx使用replace-filter-nginx-module实现内容替换 - 飞鸿影~ - 博客园
https://www.cnblogs.com/52fhy/p/7956099.html
手动编译安装nginx - 一曲秋殇 - 博客园
https://www.cnblogs.com/luobiao320/p/7189934.html
nginx代理百度地图,实现内网展示百度地图 - yezip的博客 - CSDN博客
https://blog.csdn.net/yezip/article/details/79785480