Serving DigitalOcean Spaces files from a custom domain using a CDN is surprisingly straightforward once you understand how the pieces interact.
Let’s watch it happen. Imagine we have some static assets – images, CSS, JavaScript – stored in a DigitalOcean Space named my-cool-bucket. We want these files to be accessible not at my-cool-bucket.nyc3.digitaloceanspaces.com/my-image.jpg, but at cdn.mydomain.com/my-image.jpg.
First, you need a CDN. DigitalOcean has its own CDN product, which integrates directly with Spaces. Alternatively, you can use Cloudflare, Akamai, or any other CDN provider. For this example, we’ll use DigitalOcean’s CDN.
1. Create a DigitalOcean Space:
If you haven’t already, create a Space. Let’s say you create my-cool-bucket in the nyc3 region.
2. Configure CDN for the Space:
In the DigitalOcean control panel, navigate to Networking -> CDN.
Click Add a CDN endpoint.
For Origin, select Spaces.
For Bucket, choose my-cool-bucket.
For Edge Cache TTL, set it to 1 hour. This means the CDN will cache your files for an hour at its edge locations.
For Subdomain, enter cdn.mydomain.com. This is the custom domain you want to use.
Click Create endpoint.
DigitalOcean will provision the CDN endpoint. This process can take a few minutes. Once provisioned, you’ll see an entry for cdn.mydomain.com pointing to your Space.
3. Update DNS Records:
Now, you need to tell the internet that cdn.mydomain.com should actually point to the CDN.
Go to your DNS provider (this could be DigitalOcean itself, GoDaddy, Namecheap, etc.).
Add a CNAME record:
- Hostname/Name:
cdn(orcdn.mydomain.comif your provider requires the full domain) - Value/Target: The endpoint URL provided by DigitalOcean for your CDN, which will look something like
your-bucket-name.nyc3.cdn.digitaloceanspaces.com.
Wait for DNS propagation. This can take anywhere from a few minutes to 48 hours, but usually, it’s much faster.
4. Upload Files and Test:
Upload a file to your Space, for example, my-image.jpg.
Access it via the CDN custom domain: https://cdn.mydomain.com/my-image.jpg.
You should see your image. The first request might be slightly slower as the CDN fetches it from the Space and caches it. Subsequent requests from users geographically closer to a CDN edge location will be served much faster.
The mental model is this: When a user requests https://cdn.mydomain.com/my-image.jpg, their request hits the DNS. DNS resolves cdn.mydomain.com to the CDN’s IP address. The CDN, recognizing it’s serving content for cdn.mydomain.com, checks its cache for my-image.jpg. If it’s there and not expired, it serves the file directly. If not, it goes to the origin (your DigitalOcean Space), fetches my-image.jpg, serves it to the user, and caches it for future requests.
The key levers you control are:
- Origin: Where the CDN fetches the original files (your Space).
- Custom Domain: The user-friendly URL you present.
- Edge Cache TTL: How long the CDN keeps files at its edge locations before checking the origin again. A longer TTL means faster delivery for repeat requests but a longer delay if you update a file.
- DNS Records: Crucially, linking your custom domain to the CDN endpoint.
The one thing most people don’t immediately grasp is that the CDN endpoint URL provided by DigitalOcean (e.g., my-cool-bucket.nyc3.cdn.digitaloceanspaces.com) is not what you point your CNAME record to directly if you’re using a custom domain. Instead, your CNAME record points your custom domain (e.g., cdn.mydomain.com) to that CDN endpoint URL. The CDN service itself is configured to know that requests for cdn.mydomain.com should be handled by fetching from my-cool-bucket.nyc3.digitaloceanspaces.com.
Once this is set up, you can upload new files to your Space, and they will become available on your CDN custom domain after the CDN’s cache TTL expires or if the CDN purges the cache.
The next step is usually configuring cache invalidation or understanding how to handle dynamic content with a CDN.