You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

DigitalOcean上Django(NGINX+Gunicorn)缺失CORS Header问题求助

Fixing CORS Error for DigitalOcean Spaces Resources with Django

Hey there, let's break down why you're still hitting that CORS error even after setting up django-cors-headers!

First, a critical clarification: django-cors-headers only adds CORS headers to responses from your Django backend. The SVG resource you're trying to access is hosted on DigitalOcean Spaces, not your Django server. That means the CORS restriction here is coming from Spaces—your Django CORS settings won't affect this request at all.

Here's how to resolve this:

You need to configure CORS rules directly in your DigitalOcean Spaces bucket:

  1. Log into your DigitalOcean Control Panel and navigate to your target Spaces storage bucket.
  2. Switch to the Settings tab, then locate the CORS Configuration section.
  3. Add a CORS rule that permits requests from your frontend domain. For example, if your frontend runs on https://your-app-domain.com, use this JSON configuration:
[
  {
    "AllowedHeaders": ["*"],
    "AllowedMethods": ["GET"],
    "AllowedOrigins": ["https://your-app-domain.com"],
    "ExposeHeaders": []
  }
]
  • For local development, replace the origin with your frontend's local address (like http://localhost:3000).
  • Skip using "AllowedOrigins": ["*"] in production—it’s insecure, as it opens your resources to any domain.
  1. Save the configuration. It might take 1-2 minutes for changes to propagate, so refresh your frontend and test again.

Quick troubleshooting if it still fails:

  • Double-check that your frontend domain is spelled correctly in AllowedOrigins (don’t forget http:// or https://—they’re case-sensitive!).
  • Ensure AllowedMethods includes GET (since you’re fetching an SVG file).
  • If you have multiple frontend environments (production, staging, local), add all their domains to the AllowedOrigins array (e.g., ["https://your-app.com", "https://staging.your-app.com"]).

To recap: django-cors-headers handles cross-origin requests to your Django API, but resources hosted on external services like DigitalOcean Spaces require CORS configuration directly on that service.

内容的提问来源于stack exchange,提问作者Ishwar Jangid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:05:03