# 微信头像跨域代理
笔者在前端项目中搭建的小游戏最后有海报绘制页面,考虑到团队后端人手不够,选择前端实现。但是使用Canvas组件会遇到明显的跨域问题,即在微信X5浏览器内核无法获取到微信的头像,找了很多办法都没有解决,最终使用nginx跨域代理的方法成功解决。
以下是nginx.conf加的头文件
location ^~ /wechat_image {
add_header 'Access-Control-Allow-Origin' "http://www.buaagsu.com/" always;
add_header 'Access-Control-Allow-Credentials' 'true' always;
add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
proxy_pass http://thirdwx.qlogo.cn/;
}
这段的作用是相当于把 http://thirdwx.qlogo.cn/下的图片代理到服务器下的/wechat_image文件夹,前端只需要访问本地就可以代理获取微信服务器的我头像,从而解决了跨域问题。