Nginx反向代理豆瓣API接口爲小程序提供數據

由於近來豆瓣的開發者平臺已經無限期的下線,並且因爲不少小程序直接請求豆瓣的接口,致使了豆瓣作了防盜鏈的,小程序徹底不能打開。php

可是服務器端用curl發現是能夠訪問,那麼咱們不如本身作一個反向代理,爲咱們本身的小程序提供一箇中轉站。下面不BB了,直接貼代碼html

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的錯誤】nginx

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)
      }
    })
相關文章
相關標籤/搜索