在公司搭建CAS数据服务平台开发环境时,遇到因端口不一致引起的跨域问题,导致图像不显示,记录解决方法。
问题如下所示:
Access to XMLHttpRequest at 'http://xxx.xxx.xx.xxx/download/202112191156538621384/module_3d/Lung.stl?timestamp=1642407488794' from origin 'http://xxx.xxx.xx.xxx:10050' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
问题很明显,请求从端口10050到80,出现跨域访问,浏览器出于安全问题阻止请求,并提示了解决方法,即增加Access-Control-Allow-Origin http头。
解决方法是在静态文件location下增加header:
add_header 'Access-Control-Allow-Origin' '*';
配置后问题得以解决。
备注:
跨域问题:网页访问违反了同源策略,将引起跨域。
同源策略规定,浏览器的ajax只能访问跟它的HTML页面同源(相同域名或IP)的资源。
同源是指协议、域名、端口号都相同,只要有一个不相同,那么都是非同源。
参考跨域 No 'Access-Control-Allow-Origin' header is present on the requested resource._小花生编程-CSDN博客