當瀏覽器發起ajax請求到其餘域名或者訪問其餘域名m3u8資源時,會出現跨域的問題,致使沒法正確訪問資源提示:css
No'Access-Control-Allow-Origin' header is present on the requested resourse.Origin 'http//localhost:8088' is therefore not allowed access.
在nginx上的解決方案是配置Access-Control-Allow-Origin來解決,可是此參數只容許配置單個域名「add_header Access-Control-Allow-Origin baidu.com」或者「add_header Access-Control-Allow-Origin *;」容許所有域名,當咱們須要容許多個域名跨域訪問時卻很差配置,能夠用map來實現,具體代碼以下:html
第一種方式:nginx
map $http_origin $corsHost { default 0; "~http://www.haibakeji.com" http://www.haibakeji.com; "~http://m.haibakeji.com" http://m.haibakeji.com; "~http://wap.haibakeji.com" http://wap.haibakeji.com; } server { listen 80; server_name www.haibakeji.com; root /nginx; location / { add_header Access-Control-Allow-Origin $corsHost; } }
原文網址:https://www.haibakeji.com/archives/249.htmlajax
第二種方式:segmentfault
location ~* \.(ttf|ttc|otf|eot|woff|font.css)$ { if ($http_origin = 'http://backend.test.com') { add_header 'Access-Control-Allow-Origin' "$http_origin"; } if ($http_origin = 'http://wap.test.com') { add_header 'Access-Control-Allow-Origin' "$http_origin"; } } 原文地址:https://www.jianshu.com/p/f2ec1d6af047
一個詳細的說明網站:https://segmentfault.com/a/1190000003710973跨域