上一篇 下一篇 分享链接 返回 返回顶部

Cloudflare 生产环境上线实战:DNS、SSL、WAF、Tunnel 与回源安全全流程命令指南

发布人:慈云数据-客服中心 发布时间:24小时前 阅读量:4

Cloudflare 生产环境部署指南|附完整命令

在现代 Web 架构中,Cloudflare 已经不仅仅是一个 DNS 服务商,它更像是位于用户与源站之间的一层全球边缘网络。通过 Cloudflare,可以实现 DNS 托管、HTTPS 证书、CDN 加速、DDoS 防护、WAF 防火墙、Bot 管理、访问控制、Zero Trust、Cloudflare Tunnel、Workers 边缘计算等能力。

对于生产环境而言,接入 Cloudflare 不能只是“把域名解析过去”这么简单。错误的 SSL 模式、过度缓存、源站暴露、回源策略不合理,都可能导致线上故障、安全风险或性能下降。

本文将从生产环境视角,完整介绍 Cloudflare 的部署流程,并提供常用命令示例,适用于以下场景:

  • 将已有网站迁移到 Cloudflare;
  • 使用 Cloudflare 托管 DNS;
  • 开启 CDN、HTTPS、WAF、防护规则;
  • 使用 Cloudflare Tunnel 隐藏源站;
  • 使用命令行和 API 管理生产配置;
  • 部署前后进行验证与排查。

一、生产环境接入 Cloudflare 的整体架构

典型生产环境接入 Cloudflare 后,请求链路如下:

用户浏览器
   ↓
Cloudflare Anycast 边缘节点
   ↓
Cloudflare 安全 / 缓存 / WAF / TLS
   ↓
源站负载均衡器 / Nginx / 应用服务
   ↓
数据库 / Redis / 内部服务

常见部署模式有两种。

1. 传统 CDN 回源模式

Client → Cloudflare → Origin Server

这种模式下,源站仍然暴露公网 IP,Cloudflare 通过 DNS 代理橙云接入流量。

优点:

  • 部署简单;
  • 兼容性强;
  • 适合已有生产站点平滑迁移。

缺点:

  • 源站 IP 可能被扫描或历史 DNS 泄露;
  • 需要额外限制只允许 Cloudflare IP 访问源站。

2. Cloudflare Tunnel 模式

Client → Cloudflare → cloudflared Tunnel → Internal Service

该模式下,源站可以不暴露公网端口,cloudflared 主动向 Cloudflare 建立隧道。

优点:

  • 源站无需公网 IP;
  • 安全性更高;
  • 适合内网服务、后台系统、零信任访问。

缺点:

  • 需要维护 cloudflared 服务;
  • 对高并发业务需要合理设计多实例和高可用。

二、部署前准备工作

在生产环境接入前,应先完成以下准备。

1. 确认域名和 DNS 现状

需要梳理当前域名解析记录,例如:

类型 主机名 记录值 用途
A example.com 1.2.3.4 主站
CNAME www example.com www 站点
A api 1.2.3.5 API 服务
MX @ mail.example.com 邮件
TXT @ SPF / DKIM 邮件验证
CNAME cdn xxx 静态资源

在迁移 DNS 前,可以使用以下命令导出现有解析情况。

dig example.com
dig www.example.com
dig api.example.com
dig MX example.com
dig TXT example.com

查看完整 DNS 解析链路:

dig +trace example.com

使用 nslookup 检查:

nslookup example.com
nslookup www.example.com

2. 检查源站 HTTPS 状态

生产环境强烈建议源站本身也支持 HTTPS,不要只依赖 Cloudflare 到用户侧的 HTTPS。

检查源站证书:

openssl s_client -connect example.com:443 -servername example.com

只查看证书有效期:

echo | openssl s_client -connect example.com:443 -servername example.com 2>/dev/null | openssl x509 -noout -dates

检查 TLS 协议支持:

curl -Iv https://example.com

3. 降低 DNS TTL

在正式切换到 Cloudflare 前,建议提前将原 DNS TTL 降低到 300 秒或更低,以便出现问题时快速回滚。

如果当前 DNS 服务商支持命令行或 API,可提前修改;如果只能在控制台操作,建议提前至少 24 小时完成。

验证 TTL:

dig example.com

输出中类似以下字段表示 TTL:

