[Nginx] 解决跨域been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

当需要跨域请求接口会出现

been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

可以在代码里解决 , 也可以直接在nginx里解决

类似GOFLY部署在nginx下的代码

加上header头就可以了

                add_header Access-Control-Allow-Origin *;
                add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';

 

server{
       listen 80;
        server_name  gofly.sopans.com;
        access_log  /var/log/nginx/gofly.sopans.com.access.log  main;
        location /static {
                root /var/www/html/go-fly;//自己的部署路径
        }        
        location / {
                add_header Access-Control-Allow-Origin *;
                add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
                proxy_pass http://127.0.0.1:8081;
                    proxy_http_version 1.1;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection "upgrade";
                    proxy_set_header Origin "";
        }
}

 

posted @ 2021-03-01 16:09  唯一客服系统开发笔记  阅读(7071)  评论(0编辑  收藏  举报