我有一个跨域图像的问题,希望你可以帮忙解决。
这里是具体情况。我有两个域名,例如: - domain1.com - domain2.com
在domain1上,我放了很多html5游戏。这个域名只是游戏的存储库。
Domain2是真正的网站(wordpress网站),用户可以在其上玩托管在domain1上的游戏。为此,我为每个游戏进行了curl请求。
在domain1的nginx配置文件中,我加入了以下代码以启用跨域资源共享:
location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|json|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf|swf|mp3|xml|woff2)$ { add_header "Access-Control-Allow-Origin" "*"; access_log off; log_not_found off; expires max; }
这解决了许多游戏的问题,但仍有一些游戏无法工作,我收到以下js错误:
Uncaught DOMException: Failed to execute 'texImage2D' on 'WebGLRenderingContext': The cross-origin image at http://domain1.com/html5-games/action/candy-match/images/loadingbarbackground-sheet0.png may not be loaded. at GLWrap_.loadTexture (http://domain1.com/html5-games/action/candy-match/c2runtime.js:2618:16) at pluginProto.Type.typeProto.loadTextures (http://domain1.com/html5-games/action/candy-match/c2runtime.js:18070:46) at pluginProto.Instance.instanceProto.onCreate (http://domain1.com/html5-games/action/candy-match/c2runtime.js:18146:13) at Runtime.createInstanceFromInit (http://domain1.com/html5-games/action/candy-match/c2runtime.js:4806:8) at Layer.createInitialInstances (http://domain1.com/html5-games/action/candy-match/c2runtime.js:7541:25) at Layout.startRunning (http://domain1.com/html5-games/action/candy-match/c2runtime.js:6715:10) at Runtime.go_loading_finished (http://domain1.com/html5-games/action/candy-match/c2runtime.js:4067:36) at Runtime.go (http://domain1.com/html5-games/action/candy-match/c2runtime.js:3966:9) at http://domain1.com/html5-games/action/candy-match/c2runtime.js:4025:60
我在线上进行了一些研究,发现了像这样的文章:
https://webglfundamentals.org/webgl/lessons/webgl-cors-permission.html Drawing images to canvas with img.crossOrigin = "Anonymous" doesn't work但它们并不是很有帮助。
如果可能的话,我不想修改原始游戏文件。我正在寻找服务器端解决方案。如果没有,你有什么解决我的问题的想法吗?
我的配置中是否存在错误?我是否遗漏了某些内容?
感谢您的帮助。
瓦莱里奥