example.com. 300 IN A 1.2.3.4

三、将域名添加到 Cloudflare

1. 添加站点

在 Cloudflare 控制台中执行:

  1. 登录 Cloudflare;
  2. 点击 “Add a site”;
  3. 输入域名,例如 example.com
  4. 选择套餐;
  5. Cloudflare 会自动扫描现有 DNS 记录;
  6. 核对 DNS 记录;
  7. 修改域名注册商处的 Nameserver。

Cloudflare 会给出类似以下 Nameserver:

alex.ns.cloudflare.com
lisa.ns.cloudflare.com

需要到域名注册商处将原 NS 修改为 Cloudflare 提供的 NS。

2. 验证 NS 是否切换成功

dig NS example.com

或者:

whois example.com | grep -i "Name Server"

如果看到 Cloudflare 的 NS,说明切换已经生效。

也可以使用:

dig +short NS example.com

输出示例:

alex.ns.cloudflare.com.
lisa.ns.cloudflare.com.

四、配置 DNS 记录

在 Cloudflare 中,DNS 记录通常有两种代理状态:

状态 图标 含义
Proxied 橙云 流量经过 Cloudflare
DNS only 灰云 只做 DNS 解析,不经过 Cloudflare

生产环境建议:

  • Web 服务使用橙云;
  • 邮件相关记录使用灰云;
  • SSH、数据库、内部服务不要直接橙云代理;
  • API 根据业务情况决定是否橙云;
  • 需要 Web 防护的后台服务可以配合 Access 或 Tunnel。

示例记录:

A      @       1.2.3.4      Proxied
CNAME  www     example.com  Proxied
A      api     1.2.3.5      Proxied
MX     @       mail.example.com  DNS only
TXT    @       v=spf1 include:_spf.example.com ~all

使用命令验证:

dig example.com
dig www.example.com
dig api.example.com

如果开启橙云,解析出来的一般不是你的源站 IP,而是 Cloudflare 的边缘 IP。

dig +short example.com

示例输出:

104.21.xx.xx
172.67.xx.xx

五、配置 SSL/TLS

Cloudflare 中最关键的设置之一是 SSL/TLS 模式。生产环境不建议使用 Flexible 模式。

1. SSL/TLS 模式说明

模式 浏览器到 Cloudflare Cloudflare 到源站 生产建议
Off HTTP HTTP 不建议
Flexible HTTPS HTTP 不建议
Full HTTPS HTTPS 可用
Full Strict HTTPS HTTPS 且验证证书 强烈推荐

生产环境推荐:

SSL/TLS 模式:Full (strict)

2. 源站安装证书

可以使用 Let’s Encrypt 证书,也可以使用 Cloudflare Origin Certificate。

如果使用 Certbot 安装 Let’s Encrypt:

Ubuntu / Debian:

sudo apt update
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

续期测试:

sudo certbot renew --dry-run

查看证书:

sudo certbot certificates

如果使用 Nginx 手动配置证书:

