Docs / Performance Optimization / CDN Setup with Cloudflare for Your VPS

CDN Setup with Cloudflare for Your VPS

By Admin · Mar 30, 2026 · Updated Apr 23, 2026 · 667 views · 2 min read

What Does a CDN Do?

A Content Delivery Network caches your static assets at edge servers worldwide, reducing latency and server load.

Without CDN:  User (Tokyo) → Your server (New York) → Response (200ms)
With CDN:     User (Tokyo) → Cloudflare edge (Tokyo) → Cached response (20ms)

Setup

  1. Create a Cloudflare account (free plan works)
  2. Add your domain — Cloudflare scans existing DNS records
  3. Update nameservers at your registrar to Cloudflare's
  4. Wait for activation (usually minutes)

DNS Configuration

Set your records in Cloudflare:

Type Name Value Proxy
A @ Your server IP Proxied (orange)
A www Your server IP Proxied
A api Your server IP Proxied or DNS-only
MX @ mail.example.com DNS-only (always)

Cache Configuration

Page Rules

# Cache static pages for 1 month
URL: example.com/static/*
Setting: Cache Level → Cache Everything, Edge TTL → 1 month

# Bypass cache for admin area
URL: example.com/admin/*
Setting: Cache Level → Bypass

Cache Headers from Your Server

# Nginx — control what Cloudflare caches
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ {
    expires 30d;
    add_header Cache-Control "public, immutable";
}

location /api/ {
    add_header Cache-Control "no-store";
}

Security Features (Free)

Feature What It Does
DDoS protection Absorbs volumetric attacks
WAF (limited) Blocks common attack patterns
Bot management Challenges suspicious traffic
SSL/TLS Free universal SSL certificate
Always Online Serves cached pages if your server goes down

Performance Features

Speed → Optimization:
  ☑ Auto Minify (JS, CSS, HTML)
  ☑ Brotli compression
  ☑ Early Hints (103)
  ☑ HTTP/2 + HTTP/3

Verifying CDN is Working

# Check response headers
curl -I https://example.com

# Look for:
# cf-cache-status: HIT      ← Served from Cloudflare cache
# cf-cache-status: MISS     ← Fetched from origin
# cf-cache-status: DYNAMIC  ← Not cached (HTML pages by default)

Development Mode

When making changes and need to bypass cache:

  1. Go to Caching → Configuration
  2. Enable Development Mode (3 hours, then auto-disables)

Tip Cloudflare's free plan is sufficient for most websites. The Pro plan ($20/mo) adds the full WAF and image optimization. You rarely need the Business plan.

Was this article helpful?