Nginx反代GoogleAPI,解决谷歌地图图层无法访问的问题

Nginx反代GoogleAPI,解决谷歌地图图层无法访问的问题

技术杂谈小彩虹2021-08-19 22:35:47150A+A-

1.0. 前言:

由于公司业务需求,需要使用高德地图api + 谷歌图层覆盖,但是由于在2021.1.11号之前所有国内可以访问谷歌图层的链接全部失效,所以决定使用 阿里云香港服务器 + nginx 做一下反代,保证图层可以获取到。

具体参考文章 高德地图 API 叠加谷歌地图图层高德地图加载谷歌地图瓦片反代谷歌APINginx反向代理Google

1.1 选择的是阿里云香港服务器

​ 测试的时候买的是1C1G1M的服务器.. 所以最后在获取图层贴图的时候 比较慢..

1.2 安装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

1.3 安装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'"

重新编译

./upgrade.sh nginx		# nginx 1.16.1,会提示编译版本,输入1.16.1即可

添加主机,带ssl

lnmp vhost add

会提示输入域名:比如maps.{maps.your-domain.com你的域名}.com,其他一堆配置我都直接回车默认跳过了

修改虚拟主机nginx配置文件

cd /usr/local/nginx/conf/vhost/
vi {maps.your-domain.com你的域名}.conf

1.4 配置https

因为官网使用了 htpps,所以这里必须使用https的配置, 以我们的

server
    {   
       listen 443 ssl http2;
       #listen [::]:443 ssl http2;
       server_name {maps.your-domain.com 你的域名};

       ssl_certificate /usr/local/nginx/conf/ssl/{maps.your-domain.com你的域名}/xxxx.pem;  #将domain name.pem替换成您证书的文件名称。 这里可以参考自己服务器的https配置, 只需要把证书文件上传到对应位置就可以了。
       ssl_certificate_key /usr/local/nginx/conf/ssl/{maps.your-domain.com你的域名}/xxxx.key; #将domain name.key替换成您证书的密钥文件名称。
       ssl_session_timeout 5m;
       ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。
       ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。
       ssl_prefer_server_ciphers on;

       #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;

    }

1.5 http配置

如果是http则需要这样配置

server
    {
        listen 80;
        #listen [::]:80;
        server_name 你的域名.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /home/wwwroot/你的域名.com;

        access_log  /home/wwwlogs/你的域名.log;

        location /maps/ {

            #MIME TYPE
            default_type text/javascript;

            proxy_set_header Accept-Encoding '';
			# 这里注意是 http而不是https
            proxy_pass http://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  http://maps.googleapis.com/maps-api-v3/;
        }

        include vhost/location_cn.txt;

    }

1.6 添加指定文件

vi location_cn.txt

如下内容 基本不用动

