解决谷歌地图Google Maps Api失效:NGINX代理配置全过程
本文主要介绍一种快速搭建Google maps api代理的方法,用以解决国内无法使用谷歌地图接口的问题。本文核心内容来自:GitHub: ijialong1314/googlemap-proxy
背景
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商家测试通过。
搭建流程
只写命令行,具体看注释
# 安装lnmp 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 # 安装nginx相关包 yum install bison git git clone https://github.com/agentzh/sregex cd sregex make && make install cd /usr/local/nginx/ mkdir modules && cd modules git clone https://github.com/agentzh/replace-filter-nginx-module cd /root/lnmp1.6 vim lnmp.conf # 修改这一行,添加: Nginx_Modules_Options="--add-module=/usr/local/nginx/modules/replace-filter-nginx-module --with-ld-opt='-Wl,-rpath,/usr/local/lib'" # 重新编译nginx 1.16.1 ./upgrade.sh nginx # 添加主机,带ssl lnmp vhost add # 修改虚拟主机nginx配置文件 cd /usr/local/nginx/conf/vhost/ vi {maps.your-domain.com}.conf
文件内容,注意,证书那部分原来的不要改动,其余的位置照着写:(注:2020-3-23修正第28行地址)
server { listen 443 ssl http2; #listen [::]:443 ssl http2; server_name maps.your-domain.com mapsapis.your-domain.com; ssl_certificate /usr/local/nginx/conf/ssl/maps.your-domain.com/fullchain.cer; ssl_certificate_key /usr/local/nginx/conf/ssl/maps.your-domain.com/maps.your-domain.com.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; 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"; ssl_session_cache builtin:1000 shared:SSL:10m; # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048 ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem; #error_page 404 /404.html; access_log /home/wwwlogs/maps.your-domain.com.log; location /maps/ { #MIME TYPE default_type text/javascript; proxy_set_header Accept-Encoding ''; proxy_pass https://maps.googleapis.com/maps/; replace_filter_max_buffered_size 500k; replace_filter_last_modified keep; replace_filter_types text/javascript application/javascript; include vhost/replace_cn.txt; } location /maps-api-v3/ { proxy_pass https://maps.googleapis.com/maps-api-v3/; } include vhost/location_cn.txt; }
新建location_cn.txt (注:2020-3-23修正第13行地址)
location /e173754b3b1bbd62a0be9c4afea73fdf/ { proxy_pass https://mts0.google.com/; } location /ce60bcadc66cea58583ddd700f7d80be/ { proxy_pass https://mts1.google.com/; } location /e6ff3de47e3134794d4442b83c841f20/ { proxy_pass https://khms0.google.com/; } location /da42f38f94ce6ac80e2806122a7b1933/ { proxy_pass https://khms1.google.com/; } location /7abac8c11716f6949e7b23f76e60fcd0/ { proxy_pass https://khms0.googleapis.com/; } location /444c94a4157de3c06c435132eb2f1ac5/ { proxy_pass https://khms1.googleapis.com/; } location /c7d52b85d86a06df50621e669557ea05/ { proxy_pass https://mts0.googleapis.com/; } location /57cf283b871304a296c3bd8acde4cc22/ { proxy_pass https://mts1.googleapis.com/; } location /3e0ae61058d4e7be83d222fb1f107310/ { proxy_pass https://maps.gstatic.com/; } location /ae5102db1431e3fd01dc8336085d150f/ { proxy_pass https://csi.gstatic.com/; } location /946eb25413c43b235b5806e999044125/ { proxy_pass https://maps.google.com/; } location /dcd331573c0980eab6fe7346468e9974/ { proxy_pass https://gg.google.com/; } location /0a68229ef53c18f5fc37f4106f09a6c9/ { proxy_pass https://khms.google.com/; } location /e55bb777699a599b09d3011043439a06/ { proxy_pass https://earthbuilder.googleapis.com/; } location /471b015572d3907c09789af41061e964/ { proxy_pass https://mts.googleapis.com/; } location /3a4544842f4e8e90a152257489cb594d/ { proxy_pass https://static.panoramio.com.storage.googleapis.com/; } location /85471b91952baa51514c7fad7c57b8eb/ { proxy_pass https://lh3.ggpht.com/; } location /44b3a55364b2f63e51f2c6e911f8506f/ { proxy_pass https://lh4.ggpht.com/; } location /3cc4fbda8f98767ab275af32669f56d4/ { proxy_pass https://lh5.ggpht.com/; } location /2815063fda2dd0ccf9c566d1f089d4bc/ { proxy_pass https://lh6.ggpht.com/; } location /468724d08bce9811e989b526c6222863/ { proxy_pass https://www.google.com/; }
新建replace_cn.txt,务必注意替换其中的两个域名,包括maps和mapapis (注:2020-3-23修正第15行地址)
replace_filter mts0.google.com maps.your-domain.com/e173754b3b1bbd62a0be9c4afea73fdf ig; replace_filter mts1.google.com maps.your-domain.com/ce60bcadc66cea58583ddd700f7d80be ig; replace_filter khms0.google.com maps.your-domain.com/e6ff3de47e3134794d4442b83c841f20 ig; replace_filter khms1.google.com maps.your-domain.com/da42f38f94ce6ac80e2806122a7b1933 ig; replace_filter khms0.googleapis.com maps.your-domain.com/7abac8c11716f6949e7b23f76e60fcd0 ig; replace_filter khms1.googleapis.com maps.your-domain.com/444c94a4157de3c06c435132eb2f1ac5 ig; replace_filter mts0.googleapis.com maps.your-domain.com/c7d52b85d86a06df50621e669557ea05 ig; replace_filter mts1.googleapis.com maps.your-domain.com/57cf283b871304a296c3bd8acde4cc22 ig; replace_filter maps.gstatic.com maps.your-domain.com/3e0ae61058d4e7be83d222fb1f107310 ig; replace_filter csi.gstatic.com maps.your-domain.com/ae5102db1431e3fd01dc8336085d150f ig; replace_filter maps.googleapis.com mapsapis.your-domain.com ig; replace_filter maps.google.com maps.your-domain.com/946eb25413c43b235b5806e999044125 ig; replace_filter gg.google.com maps.your-domain.com/dcd331573c0980eab6fe7346468e9974 ig; replace_filter www.google.com maps.your-domain.com/468724d08bce9811e989b526c6222863 ig; replace_filter khms.google.com maps.your-domain.com/0a68229ef53c18f5fc37f4106f09a6c9 ig; replace_filter earthbuilder.googleapis.com maps.your-domain.com/e55bb777699a599b09d3011043439a06 ig; replace_filter mts.googleapis.com maps.your-domain.com/471b015572d3907c09789af41061e964 ig; replace_filter static.panoramio.com.storage.googleapis.com maps.your-domain.com/3a4544842f4e8e90a152257489cb594d ig; replace_filter lh3.ggpht.com maps.your-domain.com/85471b91952baa51514c7fad7c57b8eb ig; replace_filter lh4.ggpht.com maps.your-domain.com/44b3a55364b2f63e51f2c6e911f8506f ig; replace_filter lh5.ggpht.com maps.your-domain.com/3cc4fbda8f98767ab275af32669f56d4 ig; 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行地址)
server { listen 1.1.1.1:443 ssl http2; listen 2.2.2.2:443 ssl http2; server_name maps.your-domain.com mapsapis.your-domain.com; access_log /var/log/nginx/domains/maps.your-domain.com.log; access_log /var/log/nginx/domains/maps.your-domain.com.bytes bytes; error_log /var/log/nginx/domains/maps.your-domain.com.error.log; # root /home/user_name/domains/maps.your-domain.com/private_html; # index index.php index.html index.htm; ssl_certificate /usr/local/directadmin/data/users/user_name/domains/maps.your-domain.com.cert.combined; ssl_certificate_key /usr/local/directadmin/data/users/user_name/domains/maps.your-domain.com.key; include /usr/local/directadmin/data/users/user_name/nginx_php.conf; location /maps/ { #MIME TYPE default_type text/javascript; proxy_set_header Accept-Encoding ''; proxy_pass https://maps.googleapis.com/maps/; replace_filter_max_buffered_size 500k; replace_filter_last_modified keep; replace_filter_types text/javascript application/javascript; include /usr/local/directadmin/data/users/user_name/replace_cn.txt; } location /maps-api-v3/ { proxy_pass https://maps.googleapis.com/maps-api-v3/; } include /usr/local/directadmin/data/users/user_name/location_cn.txt; include /etc/nginx/webapps.ssl.conf; }
注意一个问题,这个修改是手动的,如果通过DirectAdmin的脚本重写配置后,这个文件会被DA恢复掉。所以注意备份,记得这件事。
参考资料
本文参考以下资料,感谢无私分享!