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

别再乱开小橙云:Cloudflare 配置避坑与常用命令清单

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

Cloudflare 使用避坑指南|附完整命令

Cloudflare 是目前最常见的网站加速、安全防护与 DNS 托管平台之一。很多站长、开发者、独立开发者都会把域名接入 Cloudflare,用它来做 DNS 解析、CDN 加速、HTTPS 证书、WAF 防护、DDoS 缓解、反向代理、Zero Trust、Pages 部署等。

但 Cloudflare 虽然强大,实际使用时也有不少“坑”。有些问题不是 Cloudflare 本身不好,而是使用者对 DNS、缓存、SSL、源站配置、代理模式等理解不够,导致网站打不开、证书异常、后台登录失败、接口请求被缓存、真实 IP 暴露、回源异常、SEO 出问题等。

本文整理一份 Cloudflare 使用避坑指南,并附上常用完整命令,适合准备接入 Cloudflare 或已经在使用 Cloudflare 的用户参考。


一、接入 Cloudflare 前先搞清楚它在做什么

很多人以为 Cloudflare 只是一个 DNS 服务商,其实它不仅仅是 DNS。

当你把域名接入 Cloudflare 后,主要会涉及两种模式:

模式 图标 作用
DNS only 灰色云朵 只做 DNS 解析,请求直接到源站
Proxied 橙色云朵 请求先到 Cloudflare,再由 Cloudflare 回源

如果开启橙色云朵,访问链路通常是:

用户浏览器
   ↓
Cloudflare 边缘节点
   ↓
你的源站服务器

这意味着:

  1. 用户看到的是 Cloudflare 的 IP,不是你的服务器 IP;
  2. Cloudflare 可以做缓存、HTTPS、WAF、安全过滤;
  3. 你的源站需要正确配置回源 HTTPS;
  4. 某些端口、协议、请求头、真实 IP 获取方式会发生变化。

很多问题都是因为用户以为“只是改了 DNS”,但实际上已经让 Cloudflare 介入了整个 HTTP/HTTPS 请求链路。


二、DNS 接入避坑:不要随便删除原解析

接入 Cloudflare 时,它会自动扫描你原来的 DNS 记录,但自动扫描并不一定完整。

常见遗漏包括:

  • 子域名记录;
  • MX 邮件记录;
  • TXT 验证记录;
  • DKIM、SPF、DMARC 邮件安全记录;
  • _acme-challenge 证书验证记录;
  • SRV 记录;
  • 部分 AAAA IPv6 记录。

建议操作

在切换 NS 服务器之前,先导出或截图原 DNS 记录。

如果原 DNS 服务商支持导出 Zone File,可以先备份。

Linux/macOS 查询当前 DNS 记录

dig example.com A
dig example.com AAAA
dig www.example.com CNAME
dig example.com MX
dig example.com TXT
dig _dmarc.example.com TXT
dig selector1._domainkey.example.com TXT

查询指定 DNS 服务器的解析结果

dig @8.8.8.8 example.com A
dig @1.1.1.1 example.com A
dig @223.5.5.5 example.com A

查询 NS 是否已经切换成功

dig example.com NS

也可以使用:

nslookup -type=ns example.com

如果结果已经显示 Cloudflare 分配给你的 NS,例如:

alice.ns.cloudflare.com
bob.ns.cloudflare.com

说明域名 NS 已经切换到 Cloudflare。


三、橙色云朵不是所有记录都能开

Cloudflare 的代理主要支持 HTTP/HTTPS 流量。不是所有 DNS 记录都适合开启橙色云朵。

建议开启橙色云朵的记录

example.com
www.example.com
blog.example.com
api.example.com

前提是这些服务走 HTTP/HTTPS。

不建议开启橙色云朵的记录

mail.example.com
smtp.example.com
imap.example.com
pop.example.com
ftp.example.com
ssh.example.com
数据库连接域名
游戏服务器域名
非 HTTP 服务域名

例如邮件服务器、SSH、FTP、数据库,一般应使用灰色云朵 DNS only。否则可能出现端口不通、协议异常、连接失败等问题。

检查域名是否走 Cloudflare

dig example.com A

如果返回的是类似 Cloudflare 的 IP,通常说明走了代理。也可以:

curl -I https://example.com

如果响应头里有:

cf-cache-status
cf-ray

