Cloudflare 配置别踩坑:DNS、SSL、缓存和防火墙完整实战指南
Cloudflare 使用避坑指南|附完整命令
Cloudflare 是很多站长、开发者和企业都会使用的网络服务平台。它不仅提供 DNS 解析,还提供 CDN、WAF 防火墙、DDoS 防护、SSL/TLS 证书、Zero Trust、Workers、Pages、R2 对象存储、Tunnel 内网穿透等能力。对于个人站点来说,Cloudflare 往往是“免费且强大”的基础设施;对于企业业务来说,它也是提升安全性、可用性和访问速度的重要工具。
但是,Cloudflare 功能非常多,配置项也非常细。如果不了解它的工作方式,很容易出现一些典型问题,例如:
- 域名解析后网站打不开;
- SSL 配置错误导致无限重定向;
- 源站真实 IP 泄露;
- CDN 缓存导致页面更新不及时;
- API、后台、WebSocket 被缓存或拦截;
- 国内访问速度不稳定;
- 开启代理后端口无法访问;
- WordPress、宝塔、Nginx、Docker 服务配置异常;
- 防火墙规则误伤正常用户;
- 使用 Cloudflare Tunnel 后真实服务暴露不当。
本文会从实际使用角度出发,整理一份 Cloudflare 使用避坑指南,并附上常用完整命令,适合站长、运维、开发者参考。
一、Cloudflare 到底做了什么?
很多人第一次使用 Cloudflare 时,以为它只是一个 DNS 解析平台。实际上,Cloudflare 的核心能力是“反向代理 + DNS + 安全防护 + 边缘网络”。
当你把域名接入 Cloudflare 并开启橙色云朵代理后,请求链路大致如下:
用户浏览器
↓
Cloudflare 边缘节点
↓
你的源站服务器
也就是说,用户访问的并不是你的服务器 IP,而是 Cloudflare 的节点 IP。Cloudflare 会代替用户去访问你的源站,然后把结果返回给用户。
这带来几个好处:
- 隐藏源站 IP;
- 抵御一部分 DDoS 攻击;
- 提供 HTTPS 证书;
- 缓存静态资源;
- 使用 WAF 过滤恶意请求;
- 提供访问控制、Bot 管理、规则转发等能力。
但也正因为 Cloudflare 处在用户和源站之间,所以如果配置错误,问题也会非常明显。
二、接入 Cloudflare 的基本流程
1. 添加站点
登录 Cloudflare 后,点击:
Add a site
输入你的域名,例如:
example.com
注意,不要输入:
https://example.com
www.example.com/path
Cloudflare 添加的是根域名,而不是完整 URL。
2. 修改域名 NS 服务器
Cloudflare 会分配两个 NS 地址,例如:
amy.ns.cloudflare.com
mark.ns.cloudflare.com
你需要去域名注册商后台,把原来的 NS 修改成 Cloudflare 提供的 NS。
常见域名注册商包括:
- GoDaddy;
- Namecheap;
- Namesilo;
- 阿里云;
- 腾讯云;
- Cloudflare Registrar。
修改 NS 后,需要等待 DNS 生效,通常几分钟到数小时,最长可能需要 24 到 48 小时。
可以使用以下命令检查 NS 是否生效:
dig NS example.com
或者:
nslookup -type=NS example.com
如果返回的是 Cloudflare 的 NS,说明域名已经成功接管。
三、DNS 解析避坑
Cloudflare DNS 页面中,每条记录旁边都有一个“小云朵”图标:
- 橙色云朵:开启 Cloudflare 代理;
- 灰色云朵:仅 DNS 解析,不经过 Cloudflare 代理。
这是很多问题的源头。
1. A 记录和 CNAME 记录怎么配置?
如果你的源站 IP 是:
1.2.3.4
你可以添加:
A example.com 1.2.3.4
A www 1.2.3.4
也可以配置:
A example.com 1.2.3.4
CNAME www example.com
如果你使用第三方平台,例如 Vercel、Netlify、GitHub Pages,通常需要配置 CNAME。
例如 GitHub Pages:
CNAME www username.github.io
Vercel 常见配置:
A example.com 76.76.21.21
CNAME www cname.vercel-dns.com
2. 哪些记录不能随便开橙色云朵?
Cloudflare 免费版代理只支持常见 HTTP/HTTPS 端口,不是所有端口都能代理。
Cloudflare 支持代理的 HTTP/HTTPS 端口包括:
HTTP:
80, 8080, 8880, 2052, 2082, 2086, 2095
HTTPS:
443, 2053, 2083, 2087, 2096, 8443
如果你的服务运行在非支持端口,例如:
http://example.com:3000
http://example.com:9000
开启橙色云朵后通常无法访问。
避坑建议:
- 网站主服务使用 80 或 443;
- 特殊端口使用灰色云朵;
- 或者通过 Nginx 反向代理到 80/443;
- 或使用 Cloudflare Tunnel 暴露服务。
3. SSH、FTP、数据库记录不要开代理
以下类型服务不建议直接开启橙色云朵:
SSH: 22
FTP: 21
MySQL: 3306
PostgreSQL: 5432
Redis: 6379
MongoDB: 27017
SMTP/IMAP/POP3 邮件服务
如果你把这些记录开成橙色云朵,通常会连接失败。
正确做法是:
A ssh.example.com 1.2.3.4 DNS only
A db.example.com 1.2.3.4 DNS only
也就是灰色云朵。
如果你希望安全访问 SSH,可以使用 Cloudflare Zero Trust 或 Cloudflare Tunnel,而不是直接把 SSH 暴露在公网。
四、SSL/TLS 配置避坑
Cloudflare 最常见的坑就是 SSL 模式配置错误。
进入:
SSL/TLS → Overview
你会看到几种模式:
Off
Flexible
Full
Full strict
1. 不建议使用 Flexible
很多新手为了省事,会选择 Flexible。它的链路是:
用户浏览器 ←HTTPS→ Cloudflare ←HTTP→ 源站
也就是说,用户到 Cloudflare 是 HTTPS,但 Cloudflare 到你的服务器是 HTTP。
这会产生几个问题:
- 源站传输不加密;
- WordPress 容易无限重定向;
- 后台判断协议错误;
- 安全性不完整;
- 某些应用生成错误的 URL。
如果你的源站没有证书,短期可用 Flexible 过渡,但不建议长期使用。
2. 推荐使用 Full strict
最佳模式是:
Full strict
链路如下:
用户浏览器 ←HTTPS→ Cloudflare ←HTTPS→ 源站
并且 Cloudflare 会验证源站证书是否合法。
源站证书可以使用:
- Let’s Encrypt 免费证书;
- Cloudflare Origin Certificate;
- 商业证书。
3. 使用 acme.sh 申请 Let’s Encrypt 证书
安装 acme.sh:
curl https://get.acme.sh | sh
加载环境变量:
source ~/.bashrc
如果没有生效,可以执行:
source ~/.profile
注册账号:
~/.acme.sh/acme.sh --register-account -m admin@example.com
使用 HTTP 验证签发证书:
~/.acme.sh/acme.sh --issue -d example.com -d www.example.com --webroot /var/www/html
安装证书:
mkdir -p /etc/nginx/ssl/example.com
~/.acme.sh/acme.sh --install-cert -d example.com \
--key-file /etc/nginx/ssl/example.com/key.pem \
--fullchain-file /etc/nginx/ssl/example.com/fullchain.pem \
--reloadcmd "systemctl reload nginx"
如果你开启了 Cloudflare 代理,HTTP 验证可能失败。此时推荐使用 DNS API 验证。
4. 使用 Cloudflare DNS API 签发通配符证书
先在 Cloudflare 后台创建 API Token:
My Profile → API Tokens → Create Token
权限建议:
Zone → DNS → Edit
Zone → Zone → Read
限制到指定域名更安全。
设置环境变量:
export CF_Token="你的Cloudflare_API_Token"
export CF_Account_ID="你的Account_ID"
签发证书:
~/.acme.sh/acme.sh --issue --dns dns_cf \
-d example.com \
-d "*.example.com"
安装证书:
mkdir -p /etc/nginx/ssl/example.com
~/.acme.sh/acme.sh --install-cert -d example.com \
--key-file /etc/nginx/ssl/example.com/key.pem \
--fullchain-file /etc/nginx/ssl/example.com/fullchain.pem \
--reloadcmd "systemctl reload nginx"
查看证书:
openssl x509 -in /etc/nginx/ssl/example.com/fullchain.pem -noout -dates
五、Nginx 配合 Cloudflare 的正确姿势
假设你的网站目录是:
/var/www/example.com
可以创建 Nginx 配置:
nano /etc/nginx/sites-available/example.com
写入:
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html index.htm index.php;
ssl_certificate /etc/nginx/ssl/example.com/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/example.com/key.pem;
location / {
try_files $uri $uri/ /index.html;
}
}
启用站点:
ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
测试配置:
nginx -t
重载 Nginx:
systemctl reload nginx
六、真实 IP 获取避坑
开启 Cloudflare 代理后,源站看到的访问 IP 通常是 Cloudflare 节点 IP,而不是用户真实 IP。
这会导致:
- 日志里全是 Cloudflare IP;
- 程序限流失效;
- 后台安全策略误判;
- fail2ban 无法正确封禁攻击者;
- WordPress 评论 IP 不准确。
Cloudflare 会通过请求头传递真实 IP:
CF-Connecting-IP
你需要在 Nginx 中配置真实 IP。
1. 获取 Cloudflare IP 段
Cloudflare 官方 IP 列表:
curl -s https://www.cloudflare.com/ips-v4
curl -s https://www.cloudflare.com/ips-v6
2. Nginx 配置真实 IP
创建配置文件:
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;
测试并重载:
nginx -t
systemctl reload nginx
查看日志验证:
tail -f /var/log/nginx/access.log
七、防止源站 IP 泄露
Cloudflare 代理虽然可以隐藏源站 IP,但前提是你的源站没有被其他方式暴露。
常见泄露方式包括:
- 历史 DNS 记录;
- 子域名未走 Cloudflare;
- 邮件服务器和网站同 IP;
- GitHub、配置文件、环境变量泄露;
- SSL 证书透明日志;
- 源站直接允许任意 IP 访问;
- 旧域名或测试域名指向源站。
1. 源站防火墙只允许 Cloudflare IP
如果你的网站只需要通过 Cloudflare 访问,建议在源站防火墙中只允许 Cloudflare IP 访问 80 和 443。
以下是 UFW 示例。
先安装 UFW:
apt update
apt install ufw -y
默认拒绝入站:
ufw default deny incoming
ufw default allow outgoing
允许 SSH,避免把自己锁在外面:
ufw allow 22/tcp
允许 Cloudflare IPv4:
for ip in $(curl -s https://www.cloudflare.com/ips-v4); do
ufw allow from $ip to any port 80 proto tcp
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
ufw allow from $ip to any port 80 proto tcp
ufw allow from $ip to any port 443 proto tcp
done
启用 UFW:
ufw enable
查看状态:
ufw status verbose
2. iptables 限制 Cloudflare IP
如果你使用 iptables,也可以这样配置:
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
允许 Cloudflare 访问 80/443:
for ip in $(curl -s https://www.cloudflare.com/ips-v4); do
iptables -A INPUT -p tcp -s $ip --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -s $ip --dport 443 -j ACCEPT
done
保存规则:
apt install iptables-persistent -y
netfilter-persistent save
注意:操作防火墙前一定要确认 SSH 已经放行,否则可能把自己锁在服务器外面。
八、缓存配置避坑
Cloudflare 默认主要缓存静态资源,例如:
CSS
JS
图片
字体
视频
但 HTML 页面默认不一定缓存,具体取决于响应头和配置。
1. 缓存导致页面不更新
如果你更新了网站,但用户看到的还是旧内容,可能是 Cloudflare 缓存导致的。
可以进入:
Caching → Configuration → Purge Cache
清除缓存。
也可以用 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}'
只清理指定文件:
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/app.js"
]
}'
2. 后台和 API 不要乱缓存
如果你的网站有后台,例如:
/wp-admin
/admin
/api
/user
/login
不要对这些路径设置“Cache Everything”。
否则可能出现:
- 用户 A 看到用户 B 的页面;
- 登录状态异常;
- API 数据不更新;
- 后台提交失败;
- CSRF Token 错误。
建议添加 Cache Rule:
If URL path starts with /api
Then Cache eligibility: Bypass cache
常见应绕过缓存的路径:
/wp-admin*
/wp-login.php*
/api/*
/admin/*
/user/*
/account/*
/checkout/*
/cart/*
九、WAF 和安全规则避坑
Cloudflare WAF 很强,但误伤也很常见。尤其是:
- 后台上传文件;
- API POST 请求;
- WordPress 插件;
- Webhook 回调;
- 支付回调;
- GitHub Actions 部署回调。
如果规则过严,用户会看到:
403 Forbidden
Error 1020 Access denied
1. 查看 Cloudflare 安全事件
进入:
Security → Events
查看被拦截请求,重点看:
- IP;
- 国家;
- URI;
- 触发的规则;
- Action;
- User-Agent。
2. 给后台增加安全规则
你可以给后台路径添加访问限制,例如只允许自己的 IP 访问后台:
If URI Path starts with /admin
And IP Source Address is not 我的IP
Then Block
如果你的 IP 经常变化,可以使用 Cloudflare Access 或者添加验证码挑战:
If URI Path starts with /admin
Then Managed Challenge
3. 放行 Webhook
例如 GitHub Webhook 路径:
/deploy/github
可以添加规则:
If URI Path equals /deploy/github
Then Skip WAF managed rules
但不要无脑放行整个网站,否则 WAF 就失去了意义。
十、Always Use HTTPS 和重定向避坑
Cloudflare 提供:
Always Use HTTPS
Automatic HTTPS Rewrites
建议开启:
Always Use HTTPS: On
Automatic HTTPS Rewrites: On
但需要注意,如果源站 Nginx 也配置了复杂跳转,可能造成重复跳转或死循环。
典型死循环场景:
- Cloudflare SSL 使用 Flexible;
- 源站强制 HTTP 跳 HTTPS;
- Cloudflare 到源站仍然使用 HTTP;
- 源站继续跳 HTTPS;
- 无限循环。
解决方法:
- Cloudflare SSL 改为 Full strict;
- 源站安装有效证书;
- Nginx 正确监听 443;
- 避免多处重复配置冲突。
检查跳转链:
curl -I http://example.com
curl -I https://example.com
查看详细跳转:
curl -IL http://example.com
十一、Cloudflare Tunnel 使用避坑
Cloudflare Tunnel 可以让你不用开放公网端口,也能把内网服务暴露到公网。它非常适合:
- 家庭服务器;
- NAS;
- 本地开发环境;
- 没有公网 IP 的服务器;
- 临时演示服务。
1. 安装 cloudflared
Debian/Ubuntu 安装:
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.deb
dpkg -i cloudflared.deb
检查版本:
cloudflared --version
登录 Cloudflare:
cloudflared tunnel login
创建 Tunnel:
cloudflared tunnel create my-tunnel
查看 Tunnel:
cloudflared tunnel list
2. 配置 Tunnel
创建配置目录:
mkdir -p ~/.cloudflared
编辑配置文件:
nano ~/.cloudflared/config.yml
示例:
tunnel: my-tunnel
credentials-file: /root/.cloudflared/你的隧道ID.json
ingress:
- hostname: app.example.com
service: http://localhost:3000
- hostname: nas.example.com
service: http://192.168.1.10:5000
- service: http_status:404
创建 DNS 路由:
cloudflared tunnel route dns my-tunnel app.example.com
cloudflared tunnel route dns my-tunnel nas.example.com
运行:
cloudflared tunnel run my-tunnel
安装为系统服务:
cloudflared service install
systemctl enable cloudflared
systemctl start cloudflared
systemctl status cloudflared
3. Tunnel 常见坑
坑一:忘记最后的 404 规则
配置文件中建议保留:
- service: http_status:404
否则未匹配的请求可能出现意外行为。
坑二:服务地址写错
如果本地服务监听在:
127.0.0.1:3000
配置应写:
service: http://localhost:3000
如果服务在局域网其他机器上:
service: http://192.168.1.10:5000
坑三:没有给后台加 Access
Tunnel 只是隐藏了源站入口,不代表应用本身安全。后台、NAS、管理面板建议配合 Cloudflare Access 使用身份验证。
十二、WordPress 使用 Cloudflare 避坑
WordPress 和 Cloudflare 搭配非常常见,但也经常出现问题。
1. WordPress 无限重定向
如果出现:
ERR_TOO_MANY_REDIRECTS
优先检查:
SSL/TLS 模式是否为 Flexible
建议改为:
Full strict
并确保源站证书正常。
2. WordPress 获取真实 IP
可以安装 Cloudflare 官方插件,也可以在 Nginx 层配置真实 IP。建议优先在服务器层处理。
3. 不要缓存后台
Cloudflare Cache Rule 中应排除:
/wp-admin*
/wp-login.php*
/wp-json/*
尤其是 /wp-json/,很多插件、编辑器、接口都依赖它。
4. WooCommerce 特别注意
如果你使用 WooCommerce,不要缓存:
/cart/*
/checkout/*
/my-account/*
/wc-api/*
否则容易出现购物车混乱、订单异常、登录状态错误等严重问题。
十三、国内访问速度避坑
很多人以为用了 Cloudflare,国内访问一定会变快。实际上,Cloudflare 免费版在中国大陆访问速度并不稳定,甚至在某些地区可能变慢。
原因包括:
- 免费版节点线路不一定适合国内;
- 运营商路由波动;
- TLS 握手延迟;
- 高峰期拥堵;
- 部分节点被干扰或质量不稳定。
如果你的主要用户在中国大陆,可以考虑:
- 使用国内 CDN;
- 使用香港、日本、新加坡等优质源站;
- 使用付费 CDN;
- 静态资源分流;
- 国内外分线路解析;
- 不要盲目开启 Cloudflare 代理。
测试访问延迟:
ping example.com
测试 HTTP 延迟:
curl -o /dev/null -s -w "DNS:%{time_namelookup} Connect:%{time_connect} TLS:%{time_appconnect} TTFB:%{time_starttransfer} Total:%{time_total}\n" https://example.com
路由追踪:
traceroute example.com
如果没有 traceroute:
apt install traceroute -y
十四、常用 Cloudflare API 命令
1. 获取 Zone ID
curl -X GET "https://api.cloudflare.com/client/v4/zones?name=example.com" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json"
2. 查询 DNS 记录
curl -X GET "https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json"
3. 添加 A 记录
curl -X POST "https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
--data '{
"type": "A",
"name": "example.com",
"content": "1.2.3.4",
"ttl": 1,
"proxied": true
}'
说明:
ttl: 1 表示自动
proxied: true 表示开启橙色云朵
proxied: false 表示仅 DNS
4. 添加 CNAME 记录
curl -X POST "https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
--data '{
"type": "CNAME",
"name": "www",
"content": "example.com",
"ttl": 1,
"proxied": true
}'
5. 删除 DNS 记录
先查询记录 ID:
curl -X GET "https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records?name=www.example.com" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json"
删除:
curl -X DELETE "https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records/RECORD_ID" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json"
6. 更新 DNS 记录
curl -X PUT "https://api.cloudflare.com/client/v4/zones/ZONE_ID/dns_records/RECORD_ID" \
-H "Authorization: Bearer YOUR_API_TOKEN" \
-H "Content-Type: application/json" \
--data '{
"type": "A",
"name": "example.com",
"content": "5.6.7.8",
"ttl": 1,
"proxied": true
}'
十五、排障常用命令清单
1. 检查 DNS
dig example.com
dig www.example.com
dig A example.com
dig AAAA example.com
dig CNAME www.example.com
dig NS example.com
指定 DNS 服务器查询:
dig @1.1.1.1 example.com
dig @8.8.8.8 example.com
2. 检查 HTTP 状态
curl -I https://example.com
查看详细请求:
curl -v https://example.com
查看跳转链:
curl -IL https://example.com
3. 检查证书
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
4. 检查 Nginx
nginx -t
systemctl status nginx
systemctl reload nginx
journalctl -u nginx -n 100 --no-pager
查看日志:
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
5. 检查端口监听
ss -tulpen
只看 80 和 443:
ss -tulpen | grep -E ':80|:443'
6. 检查防火墙
UFW:
ufw status verbose
iptables:
iptables -L -n -v
十六、常见错误码说明
1. Error 521
Web server is down
常见原因:
- 源站没有启动;
- 80/443 端口未开放;
- 防火墙拦截 Cloudflare;
- Nginx/Apache 未监听对应端口。
排查:
systemctl status nginx
ss -tulpen | grep -E ':80|:443'
ufw status verbose
2. Error 522
Connection timed out
常见原因:
- 源站网络不通;
- 防火墙丢弃请求;
- 服务器负载过高;
- Cloudflare 到源站连接超时。
排查:
top
free -h
df -h
journalctl -u nginx -n 100 --no-pager
3. Error 525
SSL handshake failed
常见原因:
- 源站 SSL 配置错误;
- 证书不匹配;
- Nginx 没有正确配置 443;
- SSL/TLS 版本不兼容。
排查:
openssl s_client -connect example.com:443 -servername example.com
nginx -t
4. Error 526
Invalid SSL certificate
通常发生在 Cloudflare 使用 Full strict,但源站证书无效。
解决:
- 安装有效证书;
- 检查证书域名是否匹配;
- 检查证书是否过期;
- 使用 Cloudflare Origin Certificate。
5. Error 1020
Access denied
通常是 Cloudflare 防火墙规则拦截。
排查:
Security → Events
查看是哪条规则拦截了请求。
十七、最佳实践总结
最后给一份简明配置建议:
- 网站主域名开启橙色云朵,SSH、数据库、邮件服务不要开启代理。
- SSL/TLS 使用 Full strict,不建议长期使用 Flexible。
- 源站必须配置 HTTPS 证书。
- Nginx 配置真实 IP,否则日志和限流都会不准确。
- 源站防火墙只允许 Cloudflare IP 访问 80/443。
- 后台、登录、购物车、API 路径不要缓存。
- WAF 不要一上来开太狠,先观察 Security Events。
- Cloudflare Tunnel 后台服务要配合 Access 使用。
- 国内用户为主时,不要默认认为 Cloudflare 一定更快。
- 操作 DNS 和防火墙前做好回滚方案。
结语
Cloudflare 是一个非常强大的平台,但它不是“点一下就万事大吉”的工具。它的优势在于把 DNS、CDN、安全、证书、访问控制和边缘网络整合到一起;它的风险也在于,一旦配置不当,DNS、SSL、缓存、防火墙、源站安全都会互相影响。
如果你是个人站长,建议先掌握 DNS、SSL、缓存、真实 IP、防火墙这几个核心点;如果你是企业运维,则应进一步结合 WAF、Access、日志分析、API 自动化和变更管理来使用 Cloudflare。
用好 Cloudflare 的关键不是开启所有功能,而是明确每个功能解决什么问题,以及它可能带来什么副作用。只有这样,Cloudflare 才能真正成为你的网站加速器和安全屏障,而不是新的故障来源。