通过nginx代理实现内网访问百度地图方案
通過nginx代理實(shí)現(xiàn)內(nèi)網(wǎng)訪問百度地圖方案
文章目錄
- 通過nginx代理實(shí)現(xiàn)內(nèi)網(wǎng)訪問百度地圖方案
- 前言
- 第一種方式實(shí)現(xiàn)(3步)
- 獲取js中的域名(1)
- 替換域名(2)
- 配置nginx(3)
- 其他
- 參考
前言
-
如果瀏覽器所在環(huán)境不能訪問外網(wǎng),又要實(shí)現(xiàn)地圖功能,而且服務(wù)器能上外網(wǎng)的話,那么就可以通過代理實(shí)現(xiàn)內(nèi)網(wǎng)環(huán)境訪問百度地圖。
值得注意的是此種代理方案并不能解決所有的百度地圖的功能,但對于基本的地圖功能還是基本能滿足的
-
通過代理實(shí)現(xiàn)有兩種方案:一是手動下載百度js,并替換里面的域名為代理服務(wù)的地址,此種方式最簡單,但不利于后期更新維護(hù);二是通過nginx的過濾模塊來修改js里面的域名為代理服務(wù)的地址,此種方式雖然省去了手動修改的弊病,但由于引入第三方模塊,nginx需要重新編譯,可靠性也得不到保障。
查找相關(guān)資料知道nginx有http_sub_module模塊可以修改服務(wù)器返回的內(nèi)容,但只能修改一次, 不支持多次替換,而網(wǎng)上有第三方模塊ngx_http_substitutions_filter_module則可以多次修改替換,遺憾的是ngx_http_substitutions_filter_module已經(jīng)很久沒更新了
-
本文決定以第一種方式進(jìn)行實(shí)現(xiàn),第二種方式理論上更好,但實(shí)現(xiàn)較麻煩復(fù)雜,可以自行嘗試。
第一種方式實(shí)現(xiàn)(3步)
獲取js中的域名(1)
-
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.comonline0.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.comgss0.bdstatic.com gsp0.baidu.com g0.api.map.baidu.com g1.api.map.baidu.com g2.api.map.baidu.com g3.api.map.baidu.compcsv0.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
替換域名(2)
- 將js中獲取到的域名替換為代理服務(wù),如在此處將api0.map.bdimg.com替換為[ip]:[port]/api0.map.bdimg.com,其中ip,port則為代理服務(wù)nginx的ip和端口,其他域名類推。
配置nginx(3)
- 在nginx的nginx.conf中增加如下代理配置,修改后重新reload一下
- 經(jīng)過以上三步后即可百度地圖的代理
其他
- 由于ngx_http_substitutions_filter_module是第三方模塊,因此要使用的話得重新編譯nginx,我們可以利用dockerhub里的官方nginx的configure參數(shù)來做一些修改即可編譯出我們需要的nginx
- 以下配置參數(shù)是從dockerhub 官方nginx查詢獲得的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'
參考
企業(yè)內(nèi)網(wǎng)反向代理百度地圖服務(wù) 原 - 云+社區(qū) - 騰訊云
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實(shí)現(xiàn)內(nèi)容替換 - 飛鴻影~ - 博客園
https://www.cnblogs.com/52fhy/p/7956099.html
手動編譯安裝nginx - 一曲秋殤 - 博客園
https://www.cnblogs.com/luobiao320/p/7189934.html
nginx代理百度地圖,實(shí)現(xiàn)內(nèi)網(wǎng)展示百度地圖 - yezip的博客 - CSDN博客
https://blog.csdn.net/yezip/article/details/79785480
總結(jié)
以上是生活随笔為你收集整理的通过nginx代理实现内网访问百度地图方案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多点触控笔记
- 下一篇: 经典算法题(中级)-----自定义函数之