location /e173754b3b1bbd62a0be9c4afea73fdfa/ { proxy_pass https://mts0.google.com/; }
location /ce60bcadc66cea58583ddd700f7d80bea/ { proxy_pass https://mts1.google.com/; }
location /e6ff3de47e3134794d4442b83c841f20a/ { proxy_pass https://khms0.google.com/; }
location /da42f38f94ce6ac80e2806122a7b1933a/ { proxy_pass https://khms1.google.com/; }
location /7abac8c11716f6949e7b23f76e60fcd0a/ { proxy_pass https://khms0.googleapis.com/; }
location /444c94a4157de3c06c435132eb2f1ac5a/ { proxy_pass https://khms1.googleapis.com/; }
location /c7d52b85d86a06df50621e669557ea05a/ { proxy_pass https://mts0.googleapis.com/; }
location /57cf283b871304a296c3bd8acde4cc22a/ { proxy_pass https://mts1.googleapis.com/; }
location /3e0ae61058d4e7be83d222fb1f107310a/ { proxy_pass https://maps.gstatic.com/; }
location /ae5102db1431e3fd01dc8336085d150fa/ { proxy_pass https://csi.gstatic.com/; }
location /946eb25413c43b235b5806e999044125a/ { proxy_pass https://maps.google.com/; }
location /dcd331573c0980eab6fe7346468e9974a/ { proxy_pass https://gg.google.com/; }
location /0a68229ef53c18f5fc37f4106f09a6c9a/ { proxy_pass https://khms.google.com/; }
location /e55bb777699a599b09d3011043439a06a/ { proxy_pass https://earthbuilder.googleapis.com/; }
location /471b015572d3907c09789af41061e964a/ { proxy_pass https://mts.googleapis.com/; }
location /3a4544842f4e8e90a152257489cb594da/ { proxy_pass https://static.panoramio.com.storage.googleapis.com/; }
location /85471b91952baa51514c7fad7c57b8eba/ { proxy_pass https://lh3.ggpht.com/; }
location /44b3a55364b2f63e51f2c6e911f8506fa/ { proxy_pass https://lh4.ggpht.com/; }
location /3cc4fbda8f98767ab275af32669f56d4a/ { proxy_pass https://lh5.ggpht.com/; }
location /2815063fda2dd0ccf9c566d1f089d4bca/ { proxy_pass https://lh6.ggpht.com/; }
location /468724d08bce9811e989b526c6222863a/ { proxy_pass https://www.google.com/; }

然后是反代域名映射,这里需要修改成你的域名

vi replace_cn.txt
replace_filter mts0.google.com maps.your-domain.com/e173754b3b1bbd62a0be9c4afea73fdfa ig;
replace_filter mts1.google.com maps.your-domain.com/ce60bcadc66cea58583ddd700f7d80bea ig;
replace_filter khms0.google.com maps.your-domain.com/e6ff3de47e3134794d4442b83c841f20a ig;
replace_filter khms1.google.com maps.your-domain.com/da42f38f94ce6ac80e2806122a7b1933a ig;
replace_filter khms0.googleapis.com maps.your-domain.com/7abac8c11716f6949e7b23f76e60fcd0a ig;
replace_filter khms1.googleapis.com maps.your-domain.com/444c94a4157de3c06c435132eb2f1ac5a ig;
replace_filter mts0.googleapis.com maps.your-domain.com/c7d52b85d86a06df50621e669557ea05a ig;
replace_filter mts1.googleapis.com maps.your-domain.com/57cf283b871304a296c3bd8acde4cc22a ig;
replace_filter maps.gstatic.com maps.your-domain.com/3e0ae61058d4e7be83d222fb1f107310a ig;
replace_filter csi.gstatic.com maps.your-domain.com/ae5102db1431e3fd01dc8336085d150fa ig;
replace_filter maps.googleapis.com maps.your-domain.com ig;
replace_filter maps.google.com maps.your-domain.com/946eb25413c43b235b5806e999044125a ig;
replace_filter gg.google.com maps.your-domain.com/dcd331573c0980eab6fe7346468e9974a ig;
replace_filter www.google.com maps.your-domain.com/468724d08bce9811e989b526c6222863a ig;
replace_filter khms.google.com maps.your-domain.com/0a68229ef53c18f5fc37f4106f09a6c9a ig;
replace_filter earthbuilder.googleapis.com maps.your-domain.com/e55bb777699a599b09d3011043439a06a ig;
replace_filter mts.googleapis.com maps.your-domain.com/471b015572d3907c09789af41061e964a ig;
replace_filter static.panoramio.com.storage.googleapis.com maps.your-domain.com/3a4544842f4e8e90a152257489cb594da ig;
replace_filter lh3.ggpht.com maps.your-domain.com/85471b91952baa51514c7fad7c57b8eba ig;
replace_filter lh4.ggpht.com maps.your-domain.com/44b3a55364b2f63e51f2c6e911f8506fa ig;
replace_filter lh5.ggpht.com maps.your-domain.com/3cc4fbda8f98767ab275af32669f56d4a ig;
replace_filter lh6.ggpht.com maps.your-domain.com/2815063fda2dd0ccf9c566d1f089d4bca ig;

然后就可以重启Nginx了,不过好像这里直接restart会出错,至少我这里一直报错,解决不了

systemctl restart nginx

解决办法是 重启了一下服务器......

1.7 测试

通过上面搭建完反向代理后测试结果 图层可以正常访问了..

PS: 因为上面配置的时候,采用的是 这两个路径反代 mts0.google.com 路径下资源 所以在使用的时候,需要通过

{maps.your-domain.com你的域名} /e173754b3b1bbd62a0be9c4afea73fdfa/vt/lyrs=s@142&hl=zh-CN&gl=cn&x=6759&y=3209&z=13&s=Galil

这种方式获取资源.. 其他同理。

点击这里复制本文地址 以上内容由权冠洲的博客整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

支持Ctrl+Enter提交

联系我们| 本站介绍| 留言建议 | 交换友链 | 域名展示
本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除

权冠洲的博客 © All Rights Reserved.  Copyright quanguanzhou.top All Rights Reserved
苏公网安备 32030302000848号   苏ICP备20033101号-1
本网站由 提供CDN/云存储服务

联系我们