通常说明请求经过了 Cloudflare。


四、SSL/TLS 模式避坑:不要长期使用 Flexible

Cloudflare SSL/TLS 模式中最容易踩坑的是 Flexible

常见模式有:

模式 浏览器到 Cloudflare Cloudflare 到源站
Off HTTP HTTP
Flexible HTTPS HTTP
Full HTTPS HTTPS
Full strict HTTPS HTTPS,且验证源站证书

Flexible 的问题

Flexible 表面上能快速让网站显示 HTTPS,但它的回源是 HTTP,常见问题包括:

  1. WordPress 后台无限重定向;
  2. Laravel、Django、Rails 判断协议错误;
  3. 网站生成 HTTP 链接;
  4. 登录状态异常;
  5. 安全性不足;
  6. HSTS 配置后可能更麻烦。

推荐配置

生产环境建议使用:

Full (strict)

也就是浏览器到 Cloudflare 使用 HTTPS,Cloudflare 到源站也使用 HTTPS,并且验证源站证书。

源站安装证书

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

使用 Certbot 申请 Let’s Encrypt 证书,Nginx 示例

Ubuntu/Debian:

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

查看自动续期:

sudo systemctl status certbot.timer

测试续期:

sudo certbot renew --dry-run

使用 Certbot 申请 Let’s Encrypt 证书,Apache 示例

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

使用 standalone 模式申请证书

如果服务器没有安装 Nginx/Apache,或者想临时申请:

sudo apt update
sudo apt install -y certbot
sudo certbot certonly --standalone -d example.com -d www.example.com

注意 standalone 模式需要占用 80 端口。如果 80 端口被 Nginx 占用,可以先停止:

sudo systemctl stop nginx
sudo certbot certonly --standalone -d example.com -d www.example.com
sudo systemctl start nginx

五、开启 Cloudflare 后源站获取真实 IP

开启 Cloudflare 代理后,源站看到的访问 IP 通常是 Cloudflare 节点 IP,而不是用户真实 IP。

如果你的应用需要记录真实访客 IP,必须读取 Cloudflare 提供的请求头:

CF-Connecting-IP

Nginx 配置真实 IP

首先获取 Cloudflare IP 段:

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

编辑 Nginx 配置:

sudo nano /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

重载 Nginx:

sudo systemctl reload nginx

查看访问日志:

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

Apache 配置真实 IP

安装模块:

sudo apt update
sudo apt install -y libapache2-mod-remoteip
sudo a2enmod remoteip

编辑配置:

sudo nano /etc/apache2/conf-available/cloudflare-remoteip.conf

写入:

RemoteIPHeader CF-Connecting-IP

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

启用配置:

sudo a2enconf cloudflare-remoteip
sudo apachectl configtest
sudo systemctl reload apache2

六、防火墙避坑:只允许 Cloudflare 回源

如果你的站点已经开启橙色云朵代理,理论上用户不需要直接访问源站 IP。为了避免源站 IP 被扫到后绕过 Cloudflare 攻击,建议在服务器防火墙中只允许 Cloudflare IP 访问 80/443。

不过需要注意:如果你还需要自己直接访问源站、监控服务访问源站、Let’s Encrypt HTTP 验证访问源站,要提前放行相关 IP 或使用 DNS 验证。

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

启用 UFW:

sudo ufw enable

查看规则:

sudo ufw status numbered

iptables 配置示例

允许已建立连接:

sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

允许 SSH:

sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT

允许 Cloudflare IPv4:

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

拒绝其他访问 80/443:

sudo iptables -A INPUT -p tcp --dport 80 -j DROP
sudo iptables -A INPUT -p tcp --dport 443 -j DROP

保存规则,Debian/Ubuntu 可安装:

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

七、缓存避坑:不要把后台、接口、登录页缓存了

Cloudflare 的缓存能力很强,但缓存配置错误也非常危险。

常见问题:

  1. WordPress 后台页面被缓存;
  2. 登录后的用户页面显示给其他用户;
  3. API 返回旧数据;
  4. 支付回调异常;
  5. 页面更新后用户看不到新内容;
  6. 静态资源版本混乱。

推荐缓存原则

可以缓存:

CSS
JS
图片
字体
公开静态 HTML

不建议缓存:

