记阿里云SLB后配置Nginx反向代理百度地图API的坑

需求node

百度的原始请求:
https://api.map.baidu.com/place/v2/suggestion?query=s&region=sc&city_limit=true&output=json&ak=dl0tuE2WhjbHyqKcHevxXRYnLjV53OZmnginx

代理为:web

https://api.example.com/proxy/baidu/suggestion?query=s&region=sc&city_limit=true&output=json&ak=dl0tuE2WhjbHyqKcHevxXRYnLjV53OZm
docker

顺序json

 

Client –》 SLB –》 Nginx-Proxy –>baidu.com api   -阿里云环境api

Clinet –>Nginx  -> Nginx-Proxy –>baidu.com api  - 测试环境app

问题dom

在自建环境中,没有任何问题,可是发布到阿里云SLB(service load balance)以后,竟然百度端会报
参数无效。tcp

 

咱们是采用Bitbucket来管理Nginx-proxy, 因为采用的是Docker执行,总体上却是很方便,就是相比直接部署来讲,每次发布时间要长一些。(因为是全自动化,却是能够喝个水,溜下单身狗什么的)工具

image

 

# in QA, it works well with SLB
    location ^~/proxy/baidu/ {
            #support cross-domain visit for app
           
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
            #allow credentials
            add_header Access-Control-Allow-Credentials true;
            #allow-headers
            add_header 'access-control-allow-headers' 'Authentication,Origin,X-Requested-With,Content-Type,Accept,token,appId,unitId';
            
            #alias //;
       #     proxy_set_header X-Real-IP $remote_addr;
        #    proxy_set_header REMOTE-HOST $remote_addr;
         #   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      
            proxy_pass http://api.map.baidu.com/place/v2/;
            #proxy_redirect https://api.map.baidu.com/place/v2// /;
      
            #proxy_pass http://node-http/log/testGet/;
            #proxy_redirect http://node-http/log/testGet// /;
    }
检查过程

因为采用docker部署方式,理论上说都是同样的。惟一可能的是:SLB那边转发作了必定的改动。所以,采用tcpdump来抓包,比较两个环境下的转发请求的不一样

其实,我最开始的时候,采用的是Node-http来得到Nginx-Proxy 请求

Client –》 SLB –》 Nginx-Proxy –>baidu.com api   -阿里云环境 改成

Client –》 SLB –》 Nginx-Proxy –>Node Http

经过捕获Http请求,来比较两个环境下请求有哪些不一样。其实两种工具均可以: Node Http 或者采用 Tcpdump 来得到请求。

 

比较结果:
image

发现过程就是用firefox的web Tool不停的编辑header头,作测试了,直到。。。

最终发现是:因为在配置SLB的时候增长了一个header形成的,去掉就能够了。

X-Forwarded-Proto: https\r\n

在此期间分别向阿里云和百度提交了工单,阿里云的工程师回复比较积极,百度就呵呵了。有可能用的百度地图服务是免费的,所以人家呵呵。

后面补充一点

因为在Nginx-Proxy docker中即要启动Nginx,同时,又须要启动tcpdump,所以须要安装了s6-svscan 服务驻守程序。

我简单把dockerfile内容供参考吧

FROM nginx:1.12.1-alpine

RUN apk update 
RUN apk add tcpdump
RUN apk add s6


COPY ./services /etc/s6/services
RUN chmod +x  /etc/s6/services/nginx/run
RUN chmod +x  /etc/s6/services/nginx/finish

RUN chmod +x  /etc/s6/services/tcpdump/run
RUN chmod +x  /etc/s6/services/tcpdump/finish



CMD ["s6-svscan", "/etc/s6/services"]

注意要本身编写 services目录下面的服务启动文件 run和关闭文件finish,具体的你们google一下吧(为何不百度,由于它呵呵我。)

可以关掉任务很开心

 

image

相关文章
相关标签/搜索