server {
    listen 443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

测试 Nginx 配置:

sudo nginx -t
sudo systemctl reload nginx

3. 强制 HTTPS

Cloudflare 控制台建议开启:

SSL/TLS → Edge Certificates → Always Use HTTPS

也可以在 Nginx 配置 HTTP 跳转 HTTPS:

server {
    listen 80;
    server_name example.com www.example.com;

    return 301 https://$host$request_uri;
}

重载 Nginx:

sudo nginx -t
sudo systemctl reload nginx

六、限制源站只允许 Cloudflare 回源

如果源站公网 IP 暴露,攻击者可以绕过 Cloudflare 直接访问源站。因此生产环境建议只允许 Cloudflare IP 段访问 80/443。

1. 获取 Cloudflare IP 段

IPv4:

curl https://www.cloudflare.com/ips-v4

IPv6:

curl https://www.cloudflare.com/ips-v6

保存到本地:

curl -o /tmp/cf-ips-v4 https://www.cloudflare.com/ips-v4
curl -o /tmp/cf-ips-v6 https://www.cloudflare.com/ips-v6

2. 使用 UFW 配置防火墙

先允许 SSH,避免锁死自己:

sudo ufw allow 22/tcp

允许 Cloudflare IPv4 访问 80/443:

for ip in $(curl -s https://www.cloudflare.com/ips-v4); do
  sudo ufw allow from $ip to any port 80 proto tcp
  sudo ufw allow from $ip to any port 443 proto tcp
done

允许 Cloudflare IPv6:

for ip in $(curl -s https://www.cloudflare.com/ips-v6); do
  sudo ufw allow from $ip to any port 80 proto tcp
  sudo ufw allow from $ip to any port 443 proto tcp
done

拒绝其他来源访问 80/443:

sudo ufw deny 80/tcp
sudo ufw deny 443/tcp

启用 UFW:

sudo ufw enable
sudo ufw status numbered

注意:如果你还有负载均衡器、健康检查服务、监控服务需要访问源站,也需要加入白名单。

3. 使用 iptables 配置

允许 Cloudflare IPv4:

for ip in $(curl -s https://www.cloudflare.com/ips-v4); do
  sudo iptables -I INPUT -p tcp -m multiport --dports 80,443 -s $ip -j ACCEPT
done

拒绝其他访问:

sudo iptables -A INPUT -p tcp -m multiport --dports 80,443 -j DROP

保存规则:

Ubuntu/Debian:

sudo apt install -y iptables-persistent
sudo netfilter-persistent save

CentOS/RHEL:

sudo service iptables save

七、恢复真实客户端 IP

接入 Cloudflare 后,源站看到的默认远程 IP 可能是 Cloudflare 节点 IP。为了在日志、安全规则、限流系统中使用真实客户端 IP,需要读取 Cloudflare 传递的请求头。

Cloudflare 会传递:

CF-Connecting-IP
X-Forwarded-For

推荐使用 CF-Connecting-IP

1. Nginx 恢复真实 IP

安装模块通常已内置:

nginx -V 2>&1 | grep http_realip_module

创建 Cloudflare real IP 配置:

sudo vim /etc/nginx/conf.d/cloudflare-real-ip.conf

写入:

real_ip_header CF-Connecting-IP;

set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;

set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;

测试并重载:

sudo nginx -t
sudo systemctl reload nginx

2. 自动更新 Cloudflare IP 到 Nginx

由于 Cloudflare IP 段可能变化,可以使用脚本自动生成配置。

创建脚本:

sudo vim /usr/local/bin/update-cloudflare-realip.sh

写入:

#!/usr/bin/env bash
set -e

CONF="/etc/nginx/conf.d/cloudflare-real-ip.conf"

echo "real_ip_header CF-Connecting-IP;" > "$CONF"

for ip in $(curl -s https://www.cloudflare.com/ips-v4); do
  echo "set_real_ip_from $ip;" >> "$CONF"
done

for ip in $(curl -s https://www.cloudflare.com/ips-v6); do
  echo "set_real_ip_from $ip;" >> "$CONF"
done

nginx -t
systemctl reload nginx

授权:

sudo chmod +x /usr/local/bin/update-cloudflare-realip.sh

执行:

sudo /usr/local/bin/update-cloudflare-realip.sh

加入定时任务:

sudo crontab -e

添加:

0 3 * * 1 /usr/local/bin/update-cloudflare-realip.sh >/var/log/update-cloudflare-realip.log 2>&1

八、配置缓存策略

Cloudflare 的缓存策略对生产环境性能影响巨大。错误缓存可能导致用户看到旧数据、接口返回异常、登录态错乱。

1. 推荐缓存原则

生产建议:

  • 静态资源长缓存;
  • HTML 页面谨慎缓存;
  • API 默认不缓存;
  • 登录、支付、用户中心不缓存;
  • 文件带版本号或 hash 后再长缓存。

适合缓存的资源:

.js
.css
.png
.jpg
.jpeg
.gif
.webp
.svg
.ico
.woff
.woff2
.mp4

不建议缓存:

/api/*
/admin/*
/login
/logout
/payment/*
/user/*

2. Nginx 设置静态资源缓存头

location ~* \.(js|css|png|jpg|jpeg|gif|webp|svg|ico|woff|woff2)$ {
    expires 30d;
    add_header Cache-Control "public, max-age=2592000, immutable";
}

HTML 不缓存或短缓存:

location ~* \.html$ {
    add_header Cache-Control "no-cache";
}

API 不缓存:

location /api/ {
    add_header Cache-Control "no-store";
    proxy_pass http://127.0.0.1:3000;
}

重载:

sudo nginx -t
sudo systemctl reload nginx

3. 使用 curl 检查缓存命中

curl -I https://example.com/static/app.js

关注以下响应头:

CF-Cache-Status: HIT
Cache-Control: public, max-age=2592000
Age: 1234

常见 CF-Cache-Status 说明:

状态 含义
HIT 命中 Cloudflare 缓存
MISS 未命中,已回源
BYPASS 被规则或响应头绕过
EXPIRED 缓存过期后重新验证
DYNAMIC 动态内容,未缓存

4. 清理缓存

清理单个 URL:

curl -X POST "https://api.cloudflare.com/client/v4/zones//purge_cache" \
  -H "Authorization: Bearer " \
  -H "Content-Type: application/json" \
  --data '{"files":["https://example.com/static/app.js"]}'

清理全部缓存:

curl -X POST "https://api.cloudflare.com/client/v4/zones//purge_cache" \
  -H "Authorization: Bearer " \
  -H "Content-Type: application/json" \
  --data '{"purge_everything":true}'

生产环境不建议频繁全量清缓存,优先使用带 hash 的静态资源文件名,例如:

app.a8f3c9.js
style.91bc2.css

九、配置 WAF 与安全规则

Cloudflare 的 WAF 是生产环境安全防护的核心能力之一。

1. 基础安全建议

建议开启:

Security Level:Medium
Browser Integrity Check:On
Bot Fight Mode:按业务情况开启
WAF Managed Rules:开启
DDoS Protection:默认开启

对于后台路径,可以单独设置更严格规则。

2. 保护后台路径

例如后台地址:

https://example.com/admin

可以配置 WAF 规则:

URI Path starts with /admin
AND Country not in CN
→ Block 或 Managed Challenge

也可以按 IP 白名单:

URI Path starts with /admin
AND IP Source Address not in {your_office_ip}
→ Block

3. 使用 Cloudflare API 创建防火墙规则

Cloudflare 规则 API 会随版本变化,生产使用前应参考官方文档确认最新字段。下面给出一个典型思路示例:阻止访问 /admin 且不在允许 IP 内的请求。

curl -X POST "https://api.cloudflare.com/client/v4/zones//firewall/rules" \
  -H "Authorization: Bearer " \
  -H "Content-Type: application/json" \
  --data '[
    {
      "filter": {
        "expression": "(http.request.uri.path contains \"/admin\" and not ip.src in {203.0.113.10})"
      },
      "action": "block",
      "description": "Block admin except office IP"
    }
  ]'

如果使用新版 Rulesets API,需要按规则集方式创建,建议先通过控制台配置,再用 API 导出或通过 Terraform 管理。


十、使用 Cloudflare Tunnel 隐藏源站

对于后台服务、内网服务或不希望暴露公网 IP 的业务,可以使用 Cloudflare Tunnel。

1. 安装 cloudflared

Ubuntu / Debian:

curl -L --output cloudflared.deb \
https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb

sudo dpkg -i cloudflared.deb

验证版本:

cloudflared --version

CentOS / RHEL:

curl -L --output cloudflared.rpm \
https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-x86_64.rpm

sudo rpm -ivh cloudflared.rpm

macOS:

brew install cloudflared

2. 登录 Cloudflare

cloudflared tunnel login

该命令会打开浏览器,授权你的 Cloudflare 账户。

3. 创建 Tunnel

cloudflared tunnel create prod-web

查看 Tunnel:

cloudflared tunnel list

4. 配置 Tunnel 路由

创建配置目录:

sudo mkdir -p /etc/cloudflared

查看生成的凭证文件,一般在:

ls ~/.cloudflared/

复制凭证:

sudo cp ~/.cloudflared/.json /etc/cloudflared/

创建配置文件:

sudo vim /etc/cloudflared/config.yml

示例:

tunnel: 
credentials-file: /etc/cloudflared/.json

ingress:
  - hostname: app.example.com
    service: http://127.0.0.1:3000
  - hostname: admin.example.com
    service: http://127.0.0.1:8080
  - service: http_status:404

创建 DNS 路由:

cloudflared tunnel route dns prod-web app.example.com
cloudflared tunnel route dns prod-web admin.example.com

5. 以 systemd 方式运行

安装服务:

sudo cloudflared service install

启动:

sudo systemctl enable cloudflared
sudo systemctl start cloudflared

查看状态:

sudo systemctl status cloudflared

查看日志:

sudo journalctl -u cloudflared -f

重启:

sudo systemctl restart cloudflared

6. Tunnel 高可用建议

生产环境不要只运行单个 cloudflared 进程。可以在多台机器或多个容器中运行同一个 Tunnel。

例如在第二台服务器上复制配置后启动:

sudo mkdir -p /etc/cloudflared
sudo cp config.yml /etc/cloudflared/
sudo cp .json /etc/cloudflared/
sudo cloudflared service install
sudo systemctl enable cloudflared
sudo systemctl start cloudflared

Cloudflare 会自动在多个连接之间调度流量。


十一、使用 Terraform 管理 Cloudflare 配置

生产环境建议基础设施即代码,避免手动修改导致配置漂移。Terraform 是管理 Cloudflare DNS、规则、证书、Access 的常见方式。

1. 安装 Terraform

Ubuntu:

sudo apt update
sudo apt install -y wget gnupg software-properties-common

wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg >/dev/null

echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list

sudo apt update
sudo apt install -y terraform

验证:

terraform version

2. 配置 Cloudflare Provider

创建目录:

mkdir cloudflare-prod
cd cloudflare-prod

创建 main.tf

terraform {
  required_providers {
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "~> 4.0"
    }
  }
}

provider "cloudflare" {
  api_token = var.cloudflare_api_token
}

variable "cloudflare_api_token" {
  type      = string
  sensitive = true
}

variable "zone_id" {
  type = string
}

创建 DNS 记录:

resource "cloudflare_record" "root" {
  zone_id = var.zone_id
  name    = "@"
  type    = "A"
  value   = "1.2.3.4"
  proxied = true
  ttl     = 1
}

resource "cloudflare_record" "www" {
  zone_id = var.zone_id
  name    = "www"
  type    = "CNAME"
  value   = "example.com"
  proxied = true
  ttl     = 1
}

创建变量文件:

vim terraform.tfvars

写入:

cloudflare_api_token = "你的 API Token"
zone_id              = "你的 Zone ID"

初始化:

terraform init

预览:

terraform plan

应用:

terraform apply

格式化:

terraform fmt

十二、部署验证清单

接入完成后,不要立即宣布上线,应该进行系统验证。

1. DNS 验证

dig NS example.com
dig example.com
dig www.example.com
dig api.example.com

确认解析到 Cloudflare IP:

dig +short example.com

2. HTTPS 验证

curl -Iv https://example.com

检查是否有:

HTTP/2 200
server: cloudflare
cf-ray: xxxxx

检查证书:

openssl s_client -connect example.com:443 -servername example.com

3. 回源验证

查看 Nginx 访问日志:

sudo tail -f /var/log/nginx/access.log

查看错误日志:

sudo tail -f /var/log/nginx/error.log

4. 缓存验证

curl -I https://example.com/static/app.js
curl -I https://example.com/api/health

确认静态资源可以 HIT,API 不被缓存。

5. 防火墙验证

从非 Cloudflare 直接访问源站 IP:

curl -I http://1.2.3.4
curl -kI https://1.2.3.4

理想情况下应被拒绝、超时或返回非业务内容。

6. 真实 IP 验证

访问站点后查看 Nginx 日志,确认日志中记录的是用户公网 IP,而不是 Cloudflare IP。

sudo tail -n 20 /var/log/nginx/access.log

十三、常见故障与排查

1. 出现 521 Web Server Is Down

可能原因:

  • 源站未启动;
  • 防火墙拒绝了 Cloudflare IP;
  • Nginx 未监听 80/443;
  • 源站端口不通。

排查命令:

sudo systemctl status nginx
sudo ss -lntp | grep -E ':80|:443'
sudo ufw status
curl -I http://127.0.0.1

2. 出现 522 Connection Timed Out

可能原因:

  • 源站响应太慢;
  • 网络不通;
  • 安全组未放行;
  • 后端连接耗尽。

排查:

ping 1.2.3.4
traceroute 1.2.3.4
curl -Iv http://1.2.3.4
sudo tail -f /var/log/nginx/error.log

3. 出现 525 SSL Handshake Failed

可能原因:

  • 源站 TLS 配置错误;
  • Cloudflare 到源站握手失败;
  • 源站不支持 SNI;
  • 证书链异常。

排查:

openssl s_client -connect 1.2.3.4:443 -servername example.com
curl -Iv https://example.com --resolve example.com:443:1.2.3.4

4. 出现 526 Invalid SSL Certificate

通常发生在 Full Strict 模式下,说明源站证书不被信任或域名不匹配。

解决:

sudo certbot certificates
sudo certbot renew --dry-run
sudo nginx -t
sudo systemctl reload nginx

5. 静态资源没有缓存

检查响应头:

curl -I https://example.com/static/app.js

如果看到:

Cache-Control: no-store
CF-Cache-Status: BYPASS

说明源站或规则要求不缓存,需要调整 Nginx 或 Cloudflare Cache Rules。


十四、生产环境最佳实践

1. 不使用 Flexible SSL

Flexible 模式下,用户到 Cloudflare 是 HTTPS,但 Cloudflare 到源站是 HTTP,容易出现安全风险和重定向循环。生产环境应使用:

Full Strict

2. 源站必须有访问控制

如果不使用 Tunnel,源站应通过安全组、UFW、iptables 或云厂商防火墙限制只允许 Cloudflare IP 回源。

3. 缓存规则分层设计

推荐:

静态资源:长缓存
HTML:短缓存或不缓存
API:不缓存
后台路径:不缓存且加强安全

4. 所有修改先灰度

生产配置变更建议:

  1. 先在测试域名验证;
  2. 再对单个子域名启用;
  3. 观察日志和错误率;
  4. 最后全量切换。

5. 使用 API Token 而不是 Global API Key

创建 Cloudflare API Token 时,应使用最小权限原则。例如只管理 DNS,则只授予:

Zone:DNS:Edit
Zone:Zone:Read

不要在 CI/CD 中使用 Global API Key。

6. 监控和告警

建议监控:

  • 5xx 错误率;
  • 源站响应时间;
  • Cloudflare 缓存命中率;
  • WAF 拦截量;
  • Tunnel 连接状态;
  • 证书到期时间;
  • DNS 变更记录。

常用检查命令:

curl -s -o /dev/null -w "%{http_code} %{time_total}\n" https://example.com

持续检测:

watch -n 5 'curl -s -o /dev/null -w "%{http_code} %{time_total}\n" https://example.com'

十五、上线回滚方案

生产部署必须提前准备回滚方案。

1. DNS 回滚

如果 Cloudflare 接入后出现严重故障,可以将 DNS 记录切换为灰云,或者将域名 NS 切回原 DNS 服务商。

检查当前解析:

dig example.com

2. SSL 回滚

如果 Full Strict 出现证书问题,可临时切换到 Full,但不建议长期使用。更好的方式是修复源站证书。

3. 防火墙回滚

如果误封 Cloudflare IP 或业务 IP,可以查看 UFW 规则:

sudo ufw status numbered

删除规则:

sudo ufw delete <编号>

如果使用 iptables:

sudo iptables -L INPUT --line-numbers
sudo iptables -D INPUT <编号>

4. Tunnel 回滚

停止 cloudflared:

sudo systemctl stop cloudflared

查看日志:

sudo journalctl -u cloudflared -n 100

结语

Cloudflare 在生产环境中的价值,不只是 CDN 加速,更重要的是它将 DNS、TLS、安全防护、缓存、访问控制和边缘网络整合到了一起。正确接入 Cloudflare,可以显著提升站点的可用性、安全性和访问体验。

但生产部署一定要遵循几个核心原则:

  • DNS 迁移前先降低 TTL;
  • SSL 使用 Full Strict;
  • 源站证书必须有效;
  • 源站只允许 Cloudflare 回源;
  • 恢复真实客户端 IP;
  • API 和登录态页面不要误缓存;
  • WAF 规则先观察再拦截;
  • 关键配置使用 Terraform 或 API 管理;
  • 上线前完成验证,上线后持续监控;
  • 永远准备好回滚方案。

只要按照本文流程执行,Cloudflare 就可以作为一层稳定、安全、可扩展的生产入口,为你的业务提供可靠的边缘防护和全球加速能力。

目录结构
全文