最近在折腾一个视频床,考虑到本人过于贫穷的情况下要合理的分配带宽,所以放mp4就显示过于浪费,于是就想到了切片成HLS,可是之后只能在本地播放无法在其他网站上调用,F12查看后发现提示跨域问题。Access to XMLHttpRequest''from origin '' has been blocked by CORS..Access-Control-Allow-Origin。 于是在网上了一下解决方法做个记录。

解决方案

 add_header Access-Control-Allow-Credentials true;
    add_header Access-Control-Allow-Origin $http_origin;
     location /file {
      if ($request_method = 'OPTIONS') {
          add_header Access-Control-Allow-Origin $http_origin;
          add_header Access-Control-Allow-Methods $http_access_control_request_method;
          add_header Access-Control-Allow-Credentials true;
          add_header Access-Control-Allow-Headers $http_access_control_request_headers;
          add_header Access-Control-Max-Age 1728000;
          return 204;
        }        
  }

在网站设置中添加上面这段代码即可