• 您的位置: 紫荷精灵 >> 软件网络 >> 解决谷歌中国地图Google Maps Api失效:NGINX代理配置全过程
  • 解决谷歌中国地图Google Maps Api失效:NGINX代理配置全过程

  • 作者:admin  来源:原创  日期:2022/10/19 9:24:04
  • 本文主要介绍一种快速搭建Google maps api代理的方法,用以解决国内无法使用谷歌地图接口的问题。本文核心内容来自:GitHub: ijialong1314/googlemap-proxy

    本文于2020年3月23日进行了一次重要修正。

    背景

    2020年1月23日,收到谷歌邮件,标题为“[Action Required] Google Maps Platform API calls from google.cn domain will fail”。邮件中提到,自2020年2月3日起,谷歌中国域名google.cn将不再指向谷歌地图api。原使用http://maps.google.cn/maps/api/js?key=xxx 方式调用谷歌地图方法,将不再有效。必须统一使用 https://maps.googleapis.com/maps/api 方式进行调用。而由于maps.googleapis.com在国内访问不畅,导致国内无法使用。这也就意味着谷歌彻底放弃了在中国的地图接口调用。

    虽然由于观点原因,国内少见谷歌地图的使用,但是对于一些外贸企业来讲,谷歌地图仍然应该是首选。若谷歌地图访问不畅,则许多外贸网站在国内的访问速度会受到影响。所以,本文主要探索一种快速搭建google maps api代理的方法,以解决当下的燃眉之急。

    基本介绍

    基于Centos7 64bit、军哥lnmp 1.6、nginx 1.16.1、手动修改部分nginx配置。

    域名规划为:
    maps.your-domain.com
    mapsapis.your-domain.com

    本方法在阿里云香港ECS、和某海外vps商家测试通过。

    搭建流程

    只写命令行,具体看注释

    1. # 安装lnmp
    2. wget http://soft.vpser.net/lnmp/lnmp1.6.tar.gz -cO lnmp1.6.tar.gz && tar zxf lnmp1.6.tar.gz && cd lnmp1.6 && ./install.sh nginx
    3.  
    4. # 安装nginx相关包
    5. yum install bison git
    6. git clone https://github.com/agentzh/sregex
    7. cd sregex
    8. make && make install
    9.  
    10. cd /usr/local/nginx/
    11. mkdir modules && cd modules
    12.  
    13. git clone https://github.com/agentzh/replace-filter-nginx-module
    14.  
    15. cd /root/lnmp1.6
    16.  
    17. vim lnmp.conf
    18.  
    19. # 修改这一行,添加:
    20. Nginx_Modules_Options="--add-module=/usr/local/nginx/modules/replace-filter-nginx-module --with-ld-opt='-Wl,-rpath,/usr/local/lib'"
    21.  
    22. # 重新编译nginx 1.16.1
    23. ./upgrade.sh nginx
    24.  
    25. # 添加主机,带ssl
    26. lnmp vhost add
    27.  
    28. # 修改虚拟主机nginx配置文件
    29. cd /usr/local/nginx/conf/vhost/
    30. vi {maps.your-domain.com}.conf

    文件内容,注意,证书那部分原来的不要改动,其余的位置照着写:(注:2020-3-23修正第28行地址)

    1. server
    2. {
    3. listen 443 ssl http2;
    4. #listen [::]:443 ssl http2;
    5. server_name maps.your-domain.com mapsapis.your-domain.com;
    6.  
    7. ssl_certificate /usr/local/nginx/conf/ssl/maps.your-domain.com/fullchain.cer;
    8. ssl_certificate_key /usr/local/nginx/conf/ssl/maps.your-domain.com/maps.your-domain.com.key;
    9. ssl_session_timeout 5m;
    10. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    11. ssl_prefer_server_ciphers on;
    12. ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
    13. ssl_session_cache builtin:1000 shared:SSL:10m;
    14. # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
    15. ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
    16.  
    17. #error_page 404 /404.html;
    18.  
    19. access_log /home/wwwlogs/maps.your-domain.com.log;
    20.  
    21. location /maps/ {
    22.  
    23. #MIME TYPE
    24. default_type text/javascript;
    25.  
    26. proxy_set_header Accept-Encoding '';
    27.  
    28. proxy_pass https://maps.googleapis.com/maps/;
    29.  
    30. replace_filter_max_buffered_size 500k;
    31. replace_filter_last_modified keep;
    32. replace_filter_types text/javascript application/javascript;
    33.  
    34. include vhost/replace_cn.txt;
    35. }
    36.  
    37. location /maps-api-v3/ {
    38. proxy_pass https://maps.googleapis.com/maps-api-v3/;
    39. }
    40.  
    41. include vhost/location_cn.txt;
    42.  
    43. }

    新建location_cn.txt (注:2020-3-23修正第13行地址)

    1. location /e173754b3b1bbd62a0be9c4afea73fdf/ { proxy_pass https://mts0.google.com/; }
    2. location /ce60bcadc66cea58583ddd700f7d80be/ { proxy_pass https://mts1.google.com/; }
    3. location /e6ff3de47e3134794d4442b83c841f20/ { proxy_pass https://khms0.google.com/; }
    4. location /da42f38f94ce6ac80e2806122a7b1933/ { proxy_pass https://khms1.google.com/; }
    5. location /7abac8c11716f6949e7b23f76e60fcd0/ { proxy_pass https://khms0.googleapis.com/; }
    6. location /444c94a4157de3c06c435132eb2f1ac5/ { proxy_pass https://khms1.googleapis.com/; }
    7. location /c7d52b85d86a06df50621e669557ea05/ { proxy_pass https://mts0.googleapis.com/; }
    8. location /57cf283b871304a296c3bd8acde4cc22/ { proxy_pass https://mts1.googleapis.com/; }
    9. location /3e0ae61058d4e7be83d222fb1f107310/ { proxy_pass https://maps.gstatic.com/; }
    10. location /ae5102db1431e3fd01dc8336085d150f/ { proxy_pass https://csi.gstatic.com/; }
    11. location /946eb25413c43b235b5806e999044125/ { proxy_pass https://maps.google.com/; }
    12. location /dcd331573c0980eab6fe7346468e9974/ { proxy_pass https://gg.google.com/; }
    13. location /0a68229ef53c18f5fc37f4106f09a6c9/ { proxy_pass https://khms.google.com/; }
    14. location /e55bb777699a599b09d3011043439a06/ { proxy_pass https://earthbuilder.googleapis.com/; }
    15. location /471b015572d3907c09789af41061e964/ { proxy_pass https://mts.googleapis.com/; }
    16. location /3a4544842f4e8e90a152257489cb594d/ { proxy_pass https://static.panoramio.com.storage.googleapis.com/; }
    17. location /85471b91952baa51514c7fad7c57b8eb/ { proxy_pass https://lh3.ggpht.com/; }
    18. location /44b3a55364b2f63e51f2c6e911f8506f/ { proxy_pass https://lh4.ggpht.com/; }
    19. location /3cc4fbda8f98767ab275af32669f56d4/ { proxy_pass https://lh5.ggpht.com/; }
    20. location /2815063fda2dd0ccf9c566d1f089d4bc/ { proxy_pass https://lh6.ggpht.com/; }
    21. location /468724d08bce9811e989b526c6222863/ { proxy_pass https://www.google.com/; }

    新建replace_cn.txt,务必注意替换其中的两个域名,包括maps和mapapis  (注:2020-3-23修正第15行地址)

    1. replace_filter mts0.google.com maps.your-domain.com/e173754b3b1bbd62a0be9c4afea73fdf ig;
    2. replace_filter mts1.google.com maps.your-domain.com/ce60bcadc66cea58583ddd700f7d80be ig;
    3. replace_filter khms0.google.com maps.your-domain.com/e6ff3de47e3134794d4442b83c841f20 ig;
    4. replace_filter khms1.google.com maps.your-domain.com/da42f38f94ce6ac80e2806122a7b1933 ig;
    5. replace_filter khms0.googleapis.com maps.your-domain.com/7abac8c11716f6949e7b23f76e60fcd0 ig;
    6. replace_filter khms1.googleapis.com maps.your-domain.com/444c94a4157de3c06c435132eb2f1ac5 ig;
    7. replace_filter mts0.googleapis.com maps.your-domain.com/c7d52b85d86a06df50621e669557ea05 ig;
    8. replace_filter mts1.googleapis.com maps.your-domain.com/57cf283b871304a296c3bd8acde4cc22 ig;
    9. replace_filter maps.gstatic.com maps.your-domain.com/3e0ae61058d4e7be83d222fb1f107310 ig;
    10. replace_filter csi.gstatic.com maps.your-domain.com/ae5102db1431e3fd01dc8336085d150f ig;
    11. replace_filter maps.googleapis.com mapsapis.your-domain.com ig;
    12. replace_filter maps.google.com maps.your-domain.com/946eb25413c43b235b5806e999044125 ig;
    13. replace_filter gg.google.com maps.your-domain.com/dcd331573c0980eab6fe7346468e9974 ig;
    14. replace_filter www.google.com maps.your-domain.com/468724d08bce9811e989b526c6222863 ig;
    15. replace_filter khms.google.com maps.your-domain.com/0a68229ef53c18f5fc37f4106f09a6c9 ig;
    16. replace_filter earthbuilder.googleapis.com maps.your-domain.com/e55bb777699a599b09d3011043439a06 ig;
    17. replace_filter mts.googleapis.com maps.your-domain.com/471b015572d3907c09789af41061e964 ig;
    18. replace_filter static.panoramio.com.storage.googleapis.com maps.your-domain.com/3a4544842f4e8e90a152257489cb594d ig;
    19. replace_filter lh3.ggpht.com maps.your-domain.com/85471b91952baa51514c7fad7c57b8eb ig;
    20. replace_filter lh4.ggpht.com maps.your-domain.com/44b3a55364b2f63e51f2c6e911f8506f ig;
    21. replace_filter lh5.ggpht.com maps.your-domain.com/3cc4fbda8f98767ab275af32669f56d4 ig;
    22. replace_filter lh6.ggpht.com maps.your-domain.com/2815063fda2dd0ccf9c566d1f089d4bc ig;

    随后重启nginx,或重启服务器测试,配置完成。

    基于DirectAdmin的配置

    本段落主要是写给基于DirectAdmin面板的,与本文上述基于军哥lnmp一键包无关。

    如果是基于DirectAdmin面板的话,需要拥有服务器权限,自定义nginx配置文件。一般在各类服务商购买的directadmin网站空间无法实现。

    首先,在DirectAdmin中,自定义nginx编译模块,需要参考:https://help.directadmin.com/item.php?id=545

    但是这篇文章有点问题,如果是php+nginx模式,文章内的 configure/nginx/configure.nginx 应该改为 configure/nginx_reverse/configure.nginx。一个主域名,另外一个用指向就可以。随后要手动修改 /usr/local/directadmin/data/users/user_name/nginx.conf

    配置参考如下:(注:2020-3-23修正第28行地址)

    1. server
    2. {
    3. listen 1.1.1.1:443 ssl http2;
    4. listen 2.2.2.2:443 ssl http2;
    5. server_name maps.your-domain.com mapsapis.your-domain.com;
    6.  
    7. access_log /var/log/nginx/domains/maps.your-domain.com.log;
    8. access_log /var/log/nginx/domains/maps.your-domain.com.bytes bytes;
    9. error_log /var/log/nginx/domains/maps.your-domain.com.error.log;
    10. # root /home/user_name/domains/maps.your-domain.com/private_html;
    11. # index index.php index.html index.htm;
    12. ssl_certificate /usr/local/directadmin/data/users/user_name/domains/maps.your-domain.com.cert.combined;
    13. ssl_certificate_key /usr/local/directadmin/data/users/user_name/domains/maps.your-domain.com.key;
    14. include /usr/local/directadmin/data/users/user_name/nginx_php.conf;
    15.  
    16. location /maps/ {
    17. #MIME TYPE
    18. default_type text/javascript;
    19. proxy_set_header Accept-Encoding '';
    20. proxy_pass https://maps.googleapis.com/maps/;
    21. replace_filter_max_buffered_size 500k;
    22. replace_filter_last_modified keep;
    23. replace_filter_types text/javascript application/javascript;
    24.  
    25. include /usr/local/directadmin/data/users/user_name/replace_cn.txt;
    26. }
    27. location /maps-api-v3/ {
    28. proxy_pass https://maps.googleapis.com/maps-api-v3/;
    29. }
    30.  
    31. include /usr/local/directadmin/data/users/user_name/location_cn.txt;
    32.  
    33. include /etc/nginx/webapps.ssl.conf;
    34. }

    注意一个问题,这个修改是手动的,如果通过DirectAdmin的脚本重写配置后,这个文件会被DA恢复掉。所以注意备份,记得这件事。

    参考资料

    本文参考以下资料,感谢无私分享!

    Nginx反代GoogleAPI,解决谷歌地图图层无法访问的问题 - 掘金 (juejin.cn)

  • 上一篇:OpenWrt怎么连接手机热点?
    下一篇:猫盘群晖内网穿透npc打包全过程