🚀 Cloudflare CDN
Cloudflare CDN 是全球领先的内容分发网络,通过 330+ 个边缘节点加速网站内容交付,提升性能并降低成本。
产品简介
Cloudflare CDN 在每个数据中心的每台服务器上运行所有服务,确保内容从最近的位置交付给用户, 能够以最高规模交付静态和动态内容。
⚡ 极速加载
全球边缘节点,就近提供内容
💰 零出口费用
无限带宽,不收取流量费
🛡️ DDoS 防护
自动防御 DDoS 攻击
🔧 智能缓存
自动优化缓存策略
核心功能
1. 全球网络
在全球 330+ 个城市部署数据中心,覆盖 100+ 个国家,确保用户始终从最近的节点获取内容。
2. 智能缓存
自动缓存静态资源,支持自定义缓存规则,大幅减少源服务器负载。
3. 性能优化
- HTTP/2 & HTTP/3: 支持最新协议
- Brotli 压缩: 自动压缩内容
- 图片优化: 自动转换为 WebP 格式
- Rocket Loader: 优化 JavaScript 加载
快速开始
1. 添加网站到 Cloudflare
- 登录 Cloudflare Dashboard
- 点击 "Add a Site"
- 输入你的域名
- 选择计划(免费计划即可)
- 更新域名服务器到 Cloudflare 提供的 NS 记录
2. 配置 DNS
添加 DNS 记录,将流量路由到你的源服务器:
DNS 配置示例
# A 记录(IPv4)
example.com → 192.0.2.1 (代理已启用 🟠)
# CNAME 记录
www.example.com → example.com (代理已启用 🟠)
# AAAA 记录(IPv6)
example.com → 2001:db8::1 (代理已启用 🟠) 3. 配置缓存规则
在 Dashboard 中 → "Rules" → "Page Rules" 或 "Cache Rules"
使用 Workers 控制缓存
自定义缓存策略
Worker 缓存示例
export default {
async fetch(request) {
const url = new URL(request.url);
// 自定义缓存键
const cacheKey = new Request(url.toString(), request);
const cache = caches.default;
// 尝试从缓存获取
let response = await cache.match(cacheKey);
if (!response) {
// 从源服务器获取
response = await fetch(request);
// 修改缓存头
response = new Response(response.body, response);
response.headers.set('Cache-Control', 'public, max-age=3600');
// 存入缓存
await cache.put(cacheKey, response.clone());
}
return response;
}
} 按设备类型缓存
按设备缓存
export default {
async fetch(request) {
const userAgent = request.headers.get('User-Agent') || '';
const isMobile = /Mobile|Android|iPhone/i.test(userAgent);
const url = new URL(request.url);
const cacheKey = new URL(url);
cacheKey.searchParams.set('device', isMobile ? 'mobile' : 'desktop');
return fetch(new Request(cacheKey.toString(), request));
}
} 缓存控制
缓存级别
在 Dashboard → "Caching" → "Configuration" 中设置:
- No Query String: 忽略查询参数
- Ignore Query String: 仅缓存静态资源
- Standard: 根据查询参数缓存(推荐)
- Cache Everything: 缓存所有内容(需配合 Page Rules)
清除缓存
清除缓存 API
# 清除所有缓存
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone_id}/purge_cache" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
--data '{"purge_everything":true}'
# 清除指定 URL
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone_id}/purge_cache" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
--data '{"files":["https://example.com/style.css","https://example.com/script.js"]}'
# 清除指定标签
curl -X POST "https://api.cloudflare.com/client/v4/zones/{zone_id}/purge_cache" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
--data '{"tags":["tag1","tag2"]}' 性能优化技巧
1. 启用 Auto Minify
Dashboard → "Speed" → "Optimization" → 启用 JavaScript、CSS、HTML 压缩
2. 启用 Brotli
Dashboard → "Speed" → "Optimization" → 启用 Brotli 压缩
3. 配置 Cache TTL
缓存头配置
# 在源服务器设置缓存头
Cache-Control: public, max-age=31536000, immutable # 静态资源
Cache-Control: public, max-age=3600 # 动态内容
Cache-Control: no-cache, no-store, must-revalidate # 不缓存 4. 使用 Argo Smart Routing
开启 Argo 可将流量路由到最快的路径,平均提速 30%
监控和分析
查看缓存分析
Dashboard → "Analytics" → "Traffic" 查看:
- 缓存命中率
- 带宽节省
- 请求数统计
- 响应时间分布
使用 GraphQL API 查询
GraphQL 分析查询
query {
viewer {
zones(filter: {zoneTag: "YOUR_ZONE_ID"}) {
httpRequests1dGroups(limit: 7) {
sum {
requests
cachedRequests
bytes
}
dimensions {
date
}
}
}
}
} 常见问题
Q: 如何强制缓存某个页面?
创建 Page Rule: example.com/page/* → Cache Level: Cache Everything
Q: 如何排除某些文件不被缓存?
创建 Page Rule: example.com/admin/* → Cache Level: Bypass
Q: 缓存多久生效?
通常 2-5 分钟内全球生效
Q: 免费计划有限制吗?
免费计划提供无限带宽和请求,但 Page Rules 限制为 3 条
定价
| 功能 | 免费计划 | Pro ($20/月) |
|---|---|---|
| 带宽 | 无限制 | 无限制 |
| Page Rules | 3 条 | 20 条 |
| 图片优化 | 基础 | 增强(Polish) |
| 分析数据 | 24 小时 | 30 天 |