首页 > 系统相关 >内网走nginx代理访问高德webapi2.0

内网走nginx代理访问高德webapi2.0

时间:2023-11-16 19:58:02浏览次数:40  
标签:webapi sub com filter nginx webapi2.0 amap https 内网

需求:客户的电脑都只能访问内,服务器可以访问外网,客户电脑使用的项目中用到了高德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可使用变量代替。

实现思路:通过特定的前缀maps和loca把高德webapi的引用进行转发,转发之后,高德加载地图时,自动会调用一些接口,使用sub_filter 对返回的内容进行替换,替换为服务器的地址+特定前缀,再根据前缀二次转发即可。

标签:webapi,sub,com,filter,nginx,webapi2.0,amap,https,内网
From: https://www.cnblogs.com/jiekzou/p/17837106.html

相关文章

  • Ubuntu 22.04 LTS 安装最新稳定版本nginx、mysql5.7和php7.2
    Ubuntu22.04LTS安装最新稳定版本nginx、mysql5.7和php7.2全部apt-get安装,就是快,迅速。前提是需要在有网络环境的情况下哈!!操作系统版本:Ubuntu22.04LTS一、安装最新稳定版本的nginxapt-getupdate#查看默认安装的nginx版本(默认为1.18。有点老,我们安装最新稳定版本)apt......
  • frp内网穿透
    frp内网穿透内网client服务器可以访问外网一台有公网IP的服务器(可以用ECS,我这里用的是一台阿里云ECS)我这里是以ubuntu部署远程ssh的环境1.下载frp包:wgethttps://github.com/fatedier/frp/releases/download/v0.52.3/frp_0.52.3_linux_amd64.tar.gz可以去https://github.......
  • nginx 日志备份
    ·1、编辑脚本backup.sh#!/bin/bash#进入备份目录cd/data/nginx/logs/#设置备份名字newAccessLog="access`date+%Y-%m-%d`.log"newErrorLog="error`date+%Y-%m-%d`.log"#拷贝日志文件,已经在配置文件设置日志文件存放在:/data/nginx/logs/下,如果没有修改日志路径的一般在:/u......
  • nginx 定时重启
    1、编写脚本nginx_restart.sh#!/bin/bashps-ef|grepnginx|grep-vgrep>/data/nginx/nginx_restart.txtfile=$(cat/data/nginx/nginx_restart.txt|grepprocess|grep-vnginx_restart|awk'{print$2}')file_num=`cat/data/nginx/nginx_restart.......
  • nginx 日志查询
    转载:https://www.jianshu.com/p/f105fb19dd0b1、根据访问IP统计UVawk'{print$1}'access.log|sort|uniq-c|wc-l2、统计访问URL统计PVawk'{print$7}'access.log|wc-l3、查询访问最频繁的URLawk'{print$7}'access.log|sort|uniq-c|sort-n-k1-r|m......
  • skywalking(二) 实现基于nginx+java服务的全链路数据收集
    实现nginx+jenkins全链路数据追踪1.部署JenkinsIP:10.0.0.941.1安装、配置jenkins#1.安装jdk11aptupdateaptinstall-yopenjdk-11-jdk#2.下载tomcatmdkir/apps&cd/appswgethttps://dlcdn.apache.org/tomcat/tomcat-8/v8.5.93/bin/apache-tomcat-8.5.93.tar.g......
  • nginx配置kibana访问用户名和密码认证、及无认证访问配置
    在nginx上配置kibana页面访问时,默认是采用kibana的认证,一般直接安装kibana后,是没有用户名和密码认证的。如果要在负载均衡上配置反向代理和用户认证,可按以下步骤进行配置:1.安装Nginx:首先,确保已经安装了Nginx,并且可以正常访问Kibana页面。2.生成密码文件:使用htpa......
  • web nginx 大量time_wait 几乎没有establish
     #!/usr/bin/python#-*-coding:utf-8-*-#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#@auhorbyruiy####pipinstallparamiko-ihttps://pypi.tuna.tsinghua.edu.cn/simple##pipinstallpsutil-ihttps:/......
  • nginx安装ssl模块
    一、引言当我们的Linux服务器上当中发布了web项目,有时候需要配置一个SSL证书,来使用https,然而我们一开始编译的Nginx的时候并没有把SSL模块一起编译进去.二、如何补装SSL模块既然在安装的时候没有编译ssl,难道把nginx卸载重新安装一次?不不不,我们只需要在原有的基础上添加ssl模块......
  • Widows环境下安装Nginx并配置开机自启
    1下载Nginx下载地址:http://nginx.org/en/download.html2启动Nginxnginx的启动方式有两种:一种是直接点击nginx.exe启动,另一种是通过命令行启动2.1直接启动找到nginx目录,双击nginx.exe即可启动2.2命令行启动在nginx目录地址栏输入cmd,进入cmd窗口输入下列命令行nginx.exe或者st......