Setting Up Your First CDN: A Hands-On Tutorial
Setting up a CDN involves pointing your domain to a distributed network of edge servers that cache and deliver your content from locations close to your visitors. This tutorial walks through the complete process using Cloudflare as the primary example, then provides parallel instructions for AWS CloudFront and Bunny CDN. If you are new to CDN technology, start with our guide on how CDNs work before diving into configuration.
What Do You Need Before Setting Up a CDN?
Before starting, gather these prerequisites: For foundational context, read What Is a CDN? The Complete Beginner's Guide — FlyFish NWA.
- A working website — Your origin server must be online and serving content at your domain or IP address. Test by accessing your site directly.
- Domain registrar access — You need the ability to change nameservers or add DNS records at your domain registrar (GoDaddy, Namecheap, Cloudflare Registrar, etc.).
- Current DNS records — Export or screenshot your existing DNS records before making changes. You will need to recreate A, AAAA, CNAME, MX, and TXT records in the CDN dashboard.
- SSL certificate status — Know whether your origin server has a valid SSL certificate. This affects the SSL/TLS encryption mode you configure.
- Origin server IP address — The public IP address where your web server responds to HTTP requests.
How Do You Set Up Cloudflare CDN Step by Step?
Cloudflare is the most popular CDN for first-time users because it offers a generous free tier, a straightforward setup wizard, and built-in DDoS protection. The entire process takes 15-30 minutes plus DNS propagation time.
| Metric | Edge Processing | Origin Processing | Improvement |
|---|---|---|---|
| Latency (p50) | 8–15 ms | 120–300 ms | 15–20x faster |
| Cache Hit Ratio | 85–95% | N/A | Reduces origin load |
| TLS Handshake | 1–5 ms (nearby PoP) | 50–150 ms | 10–30x faster |
| Bandwidth Cost | $0.01–0.04/GB | $0.05–0.12/GB | 3–5x cheaper |
| Availability | 99.99% (multi-PoP) | 99.9% (single DC) | 10x fewer outages |
Step 1: Create an Account and Add Your Domain
Sign up at cloudflare.com and click "Add a Site." Enter your domain name (example.com, not www.example.com). Cloudflare automatically scans your existing DNS records and imports them. Select the Free plan to start. Verify the imported DNS records match your current configuration, paying special attention to MX records for email delivery.
Step 2: Change Your Nameservers
Cloudflare assigns two nameservers (e.g., ada.ns.cloudflare.com and bob.ns.cloudflare.com). Log in to your domain registrar and replace the existing nameservers with the Cloudflare pair. This step is critical because it routes all DNS resolution through Cloudflare, enabling the CDN proxy layer. Propagation typically takes 1-4 hours but can take up to 48 hours.
Step 3: Configure SSL/TLS
Navigate to SSL/TLS settings and choose the appropriate encryption mode:
| Mode | Origin SSL Required | Security Level | Best For |
|---|---|---|---|
| Flexible | No | Basic | Sites without origin SSL (temporary only) |
| Full | Self-signed OK | Good | Development environments |
| Full (Strict) | Valid certificate | Best | Production websites (recommended) |
Always aim for Full (Strict) mode in production. Enable "Always Use HTTPS" and "Automatic HTTPS Rewrites" to eliminate mixed content issues.
Step 4: Set Up Cache Rules
The default Cloudflare configuration caches static assets (images, CSS, JS, fonts) but passes HTML through to the origin. For most websites, this default is a solid starting point. Customize caching behavior using cache-control headers from your origin server or Cloudflare Cache Rules:
- Static assets (CSS, JS, images) — Set cache TTL to 1 year with content-hashing file names for instant invalidation.
- HTML pages — Cache for 1-4 hours if content changes infrequently, or bypass cache for dynamic pages.
- API responses — Bypass cache unless you have specific caching strategies for API endpoints.
Step 5: Enable Performance Settings
Under the Speed section, enable these features: Auto Minify for CSS, JavaScript, and HTML; Brotli compression; Early Hints (103 status code); and HTTP/2 prioritization. These settings improve delivery speed beyond basic caching. Refer to the Cloudflare architecture deep dive for advanced tuning options.
How Do You Set Up AWS CloudFront as an Alternative?
AWS CloudFront is the CDN of choice for applications already running on AWS infrastructure. The setup is more involved than Cloudflare but integrates deeply with S3, EC2, and Lambda@Edge.
Distribution Configuration
- Open the CloudFront console and click "Create Distribution."
- Set the origin domain to your server hostname or S3 bucket endpoint.
- Configure the origin protocol policy to "HTTPS Only" if your origin supports SSL.
- Set the viewer protocol policy to "Redirect HTTP to HTTPS."
- Add your custom domain as an alternate domain name (CNAME).
- Request or import an SSL certificate through AWS Certificate Manager (ACM). The certificate must be in the us-east-1 region.
- Configure the default cache behavior with TTL values matching your content update frequency.
DNS Configuration
Unlike Cloudflare, CloudFront uses a CNAME approach. Create a CNAME record at your DNS provider pointing your domain to the CloudFront distribution domain (e.g., d1234abcdef.cloudfront.net). If using Route 53, create an alias record instead, which supports the zone apex (example.com without www).
How Do You Set Up Bunny CDN as a Budget-Friendly Option?
Bunny CDN offers the simplest setup with transparent per-GB pricing and no bandwidth commitments:
- Create a Pull Zone with your origin URL (e.g., https://origin.example.com).
- Test the default CDN URL (yourzone.b-cdn.net) to verify content delivery.
- Add a custom hostname under the Pull Zone settings.
- Create a CNAME record at your DNS provider pointing to yourzone.b-cdn.net.
- Enable the free SSL certificate for your custom hostname.
- Configure cache expiration and Vary headers for proper content negotiation.
Bunny CDN starts at approximately 0.01 per GB for North American and European traffic, making it cost-effective for bandwidth-heavy sites delivering images, video, or software downloads.
How Do You Verify Your CDN Is Working?
After DNS propagation completes, verify CDN functionality through three methods:
Method 1: Response Header Inspection
Open your browser developer tools (F12), navigate to your site, and inspect the response headers of a static asset. Look for these CDN-specific headers:
| CDN Provider | Cache Status Header | HIT Value | MISS Value |
|---|---|---|---|
| Cloudflare | CF-Cache-Status | HIT | MISS (first request) |
| CloudFront | X-Cache | Hit from cloudfront | Miss from cloudfront |
| Bunny CDN | CDN-Cache | HIT | MISS |
Request the same asset twice. The first request should show MISS (fetched from origin), and the second should show HIT (served from edge cache).
Method 2: Multi-Location Performance Testing
Use tools like KeyCDN Performance Test, Dotcom-Tools, or Pingdom to measure TTFB from servers worldwide. Before CDN activation, distant locations might show 300-800ms TTFB. After CDN activation, all locations should show under 100ms for cached content.
Method 3: Google PageSpeed Comparison
Run Google PageSpeed Insights before and after CDN setup. Compare Largest Contentful Paint (LCP), First Contentful Paint (FCP), and Time to First Byte (TTFB) metrics. Expect measurable improvements in all three, directly impacting your Core Web Vitals scores.
What Are the Most Common CDN Setup Mistakes?
After helping hundreds of teams configure their first CDN, these mistakes appear repeatedly:
- Missing origin cache-control headers — The CDN relies on your origin to communicate caching intent. Without explicit headers, caching behavior becomes unpredictable.
- SSL redirect loops — Caused by mismatched SSL settings between CDN and origin. If Cloudflare uses Flexible SSL but the origin forces HTTPS, infinite redirects occur.
- Caching authenticated content — Failing to bypass cache for pages behind login results in one user seeing another user's account data. Always set
Cache-Control: no-storefor authenticated responses. - Forgetting email DNS records — When migrating to Cloudflare nameservers, forgetting to recreate MX, SPF, and DKIM records breaks email delivery.
- Not testing mobile — Desktop verification alone misses issues with Vary headers and mobile-specific content that may be cached incorrectly.
- Ignoring origin IP exposure — After CDN setup, the origin IP may still be discoverable through DNS history tools. Configure your origin firewall to accept connections only from CDN IP ranges.
What Should You Do After Your CDN Is Running?
Once the basic setup is verified, take these next steps to maximize CDN value:
- Monitor cache hit ratio — Aim for 85%+ for static-heavy sites. Check your CDN analytics dashboard weekly during the first month.
- Enable WAF rules — Activate the web application firewall to block common attack patterns (SQL injection, XSS, directory traversal).
- Configure page-specific rules — Create rules that bypass cache for admin pages, API endpoints, and checkout flows.
- Set up monitoring alerts — Configure alerts for cache hit ratio drops, origin error spikes, and certificate expiration warnings.
- Document your configuration — Record all custom rules, DNS changes, and SSL settings for your team. CDN configurations are notoriously difficult to audit months later.
With a properly configured CDN in place, you can explore advanced optimization techniques including image optimization, HTTP/3 and QUIC protocols, and edge computing capabilities that transform your CDN from a simple cache into a powerful application delivery platform.