內網走nginx代理訪問高德webapi2.0

来源:https://www.cnblogs.com/jiekzou/archive/2023/11/16/17837106.html
-Advertisement-
Play Games

需求:客戶的電腦都只能訪問內,伺服器可以訪問外網,客戶電腦使用的項目中用到了高德webapi2.0。10.200.31.45:32100是我們的web伺服器。 網上基本上都是對高德webapi1.4的配置方式,而web2.0有一些差別。 1.前端修改高德地圖的js應用 如果是index.html引入 ...


需求:客戶的電腦都只能訪問內,伺服器可以訪問外網,客戶電腦使用的項目中用到了高德webapi2.0。
10.200.31.45:32100是我們的web伺服器。

網上基本上都是對高德webapi1.4的配置方式,而web2.0有一些差別。

1.前端修改高德地圖的js應用

如果是index.html引入,

修改之前的應用:

    <script type="text/javascript" crossorigin
        src="https://webapi.amap.com/maps?v=2.0&key=高德key&plugin=AMap.ToolBar, AMap.ControlBar,AMap.MoveAnimation,AMap.RangingTool,AMap.Geocoder"></script>
    <script src="https://webapi.amap.com/loca?v=2.0.0&key=高德key"></script>

修改之後的配置:

    <script type="text/javascript" crossorigin
    src="<%= BASE_URL %>maps?v=2.0&key=高德key&plugin=AMap.ToolBar, AMap.ControlBar,AMap.MoveAnimation,AMap.RangingTool,AMap.Geocoder"></script>
<script src="<%= BASE_URL %>loca?v=2.0.0&key=高德key"></script>

如果是js動態載入,代碼如下:

/**
 * 創建高德秘鑰
 * @param secret 
 */
export function createSecret(secret?:string){
         secret=secret||'高德秘鑰';
         // 拼裝腳本字元串
         const script = document.createElement('script');
          // 高德 Web API 初始化配置
         script.innerHTML = `
           window._AMapSecurityConfig = {
            securityJsCode: '${secret}',
          };`;
         // 將腳本插入到頁面頭部
         document.head.appendChild(script);
}
//獲取地圖地址
const getMapUrl=()=>{
  const {map_offline}=Local.getAmapConfig();
  let mapUrl='https://webapi.amap.com';
   if(map_offline){
    mapUrl='';
   }
  if (process.env.NODE_ENV === "development"){
    mapUrl='https://webapi.amap.com';
  }
  return mapUrl;
}
/**
 * 創建高德script引入腳本
 * @param key 
 * @param url 
 */
export function loadAMapScript(url:string,key?:string) {
  key=key||'高德key';
  const mapUrl=getMapUrl();
  const src =`${mapUrl}/maps?v=2.0&key=${key}${url}`;
  console.log('我的src',src);
  return new Promise((resolve, reject) => {
    const script = document.createElement('script');
    script.src = src;
    script.async = true;
    script.onerror = reject;
    script.onload = resolve;
    document.head.appendChild(script);
  });
}
/**
 * 創建高德local script引入腳本
 * @param key 
 * @param url 
 */
 export function loadAMapLocaScript(key?:string,url?:string=''){
  key=key||'高德key';
  const mapUrl=getMapUrl();
  const src =`${mapUrl}/loca?v=2.0&key=${key}${url}`;
  return new Promise((resolve, reject) => {
    const script = document.createElement('script');
    script.src = src;
    script.async = true;
    script.onerror = reject;
    script.onload = resolve;
    document.head.appendChild(script);
  });
}

然後在main.ts中:

import {DefaultAmapConfig} from "@/xd-common-web/xdConst";
import {loadAMapLocaScript,loadAMapScript,createSecret} from "@/xd-common-web/utils/map/amapLink";
try{
console.log('開始載入地圖');
const mapConfig=Local.getAmapConfig()||DefaultAmapConfig;
createSecret(mapConfig.map_secret);
await loadAMapScript("&&plugin=AMap.LineSearch,AMap.StationSearch,AMap.PolylineEditor,AMap.Geocoder,AMap.PlaceSearch,AMap.AutoComplete,Amap.ElasticMarker,AMap.PolygonEditor",mapConfig.map_key);
await loadAMapLocaScript(mapConfig.map_key);
  console.log('地圖載入成功');
} catch{
  console.log('地圖載入異常');
}

