Skip to content

Cloudflare 基础

Use Cloudflare for CDN, DNS, 边缘函数, and security features for web applications.

你将学到

  • 什么 Cloudflare provides and 如何它 工作
  • CDN and DNS management
  • Cloudflare 工作者 for 边缘 computing
  • Security features: WAF, DDoS protection, SSL

概念讲解

什么是 Cloudflare?

Cloudflare is a platform that sits between your visitors and your origin server, providing a content delivery network (CDN), DNS, security, and edge computing. When a visitor requests your site, Cloudflare serves cached content from the nearest edge location, reducing latency and origin load. Cloudflare has over 300 cities in its network, meaning content is delivered from close to the user wherever they are in the world.

CDN and DNS

Cloudflare's CDN caches your static assets — images, CSS, JavaScript — at edge locations worldwide. The first request fetches from your origin; subsequent requests are served from the cache, dramatically faster. Cloudflare can also manage your DNS, offering fast resolution and instant changes. Using Cloudflare for both DNS and CDN gives you a single control plane for your web infrastructure.

Cloudflare Workers

Workers are serverless functions that run at Cloudflare's edge — not in a single region, but in every city. You write JavaScript (or languages compiled to WebAssembly) and deploy it with Wrangler. Workers can modify requests, serve responses, run A/B tests, or build entire applications. Because they run at the edge, latency is minimal. CoderNewbie itself is deployed on Cloudflare Pages with Workers for dynamic functionality.

Security Features

Cloudflare provides a suite of security features. SSL/TLS is automatically provisioned and renewed — you get HTTPS with no effort. The Web Application Firewall (WAF) blocks malicious requests. DDoS protection is built in and always on. Bot management identifies and blocks automated traffic. Rate limiting protects against abuse. These features are configured through the dashboard or via API, and many are available on the free plan.

Cloudflare Pages

Cloudflare Pages is a static site hosting platform that integrates with Git. Push to your repository and Cloudflare builds and deploys automatically. It supports preview deployments for every pull request, custom domains, and automatic SSL. For dynamic functionality, Pages Functions run server-side code at the edge using the Workers runtime. This combination of static hosting with edge functions is powerful for Jamstack applications — you get the performance of static sites with the capability of server-side processing where needed.

示例代码

              # Install Wrangler CLI and authenticate
npm install -g wrangler
wrangler login

# Create and deploy a Cloudflare Worker
wrangler init my-worker
# src/index.ts:
#   export default {
#     async fetch(request: Request): Promise<Response> {
#       const url = new URL(request.url);
#       return new Response("Hello from " + url.hostname);
#     },
#   };
wrangler deploy

# Manage DNS records via the Cloudflare API
curl -X POST "https://api.cloudflare.com/client/v4/zones" \
  -H "Authorization: Bearer $CF_TOKEN" \
  -H "Content-Type: application/json"
            

Wrangler 是 Cloudflare CLI for deploying 工作者. 工作者运行 at 边缘 in 每个 city, giving minimal latency. API 能也 manage DNS and other 设置.

动手试一试

常见错误

错误写法

不缓存 enough 内容 at 边缘

正确做法

设置 appropriate 缓存请求头 on 静态 assets. Use Cloudflare's 缓存规则到 缓存 aggressively. Uncached 内容 hits 你的原点 on 每个请求, increasing latency and 服务器加载.

错误写法

存储 API 令牌 in 客户端-边 code

正确做法

Use Cloudflare 工作者 secrets (wrangler secret put) for sensitive 值. 从不 embed API 令牌 in 客户端-边 JavaScript — 它们是 可见到 anyone 谁打开浏览器 devtools.

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误