Django digitalocean Spaces所请求的资源上没有“ Access-Control-Allow-Origin”标头

时间:2018-09-21 18:33:16

标签: django nginx cors digital-ocean django-cors-headers

我的Django网站通过Nginx托管在digitalocean ubuntu 16.04中。

我已经在这里设置了digitalocean CORS配置https://www.digitalocean.com/docs/spaces/how-to/cors/,在“起源”中添加了自己的domian名称,选中了“允许的方法”中的所有选项,唯一要做的就是在“取消标题,因为我不知道要添加什么。

当我使用digitalocean Spaces(一种基于服务且与Amazon S3非常相似的服务)作为我的静态和媒体文件存储时。所请求的资源上没有'Access-Control-Allow-Origin'标头错误:

SelectedValProperty

我尝试使用django-cors-headers,但收到了:

Access to Font at 'https://nyc3.digitaloceanspaces.com/kjmgstorage/kjmgstorage/fonts/fontawesome-webfont.woff2?v=4.7.0' from origin 'https://kjmg.co' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://kjmg.co' is therefore not allowed access.

因此必须将其卸载。

任何朋友有任何想法吗?非常感谢!

4 个答案:

答案 0 :(得分:1)

尽管如此,我还是遇到了一个老问题,因为DO文档不完整,我花了一些时间来解决。我的2美分是...按照DOs文档(https://www.digitalocean.com/docs/spaces/how-to/configure-cors/)所述配置CORS。完成后,转到“空间”设置并清除缓存。这是文档中缺少的步骤。在您清除缓存之前,配置不会生效。

答案 1 :(得分:1)

这是一个很老的问题。但是回答它会导致有人发现它有用。

我也遇到过同样的问题。首先,如果您正在使用DO(数字海洋)空间,则需要将CORS标头选项设置为DO空间,而不是在django应用中。

您需要打开DO空间的设置并转到CORS Configuration

然后在“允许的标题”部分添加Access-Control-Allow-Origin

然后将Access Control Max Age的值设置为600秒,这意味着浏览器将在10分钟内缓存CORS标头信息。

如果使用CDN,请单击设置中的purge cache按钮,最后不要忘记从DO spcae的边缘服务器清除缓存,否则您将像我一样浪费很多时间。

答案 2 :(得分:0)

您需要在Digital ocean而不是在网站上设置CORS。检查以下内容:digitalocean.com/docs/spaces/how-to/cors

答案 3 :(得分:0)

你必须确保:

  • 设置跨域允许配置空间中的所有内容,例如:

    • 起源:*
    • 允许的方法:GET、PUT
    • 高级选项:标题:*
  • 在客户端需要注入头:'x-amz-acl':'public-read'

$.ajax({
    url: data.response.pre_sign_url,
    type: 'PUT',
    headers: {
      'x-amz-acl': 'public-read'
    },
    contentType: input.files[0].type,
    cache : false,
    processData: false,
    data: input.files[0],
    success: function(response) {
      console.log(response)
    },
  });