2.nginx配置如下:

       location /restapi/ {
          proxy_pass https://restapi.amap.com/;
        }
       location /restapi/v3/ {
          proxy_pass https://restapi.amap.com/v3/;
        }
        location /webapi/ {
                proxy_pass https://webapi.amap.com/;
        }
        location /webapi/count{
           proxy_pass https://webapi.amap.com/count;
        }
        location /vdata/ {
                proxy_pass https://vdata.amap.com/;
        }
       location /vdata/style_icon/ {
            proxy_pass   http://vdata.amap.com/style_icon/;
        }
       location /vdata/style/{
           proxy_pass https://vdata.amap.com/style/;
        }
        location /vdata01/ {
             proxy_pass   https://vdata01.amap.com/;
        }
        location /vdata02/ {
             proxy_pass   https://vdata02.amap.com/;
        }
         location /vdata03/ {
             proxy_pass   https://vdata03.amap.com/;
        }
        location /vdata04/ {
             proxy_pass   https://vdata04.amap.com/;
        }
         location /wprd01/ {
            proxy_pass   https://wprd01.is.autonavi.com/;
        }
        location /wprd02/ {
            proxy_pass   https://wprd02.is.autonavi.com/;
        }
        location /wprd03/ {
            proxy_pass   https://wprd03.is.autonavi.com/;
        }
        location /wprd04/ {
            proxy_pass   https://wprd04.is.autonavi.com/;
        }
       location /webrd01/ {
          proxy_pass   https://webrd01.is.autonavi.com/;
        }
        location /webrd02/ {
            proxy_pass   https://webrd02.is.autonavi.com/;
        }
        location /webrd03/ {
            proxy_pass   https://webrd03.is.autonavi.com/;
        }
        location /webrd04/ {
            proxy_pass   https://webrd04.is.autonavi.com/;
        }

        #代理獲取js api文件並修改文件內容
        location /maps {
            proxy_set_header Accept-Encoding "";
            proxy_pass https://webapi.amap.com/maps;
            sub_filter_types *;
            sub_filter_once off;
            sub_filter 'http://webapi.amap.com' 'http://10.200.31.45:32100/webapi';
            sub_filter 'https://webapi.amap.com' 'http://10.200.31.45:32100/webapi';
            sub_filter 'http://restapi.amap.com' 'http://10.200.31.45:32100/restapi';
            sub_filter 'https://restapi.amap.com' 'http://10.200.31.45:32100/restapi';
            sub_filter 'http://vdata.amap.com' 'http://10.200.31.45:32100/vdata';
            sub_filter 'https://vdata.amap.com' 'http://10.200.31.45:32100/vdata';
            sub_filter 'webapi.amap.com' '10.200.31.45:32100/webapi';
            sub_filter 'vdata.amap.com' '10.200.31.45:32100/vdata';
            sub_filter 'restapi.amap.com' '10.200.31.45:32100/restapi';

            sub_filter 'vdata0{1,2,3,4}.amap.com' '10.200.31.45:32100/vdata0{1,2,3,4}';
            sub_filter '{vdata,vdata01,vdata02,vdata03,vdata04}.amap.com' '10.200.31.45:32100/{vdata,vdata01,vdata02,vdata03,vdata04}';
            sub_filter 'webapi.amap.com/count' '10.200.31.45:32100/webapi/count';
            sub_filter 'webapi.amap.com/theme' '10.200.31.45:32100/webapi/theme';
            sub_filter 'restapi.amap.com/v3' '10.200.31.45:32100/restapi/v3';
            sub_filter 'webapi.amap.com/style' '10.200.31.45:32100/webapi/style';

           sub_filter 'wprd0{1,2,3,4}.is.autonavi.com' '10.200.31.45:32100/wprd0{1,2,3,4}';
           sub_filter 'webrd0{1,2,3,4}.is.autonavi.com' '10.200.31.45:32100/wprd0{1,2,3,4}';

           sub_filter 'https' 'http';
        }
        location /loca {
            proxy_set_header Accept-Encoding "";
            proxy_pass   https://webapi.amap.com/loca;
            sub_filter_types *;
            sub_filter_once off;
            sub_filter 'webapi.amap.com/count' '10.200.31.45:32100/webapi/count';   
            sub_filter 'https' 'http';
        }