/wp-admin/*
/wp-login.php
/api/*
/admin/*
/user/*
/checkout/*
/cart/*
/payment/*

使用 Cache Rules

在 Cloudflare 后台进入:

Caching → Cache Rules

建议创建规则:

If URL path starts with /wp-admin
Then Bypass cache

再创建:

If URL path equals /wp-login.php
Then Bypass cache

API 站点可设置:

If URL path starts with /api
Then Bypass cache

清理缓存

如果网站更新后没有生效,可以手动清理缓存。

Cloudflare 后台:

Caching → Configuration → Purge Cache

如果使用 API 清理全部缓存,可以执行:

curl -X POST "https://api.cloudflare.com/client/v4/zones/YOUR_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/YOUR_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/app.js"]}'

八、WordPress 使用 Cloudflare 的常见坑

WordPress 是 Cloudflare 用户中最容易出问题的一类。

1. 后台无限重定向

通常原因是 SSL 模式使用了 Flexible,或者 WordPress 没正确识别 HTTPS。

建议:

  1. Cloudflare SSL/TLS 设置为 Full strict;
  2. 源站安装有效 HTTPS 证书;
  3. WordPress 配置识别 Cloudflare HTTPS。

wp-config.php 中添加:

if (isset($_SERVER['HTTP_CF_VISITOR'])) {
    $cf_visitor = json_decode($_SERVER['HTTP_CF_VISITOR']);
    if (isset($cf_visitor->scheme) && $cf_visitor->scheme === 'https') {
        $_SERVER['HTTPS'] = 'on';
    }
}

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
    $_SERVER['HTTPS'] = 'on';
}

2. 后台被缓存

建议绕过:

/wp-admin/*
/wp-login.php
/wp-json/*

3. 评论、登录、购物车异常

如果使用 WooCommerce,一定要绕过:

/cart/*
/checkout/*
/my-account/*
/wc-api/*

4. 获取真实 IP

可以配置 Nginx/Apache 真实 IP,也可以使用官方插件,但更推荐在服务器层面处理。


九、Cloudflare 端口限制避坑

Cloudflare 代理并不是所有端口都支持。如果你开启橙色云朵,但源站服务运行在非标准端口,可能会访问失败。

Cloudflare 支持代理的常见 HTTP 端口包括:

80
8080
8880
2052
2082
2086
2095

常见 HTTPS 端口包括:

443
2053
2083
2087
2096
8443

如果你的服务运行在:

3000
5000
8000
9000
10000

直接开启橙色云朵可能无法正常访问。解决方式:

  1. 让 Nginx/Caddy 反向代理到本地端口;
  2. 改用 Cloudflare 支持的端口;
  3. 使用灰色云朵 DNS only;
  4. 使用 Cloudflare Tunnel。

Nginx 反向代理到本地 3000

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

    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 CF-Connecting-IP $http_cf_connecting_ip;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

测试并重载:

sudo nginx -t
sudo systemctl reload nginx

十、Cloudflare Tunnel 避坑

Cloudflare Tunnel 可以让你不暴露源站公网 IP,也不需要开放入站端口,非常适合家庭服务器、内网服务、临时项目。

安装 cloudflared

Debian/Ubuntu:

curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null

echo "deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared any main" | sudo tee /etc/apt/sources.list.d/cloudflared.list

sudo apt update
sudo apt install -y cloudflared

查看版本:

cloudflared --version

登录 Cloudflare:

cloudflared tunnel login

创建 Tunnel:

cloudflared tunnel create my-tunnel

查看 Tunnel:

cloudflared tunnel list

创建配置文件:

sudo mkdir -p /etc/cloudflared
sudo nano /etc/cloudflared/config.yml

示例配置:

tunnel: my-tunnel
credentials-file: /root/.cloudflared/YOUR_TUNNEL_ID.json

ingress:
  - hostname: app.example.com
    service: http://localhost:3000
  - hostname: ssh.example.com
    service: ssh://localhost:22
  - service: http_status:404

创建 DNS 记录:

cloudflared tunnel route dns my-tunnel app.example.com

安装为系统服务:

sudo cloudflared service install
sudo systemctl enable cloudflared
sudo systemctl start cloudflared

查看状态:

sudo systemctl status cloudflared

查看日志:

journalctl -u cloudflared -f

Tunnel 常见坑

  1. hostname 必须已经在 Cloudflare 托管的域名下;
  2. 配置文件路径和 credentials 文件路径要正确;
  3. 本地服务必须已经启动;
  4. 如果服务监听在 127.0.0.1,配置也应使用 localhost127.0.0.1
  5. 不要忘记最后的兜底规则:
- service: http_status:404

十一、WAF 和安全规则避坑

Cloudflare 的 WAF 很好用,但如果规则太激进,可能导致正常用户访问失败。

常见误伤场景:

  1. 后台登录被拦截;
  2. API 请求被 JS Challenge;
  3. 移动 App 请求被验证页面拦截;
  4. 支付回调被拦截;
  5. 搜索引擎爬虫异常;
  6. Webhook 请求失败。

建议

对于 API、Webhook、支付回调,不要随便启用浏览器验证。因为这些请求不是浏览器,不会执行 JavaScript Challenge。

例如以下路径建议设置跳过或降低安全级别:

/api/*
/webhook/*
/payment/callback/*
/stripe/webhook
/github/webhook

创建 WAF 自定义规则示例

规则表达式示例:

(http.request.uri.path starts_with "/api/")

动作选择:

Skip

或者:

Allow

对于后台路径可以提高安全性:

(http.request.uri.path starts_with "/admin")

动作:

Managed Challenge

但要注意,如果后台有自动化接口或移动端访问,可能会被误伤。


十二、Rocket Loader、Auto Minify 不要盲目开启

Cloudflare 提供很多性能优化开关,例如:

Auto Minify
Rocket Loader
Brotli
Early Hints
HTTP/2
HTTP/3
0-RTT

其中 Brotli、HTTP/2、HTTP/3 通常可以开启,但 Rocket Loader 和 Auto Minify 不建议盲目开启。

Rocket Loader 常见问题

  1. JS 执行顺序变化;
  2. 前端框架异常;
  3. 统计代码不工作;
  4. 广告代码不显示;
  5. 表单提交异常;
  6. 页面交互失效。

如果你使用 Vue、React、Next.js、Nuxt、复杂电商主题、第三方统计脚本,建议谨慎开启。

Auto Minify 常见问题

如果前端构建工具已经压缩过 CSS/JS,再由 Cloudflare 二次处理,可能出现兼容性问题。

建议:

  • 静态博客可尝试开启;
  • 复杂前端项目谨慎开启;
  • 出问题先关闭 Rocket Loader;
  • 前端异常时清理缓存后再测试。

十三、HSTS 避坑:开启前一定确认 HTTPS 完全正常

HSTS 可以强制浏览器只使用 HTTPS 访问站点,是安全增强功能。但它也有风险。

如果你开启了 HSTS,尤其是开启了:

Include subdomains
Preload

那么一旦某个子域名没有正确配置 HTTPS,用户可能无法访问,而且浏览器会强制 HTTPS。

开启 HSTS 前检查

curl -I https://example.com
curl -I https://www.example.com
curl -I https://api.example.com

确认证书正常:

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

检查是否有 HSTS 响应头:

curl -I https://example.com | grep -i strict

如果返回:

Strict-Transport-Security: max-age=31536000

说明 HSTS 已生效。

建议

第一次开启可以先使用较短时间:

max-age=300

确认无问题后再逐步增加到:

max-age=31536000

不要一开始就提交 HSTS Preload,除非你非常确定所有子域名 HTTPS 都长期稳定。


十四、源站 IP 暴露避坑

很多人以为开启 Cloudflare 后源站 IP 就绝对隐藏了,这是错误的。

源站 IP 可能通过以下方式泄露:

  1. 历史 DNS 记录;
  2. 邮件服务器同 IP;
  3. 子域名未代理;
  4. 证书透明日志;
  5. GitHub 配置泄露;
  6. 服务器主动请求外部服务;
  7. 旧的 A 记录缓存;
  8. 直接使用 IP 访问返回站点内容。

建议

  1. 网站服务器和邮件服务器分离;
  2. 不要让 mail.example.com 指向网站源站 IP;
  3. 防火墙只允许 Cloudflare 回源;
  4. 源站 Nginx 禁止直接 IP 访问;
  5. 敏感子域名谨慎使用 DNS only。

Nginx 禁止直接 IP 访问

server {
    listen 80 default_server;
    listen 443 ssl default_server;

    server_name _;

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

    return 444;
}

测试并重载:

sudo nginx -t
sudo systemctl reload nginx

十五、排查 Cloudflare 问题的常用命令

查看响应头

curl -I https://example.com

重点关注:

status code
server
cf-cache-status
cf-ray
cache-control
strict-transport-security

查看完整请求过程

curl -v https://example.com

强制指定 Host 访问源站 IP

用于测试源站是否正常:

curl -I --resolve example.com:443:YOUR_ORIGIN_IP https://example.com

HTTP 测试:

curl -I --resolve example.com:80:YOUR_ORIGIN_IP http://example.com

检查证书

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

检查 DNS 解析链路

dig +trace example.com

查看本地访问到哪个 IP

dig example.com A
dig example.com AAAA

测试 HTTP/2

curl -I --http2 https://example.com

测试 HTTP/3

如果 curl 支持 HTTP/3:

curl -I --http3 https://example.com

查看 Nginx 错误日志

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

查看 Nginx 访问日志

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

查看系统服务状态

sudo systemctl status nginx
sudo systemctl status apache2
sudo systemctl status cloudflared

十六、推荐的 Cloudflare 基础配置清单

对于普通网站,推荐如下:

DNS

主站 A/CNAME:橙色云朵
www:橙色云朵
mail/smtp/imap/pop:灰色云朵
SSH/数据库/非 HTTP 服务:灰色云朵

SSL/TLS

模式:Full strict
Always Use HTTPS:开启
Automatic HTTPS Rewrites:开启
HSTS:谨慎开启

缓存

静态资源:缓存
后台路径:绕过
API:绕过或按业务设置
登录/支付/购物车:绕过

安全

WAF:开启但不要过度激进
Bot Fight Mode:谨慎开启
后台路径:可加 Challenge
API/Webhook:不要 JS Challenge
源站防火墙:只允许 Cloudflare 回源

性能

Brotli:开启
HTTP/2:开启
HTTP/3:可开启
Rocket Loader:谨慎
Auto Minify:谨慎

十七、常见故障与解决方向

1. 网站 521

含义通常是 Cloudflare 无法连接源站。

检查:

sudo systemctl status nginx
sudo systemctl status apache2
sudo ss -tlnp | grep -E ':80|:443'

确认防火墙是否放行 Cloudflare IP。

2. 网站 522

通常是连接源站超时。

检查:

ping YOUR_ORIGIN_IP
curl -I --resolve example.com:443:YOUR_ORIGIN_IP https://example.com

可能原因:

  • 源站负载过高;
  • 防火墙拦截;
  • 网络线路异常;
  • Nginx/Apache 没响应。

3. 网站 525

SSL 握手失败。

检查:

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

可能原因:

  • 源站证书配置错误;
  • SSL 协议不兼容;
  • Nginx SSL 配置错误。

4. 网站 526

Full strict 模式下证书无效。

解决:

  • 安装有效 Let’s Encrypt 证书;
  • 使用 Cloudflare Origin Certificate;
  • 确保证书域名匹配。

十八、总结

Cloudflare 的核心价值是 DNS、CDN、安全、防护和边缘网络能力,但它不是“开了就自动变快、自动安全、自动没问题”的万能工具。真正稳定的配置,依赖你对 DNS、HTTPS、缓存、源站、防火墙、真实 IP、WAF 规则的正确理解。

最重要的避坑原则可以总结为:

  1. 切换 NS 前备份 DNS 记录;
  2. 非 HTTP 服务不要开橙色云朵;
  3. SSL/TLS 优先使用 Full strict;
  4. 源站必须正确安装 HTTPS 证书;
  5. 开启代理后要配置真实 IP;
  6. 后台、登录、支付、API 不要乱缓存;
  7. 防火墙只允许 Cloudflare 回源;
  8. WAF 不要误伤 API 和 Webhook;
  9. HSTS 不要一上来就 Preload;
  10. 排查问题时先看 DNS、响应头、源站日志和 Cloudflare 错误码。

如果你是新手,建议先从基础功能开始:DNS 托管、HTTPS、静态资源缓存、简单 WAF。等网站稳定后,再逐步尝试 Tunnel、Zero Trust、Workers、Rules、R2 等高级功能。Cloudflare 很强,但越强的工具越需要谨慎配置。配置得当,它可以显著提升网站安全性和可用性;配置错误,它也可能成为故障的来源。

目录结构
全文