Nginx反向代理豆瓣API接口为小程序提供数据

因为近来豆瓣的开发者平台已经无限期的下线,而且由于很多小程序直接请求豆瓣的接口,导致了豆瓣做了防盗链的,小程序完全不能打开。

但是服务器端用curl发现是可以访问,那么我们不如自己做一个反向代理,为我们自己的小程序提供一个中转站。下面不BB了,直接贴代码

Nginx端:【因为小程序必须要用https协议,建议小伙伴自己申请一个免费的,华为云,阿里云,腾讯云都提供】

server {
        listen       443;
        server_name  www.example.com;
        ssl on;
        index index.html index.htm index.php;
        ssl_certificate   /aliyun/server/nginx/cert/example.com/www/server.crt;
        ssl_certificate_key  /aliyun/server/nginx/cert/example.com/www/server.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

          location / {
                proxy_store off;
				proxy_redirect off;
				proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
				proxy_set_header X-Real-IP $remote_addr;
				proxy_set_header Referer 'no-referrer-when-downgrade';
				proxy_set_header User-Agent 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36';
				proxy_connect_timeout 600;
				proxy_read_timeout 600;
				proxy_send_timeout 600;
                proxy_pass  https://api.douban.com;
          }
}

小程序前端的坑:【 'content-type': 'application/json'  已经不支持了,建议换成 'Content-Type': 'json' 不然会报400的错误】

    wx.request({
      url: 'https://www.example.com/v2/movie/top250?start=0&count=2',
      data: {},
      method: 'get',
      header: {
        // 'content-type': 'application/json'
        'Content-Type': 'json'
      },
      success: function (res) {
        console.log(res.data)
      }
    })

 

转载于:https://my.oschina.net/jishuge/blog/2980810

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值