至此,就配置好了。註意:實際項目中nginx配置文件中的10.200.31.45:32100可使用變數代替。例如:

        #代理獲取js api文件並修改文件內容
        location /maps {
            set $proxyip "10.200.31.45:32100";
            proxy_set_header Accept-Encoding "";
            proxy_pass https://webapi.amap.com/maps;
            sub_filter_types *;
            sub_filter_once off;
            sub_filter 'http://webapi.amap.com' 'http://$proxyip/webapi';
            sub_filter 'https://webapi.amap.com' 'http://$proxyip/webapi';
            sub_filter 'http://restapi.amap.com' 'http://$proxyip/restapi';
            sub_filter 'https://restapi.amap.com' 'http://$proxyip/restapi';
            sub_filter 'http://vdata.amap.com' 'http://$proxyip/vdata';
            sub_filter 'https://vdata.amap.com' 'http://$proxyip/vdata';
            sub_filter 'webapi.amap.com' '$proxyip/webapi';
            sub_filter 'vdata.amap.com' '$proxyip/vdata';
            sub_filter 'restapi.amap.com' '$proxyip/restapi';

            sub_filter 'vdata0{1,2,3,4}.amap.com' '$proxyip/vdata0{1,2,3,4}';
            sub_filter '{vdata,vdata01,vdata02,vdata03,vdata04}.amap.com' '$proxyip/{vdata,vdata01,vdata02,vdata03,vdata04}';
            sub_filter 'webapi.amap.com/count' '$proxyip/webapi/count';
            sub_filter 'webapi.amap.com/theme' '$proxyip/webapi/theme';
            sub_filter 'restapi.amap.com/v3' '$proxyip/restapi/v3';
            sub_filter 'webapi.amap.com/style' '$proxyip/webapi/style';

           sub_filter 'wprd0{1,2,3,4}.is.autonavi.com' '$proxyip/wprd0{1,2,3,4}';
           sub_filter 'webrd0{1,2,3,4}.is.autonavi.com' '$proxyip/wprd0{1,2,3,4}';

           sub_filter 'https' 'http';
        }
        location /loca {
            set $proxyip "10.200.31.45:32100";
            proxy_set_header Accept-Encoding "";
            proxy_pass   https://webapi.amap.com/loca;
            sub_filter_types *;
            sub_filter_once off;
            sub_filter 'webapi.amap.com/count' '$proxyip/webapi/count';   
            sub_filter 'https' 'http';
        }

實現思路:通過特定的首碼maps和loca把高德webapi的引用進行轉發,轉發之後,高德載入地圖時,自動會調用一些介面,使用sub_filter 對返回的內容進行替換,替換為伺服器的地址+特定首碼,再根據首碼二次轉發即可。

博客地址:http://www.cnblogs.com/jiekzou/
博客版權:本文以學習、研究和分享為主,歡迎轉載,但必須在文章頁面明顯位置給出原文連接。
如果文中有不妥或者錯誤的地方還望高手的你指出,以免誤人子弟。如果覺得本文對你有所幫助不如【推薦】一下!如果你有更好的建議,不如留言一起討論,共同進步!
再次感謝您耐心的讀完本篇文章。
其它: .net-QQ群4:612347965 java-QQ群:805741535 H5-QQ群:773766020
我的拙作 《Vue3.x+TypeScript實踐指南》 《ASP.NET MVC企業級實戰》 《H5+移動應用實戰開發》 《Vue.js 2.x實踐指南》 《JavaScript實用教程 》 《Node+MongoDB+React 項目實戰開發》 已經出版,希望大家多多支持!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • sql server 2005安裝包sql server 2005 SP4補丁包(非常難找,留作備用) 鏈接: https://pan.baidu.com/s/1j5OOX-iV8gLrmSNqNLE-kg 提取碼: jvtr 複製這段內容後打開百度網盤手機App,操作更方便哦 背景: 在windo ...
  • 嘗試用node編寫一個簡單的登錄介面,結果啟動服務後請求介面出現了該錯誤。 其問題就是訪問的工具身份驗證協議過於落後,在node內安裝的2.18.1 mysql包。 解決: 先登錄資料庫。 use mysql;(mysql為資料庫名) 提示Database changed; 查詢表中信息 ; sel ...
  • 本文以GaussDB資料庫為平臺,將詳細介紹SQL中DROP、TRUNCATE和DELETE等語句的含義、使用場景以及註意事項,幫助讀者更好地理解和掌握這些常用的資料庫操作命令。 ...
  • 作為一款火山引擎推出的雲原生數據倉庫,ByteHouse基於開源ClickHouse構建,併在位元組跳動內外部場景的檢驗下,對OLAP引擎能力、性能、運維、架構進一步升級。除此之外,ByteHouse也在Serverless方向探索,基於cloud-native 雲原生的理念構建了全新一代的數據倉庫,... ...
  • 本章將介紹如何在 HarmonyOS 上進行實際項目開發。我們將從項目需求分析開始,逐步完成項目的設計、開發、測試和上線過程。 ...
  • 目錄準備界面:view控制項LayoutCreator事件監聽OnClickListener轉跳頁面IntentIntent傳遞數據Toast和AlertDialogGson使用OKhttp3的基本使用post方法get方法輕量級存儲SharedPreferenceListView基本使用1、Simp ...
  • 作為一名全棧工程師,在日常的工作中,可能更側重於後端開發,如:C#,Java,SQL ,Python等,對前端的知識則不太精通。在一些比較完善的公司或者項目中,一般會搭配前端工程師,UI工程師等,來彌補後端開發的一些前端經驗技能上的不足。但並非所有的項目都會有專職前端工程師,在一些小型項目或者初創公... ...
  • 這裡給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 在我們寫項目代碼時,應該更加專註於業務邏輯的實現,而把定式代碼交給js庫或工程化自動處理,而我想說的是,請求邏輯其實也是可以繼續簡化的。 你可能會說,用axios或fetch api就夠了啊,哪有什麼請求邏輯,那可能是你還沒有意識到這個問 ...
