1. 领加网络的博客首页
  2. 服务器

通过Nginx代理机制显示微信头像和二维码

微信头像和某些通过微信api生成的二维码无法正确显示到网站

通过Nginx代理机制显示微信头像和二维码

一般用两种方法

1.直接下载到本地或者下载到本地后再上传到三方存储…我是拒绝的

2.选择更优雅的方法,通过nginx的代理机制,完美解决,一劳永逸,简单高效,附上代码

location ^~ /wechat_image/ {
        add_header 'Access-Control-Allow-Origin' "$http_origin" 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/;
}

  location ^~ /wechat_qrcode/ {
        add_header 'Access-Control-Allow-Origin' "$http_origin" 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 https://mp.weixin.qq.com/;
}

亲测,没任何问题。之前做的一个生成带有微信头像和参数二维码海报的项目,如果不是本地图片,浏览器直接报跨域错误,后来采用这种方法,非常简单。

原创文章,作者:代码加工厂,如若转载,请注明出处:http://blog.lingj.net/2020/12/16/199.html

发表评论

邮箱地址不会被公开。 必填项已用*标注