一周排行
    -Advertisement-
    Play Games
  • 最近做項目過程中,使用到了海康相機,官方只提供了C/C++的SDK,沒有搜尋到一個合適的封裝了的C#庫,故自己動手,簡單的封裝了一下,方便大家也方便自己使用和二次開發 ...
  • 前言 MediatR 是 .NET 下的一個實現消息傳遞的庫,輕量級、簡潔高效,用於實現進程內的消息傳遞機制。它基於中介者設計模式,支持請求/響應、命令、查詢、通知和事件等多種消息傳遞模式。通過泛型支持,MediatR 可以智能地調度不同類型的消息,非常適合用於領域事件處理。 在本文中,將通過一個簡 ...
  • 前言 今天給大家推薦一個超實用的開源項目《.NET 7 + Vue 許可權管理系統 小白快速上手》,DncZeus的願景就是做一個.NET 領域小白也能上手的簡易、通用的後臺許可權管理模板系統基礎框架。 不管你是技術小白還是技術大佬或者是不懂前端Vue 的新手,這個項目可以快速上手讓我們從0到1,搭建自 ...
  • 第1章:WPF概述 本章目標 瞭解Windows圖形演化 瞭解WPF高級API 瞭解解析度無關性概念 瞭解WPF體繫結構 瞭解WPF 4.5 WPF概述 ​ 歡迎使用 Windows Presentation Foundation (WPF) 桌面指南,這是一個與解析度無關的 UI 框架,使用基於矢 ...
  • 在日常開發中,並不是所有的功能都是用戶可見的,還在一些背後默默支持的程式,這些程式通常以服務的形式出現,統稱為輔助角色服務。今天以一個簡單的小例子,簡述基於.NET開發輔助角色服務的相關內容,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 第3章:佈局 本章目標 理解佈局的原則 理解佈局的過程 理解佈局的容器 掌握各類佈局容器的運用 理解 WPF 中的佈局 WPF 佈局原則 ​ WPF 視窗只能包含單個元素。為在WPF 視窗中放置多個元素並創建更貼近實用的用戶男面,需要在視窗上放置一個容器,然後在這個容器中添加其他元素。造成這一限制的 ...
  • 前言 在平時項目開發中,定時任務調度是一項重要的功能,廣泛應用於後臺作業、計劃任務和自動化腳本等模塊。 FreeScheduler 是一款輕量級且功能強大的定時任務調度庫,它支持臨時的延時任務和重覆迴圈任務(可持久化),能夠按秒、每天/每周/每月固定時間或自定義間隔執行(CRON 表達式)。 此外 ...
  • 目錄Blazor 組件基礎路由導航參數組件參數路由參數生命周期事件狀態更改組件事件 Blazor 組件 基礎 新建一個項目命名為 MyComponents ,項目模板的交互類型選 Auto ,其它保持預設選項: 客戶端組件 (Auto/WebAssembly): 最終解決方案裡面會有兩個項目:伺服器 ...
  • 先看一下效果吧: isChecked = false 的時候的效果 isChecked = true 的時候的效果 然後我們來實現一下這個效果吧 第一步:創建一個空的wpf項目; 第二步:在項目裡面添加一個checkbox <Grid> <CheckBox HorizontalAlignment=" ...
  • 在編寫上位機軟體時,需要經常處理命令拼接與其他設備進行通信,通常對不同的命令封裝成不同的方法,擴展稍許麻煩。 本次擬以特性方式實現,以兼顧維護性與擴展性。 思想: 一種命令對應一個類,其類中的各個屬性對應各個命令段,通過特性的方式,實現其在這包數據命令中的位置、大端或小端及其轉換為對應的目標類型; ...