Cloudflare 防护实战:从隐藏源站到 WAF 规则的完整加固配置指南
Cloudflare 安全加固方案|附配置文件
在现代 Web 架构中,Cloudflare 已经不仅仅是一个 CDN 服务商,它同时承担着 DNS 解析、边缘缓存、DDoS 防护、WAF 防火墙、Bot 管理、访问控制、TLS 加密、Zero Trust 安全接入等多种职责。对于大多数网站、API 服务、SaaS 平台以及企业业务系统而言,合理配置 Cloudflare,往往可以在不大幅改造源站架构的前提下,显著提升整体安全性与可用性。
本文将围绕 Cloudflare 的安全加固思路展开,介绍从 DNS、SSL/TLS、WAF、防火墙规则、速率限制、Bot 防护、源站保护、缓存策略、HTTP 安全响应头到监控告警等多个方面的配置建议,并附上常用配置文件示例,方便直接参考落地。
一、Cloudflare 安全加固目标
在开始配置之前,需要明确 Cloudflare 安全加固的核心目标:
-
隐藏真实源站 IP
- 避免攻击者绕过 Cloudflare 直接攻击源站。
- 降低源站被扫描、爆破、DDoS 的风险。
-
拦截恶意请求
- 阻止常见 Web 攻击,例如 SQL 注入、XSS、路径穿越、命令注入等。
- 对异常请求进行质询、阻断或限速。
-
提升访问安全性
- 强制 HTTPS。
- 启用安全 TLS 版本。
- 添加 HSTS、CSP、X-Frame-Options 等安全响应头。
-
降低自动化攻击风险
- 缓解恶意爬虫、撞库、爆破、接口刷量。
- 对登录、注册、搜索、支付等敏感接口进行速率限制。
-
增强源站访问控制
- 只允许 Cloudflare 回源 IP 访问源站。
- 对后台路径或内部系统启用 Zero Trust 访问控制。
-
提升可观测性
- 通过日志、告警、分析报表发现异常流量。
- 建立持续安全运营机制。
二、DNS 与代理配置加固
Cloudflare 的第一道防线是 DNS。很多网站接入 Cloudflare 后,虽然开启了 CDN 代理,但仍然因为历史 DNS 记录、邮件记录、子域名记录等原因泄露源站 IP。
1. 开启代理模式
在 Cloudflare DNS 管理中,对于对外提供 Web 服务的记录,应启用代理状态,也就是橙色云朵。
例如:
| 类型 | 名称 | 内容 | 代理状态 |
|---|---|---|---|
| A | example.com |
203.0.113.10 |
已代理 |
| A | www |
203.0.113.10 |
已代理 |
| CNAME | api |
example.com |
已代理 |
如果是邮件服务相关记录,例如 MX、mail、smtp、imap 等,通常不应开启代理,因为 Cloudflare 默认并不代理邮件协议。
2. 避免源站 IP 泄露
需要重点检查以下位置:
- 历史 DNS 记录;
mail.example.com是否与 Web 源站共用 IP;- 子域名是否直接指向源站;
- GitHub、文档、代码仓库中是否写死源站 IP;
- SSL 证书透明日志中是否暴露内部域名;
- Shodan、Censys 等互联网资产搜索引擎是否已收录源站。
建议 Web 服务和邮件服务分离服务器,避免通过邮件服务反查到 Web 源站 IP。
3. 使用 Cloudflare Origin CA 证书
Cloudflare Origin CA 证书只被 Cloudflare 信任,不适合浏览器直接访问,但非常适合用于 Cloudflare 到源站之间的加密连接。
这样即使攻击者直接访问源站 IP,也可能因为证书不受浏览器信任或 SNI 不匹配而失败。
三、SSL/TLS 安全配置
进入 Cloudflare 控制台:
SSL/TLS → Overview
建议配置如下:
| 配置项 | 推荐值 |
|---|---|
| SSL/TLS 加密模式 | Full Strict |
| Always Use HTTPS | 开启 |
| Automatic HTTPS Rewrites | 开启 |
| Minimum TLS Version | TLS 1.2 或 TLS 1.3 |
| TLS 1.3 | 开启 |
| Opportunistic Encryption | 开启 |
| Authenticated Origin Pulls | 建议开启 |
| HSTS | 谨慎开启,确认全站 HTTPS 后启用 |
1. 为什么选择 Full Strict
Cloudflare 提供多种 SSL 模式:
- Off:不加密,不推荐。
- Flexible:用户到 Cloudflare 是 HTTPS,Cloudflare 到源站是 HTTP,不推荐。
- Full:Cloudflare 到源站加密,但不严格校验证书。
- Full Strict:Cloudflare 到源站加密,并严格校验证书,推荐。
生产环境应尽量使用 Full Strict。源站可以使用以下证书:
- Let’s Encrypt;
- 商业 SSL 证书;
- Cloudflare Origin CA 证书。
2. HSTS 配置建议
HSTS 可以强制浏览器未来只通过 HTTPS 访问网站,提升安全性,但配置错误可能导致业务长时间无法通过 HTTP 回退。
推荐配置:
max-age=15552000; includeSubDomains; preload
说明:
max-age=15552000:有效期 180 天;includeSubDomains:对子域名也生效;preload:可申请加入浏览器 HSTS 预加载列表。
如果是首次启用,建议先设置较短时间,例如:
max-age=86400
确认无问题后再逐步增加。
四、WAF 托管规则配置
Cloudflare WAF 是非常重要的安全能力,建议至少启用以下托管规则集:
- Cloudflare Managed Ruleset
- OWASP Core Ruleset
- Cloudflare Exposed Credentials Check
- Cloudflare PHP Ruleset,如果业务使用 PHP
- Cloudflare WordPress Ruleset,如果业务使用 WordPress
推荐策略
| 规则集 | 建议动作 |
|---|---|
| Cloudflare Managed Ruleset | Block 或 Managed Challenge |
| OWASP Core Ruleset | 默认先设置为 Log 或 Managed Challenge |
| Exposed Credentials Check | Block |
| WordPress Ruleset | Managed Challenge / Block |
| PHP Ruleset | Block |
对于新接入业务,建议先观察 3 到 7 天日志,确认误报情况,再逐步从 Log 调整到 Managed Challenge 或 Block。
五、防火墙规则加固方案
Cloudflare 自定义规则可以按照 URI、国家地区、ASN、IP、请求方法、Header、Cookie、User-Agent 等条件进行控制。
下面给出几类常用规则示例。
1. 阻止高风险国家或地区访问后台
适用场景:后台管理系统只面向国内员工或指定地区使用。
Cloudflare 表达式示例:
(http.request.uri.path contains "/admin" and not ip.geoip.country in {"CN" "HK" "SG"})
动作建议:
Block
如果担心误封,可以使用:
Managed Challenge
2. 保护登录接口
登录接口是撞库、爆破攻击的高发区域。
表达式示例:
(http.request.uri.path eq "/login" and http.request.method eq "POST")
动作建议:
Managed Challenge
也可以结合速率限制,例如每个 IP 1 分钟内最多 10 次。
3. 阻止危险请求方法
普通网站一般只需要 GET、POST、HEAD。除非业务明确需要 PUT、DELETE、PATCH,否则可以阻断。
表达式示例:
not http.request.method in {"GET" "POST" "HEAD"}
动作建议:
Block
4. 阻止常见扫描路径
很多扫描器会探测 .env、备份文件、Git 目录、配置文件等。
表达式示例:
(
http.request.uri.path contains "/.env" or
http.request.uri.path contains "/.git" or
http.request.uri.path contains "/wp-config.php" or
http.request.uri.path contains "/config.php" or
http.request.uri.path contains "/backup" or
http.request.uri.path contains ".sql" or
http.request.uri.path contains ".bak" or
http.request.uri.path contains ".old"
)
动作建议:
Block
5. 对后台路径仅允许指定 IP
如果公司有固定公网出口 IP,可以对白名单以外的访问直接阻断。
表达式示例:
(http.request.uri.path contains "/admin" and not ip.src in {203.0.113.8 198.51.100.10})
动作建议:
Block
6. 阻止恶意 User-Agent
一些低质量扫描器会使用明显异常的 User-Agent。
表达式示例:
(
http.user_agent contains "sqlmap" or
http.user_agent contains "acunetix" or
http.user_agent contains "nikto" or
http.user_agent contains "masscan" or
http.user_agent contains "nmap" or
http.user_agent contains "zgrab" or
http.user_agent contains "python-requests" or
http.user_agent contains "curl"
)
动作建议:
Managed Challenge
注意:curl、python-requests 也可能被正常接口调用使用,不建议一刀切 Block,除非业务确认不需要。
六、速率限制配置
速率限制适合保护登录、注册、短信验证码、搜索、评论、上传、支付回调等接口。
1. 登录接口限速
规则示例:
(http.request.uri.path eq "/login" and http.request.method eq "POST")
建议限制:
10 requests / 1 minute / IP
超过后动作:
Managed Challenge 或 Block 10 minutes
2. 短信验证码接口限速
规则示例:
(http.request.uri.path eq "/api/sms/send" and http.request.method eq "POST")
建议限制:
3 requests / 10 minutes / IP
超过后动作:
Block 30 minutes
如果业务允许,可以同时按手机号、账号、设备指纹在应用层继续限速。
3. 搜索接口限速
搜索接口容易被爬虫刷爆数据库。
规则示例:
(http.request.uri.path starts_with "/search")
建议限制:
60 requests / 1 minute / IP
超过后动作:
Managed Challenge
七、Bot 防护与浏览器完整性检查
Cloudflare 提供多种 Bot 防护能力。免费版也可以使用部分基础功能,例如:
- Bot Fight Mode;
- Browser Integrity Check;
- Security Level;
- Challenge Passage。
推荐配置
| 配置项 | 推荐值 |
|---|---|
| Bot Fight Mode | 开启 |
| Browser Integrity Check | 开启 |
| Security Level | Medium 或 High |
| Challenge Passage | 30 分钟至 1 小时 |
对于登录、注册、评论、搜索等接口,可以配合 Managed Challenge。对于 API 服务,不建议直接对所有请求启用 JS Challenge,否则可能影响正常客户端调用。
八、源站服务器加固
Cloudflare 只能保护经过 Cloudflare 的流量。如果攻击者掌握源站 IP,并且源站没有访问控制,那么仍然可以绕过 Cloudflare 直接攻击。
因此,源站必须配置防火墙,只允许 Cloudflare IP 段访问 80 和 443 端口。
九、Nginx 源站配置示例
以下是一个适用于 Cloudflare 回源的 Nginx HTTPS 配置示例。
文件路径:
/etc/nginx/conf.d/example.com.conf
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;
ssl_certificate /etc/ssl/cloudflare/example.com.pem;
ssl_certificate_key /etc/ssl/cloudflare/example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
real_ip_header CF-Connecting-IP;
# Cloudflare IPv4
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;
# Cloudflare IPv6
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;
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log warn;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;
add_header Strict-Transport-Security "max-age=15552000; includeSubDomains; preload" always;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $realip_remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
location ~ /\.(git|svn|hg) {
deny all;
}
location ~* \.(env|bak|old|sql|tar|gz|zip)$ {
deny all;
}
}
十、源站防火墙配置示例
1. UFW 配置示例
适用于 Ubuntu / Debian 系统。
文件:
cloudflare-ufw.sh
#!/bin/bash
ufw --force reset
ufw default deny incoming
ufw default allow outgoing
# SSH 仅允许指定管理 IP
ufw allow from 203.0.113.8 to any port 22 proto tcp
# Cloudflare IPv4
for ip in \
173.245.48.0/20 \
103.21.244.0/22 \
103.22.200.0/22 \
103.31.4.0/22 \
141.101.64.0/18 \
108.162.192.0/18 \
190.93.240.0/20 \
188.114.96.0/20 \
197.234.240.0/22 \
198.41.128.0/17 \
162.158.0.0/15 \
104.16.0.0/13 \
104.24.0.0/14 \
172.64.0.0/13 \
131.0.72.0/22
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 \
2400:cb00::/32 \
2606:4700::/32 \
2803:f800::/32 \
2405:b500::/32 \
2405:8100::/32 \
2a06:98c0::/29 \
2c0f:f248::/32
do
ufw allow from $ip to any port 80 proto tcp
ufw allow from $ip to any port 443 proto tcp
done
ufw --force enable
ufw status verbose
执行:
chmod +x cloudflare-ufw.sh
sudo ./cloudflare-ufw.sh
2. firewalld 配置示例
适用于 CentOS / Rocky Linux / AlmaLinux。
文件:
cloudflare-firewalld.sh
#!/bin/bash
firewall-cmd --permanent --remove-service=http
firewall-cmd --permanent --remove-service=https
# SSH 仅允许管理 IP
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.8" port protocol="tcp" port="22" accept'
CF_IPV4=(
"173.245.48.0/20"
"103.21.244.0/22"
"103.22.200.0/22"
"103.31.4.0/22"
"141.101.64.0/18"
"108.162.192.0/18"
"190.93.240.0/20"
"188.114.96.0/20"
"197.234.240.0/22"
"198.41.128.0/17"
"162.158.0.0/15"
"104.16.0.0/13"
"104.24.0.0/14"
"172.64.0.0/13"
"131.0.72.0/22"
)
for ip in "${CF_IPV4[@]}"; do
firewall-cmd --permanent --add-rich-rule="rule family=\"ipv4\" source address=\"$ip\" port protocol=\"tcp\" port=\"80\" accept"
firewall-cmd --permanent --add-rich-rule="rule family=\"ipv4\" source address=\"$ip\" port protocol=\"tcp\" port=\"443\" accept"
done
CF_IPV6=(
"2400:cb00::/32"
"2606:4700::/32"
"2803:f800::/32"
"2405:b500::/32"
"2405:8100::/32"
"2a06:98c0::/29"
"2c0f:f248::/32"
)
for ip in "${CF_IPV6[@]}"; do
firewall-cmd --permanent --add-rich-rule="rule family=\"ipv6\" source address=\"$ip\" port protocol=\"tcp\" port=\"80\" accept"
firewall-cmd --permanent --add-rich-rule="rule family=\"ipv6\" source address=\"$ip\" port protocol=\"tcp\" port=\"443\" accept"
done
firewall-cmd --reload
firewall-cmd --list-all
十一、HTTP 安全响应头配置
HTTP 安全响应头可以有效降低浏览器侧攻击风险。推荐至少配置以下内容:
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "geolocation=(), microphone=(), camera=()" always;
add_header Strict-Transport-Security "max-age=15552000; includeSubDomains; preload" always;
如果业务允许,可以进一步配置 CSP:
add_header Content-Security-Policy "default-src 'self'; img-src 'self' data: https:; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; frame-ancestors 'self';" always;
需要注意,CSP 很容易影响前端资源加载,尤其是第三方统计、验证码、客服插件、支付 SDK 等。因此建议先使用 Content-Security-Policy-Report-Only 模式观察,再正式启用。
十二、Cloudflare 页面规则与缓存策略
缓存不仅影响性能,也影响安全。错误缓存可能导致敏感数据泄露,例如把用户个人中心页面缓存给其他用户。
1. 静态资源缓存
适合缓存:
- CSS;
- JS;
- 图片;
- 字体;
- 下载文件;
- 静态 HTML 页面。
示例规则:
URI Path ends with .css
URI Path ends with .js
URI Path ends with .png
URI Path ends with .jpg
URI Path ends with .webp
URI Path ends with .woff2
动作:
Cache Eligible
Edge TTL: 1 month
Browser TTL: 1 week
2. 敏感路径禁止缓存
以下路径建议绕过缓存:
/login
/logout
/register
/admin
/user
/account
/cart
/checkout
/api
规则示例:
http.request.uri.path starts_with "/api" or
http.request.uri.path starts_with "/admin" or
http.request.uri.path starts_with "/account"
动作:
Bypass Cache
十三、Zero Trust 保护后台
对于管理后台、内部系统、运维面板,建议不要仅依赖用户名密码。可以使用 Cloudflare Zero Trust Access 增加一层身份验证。
适合保护的路径:
/admin/wp-admin/dashboard/grafana/kibana/jenkins/phpmyadmin
策略示例:
Application: admin.example.com
Policy:
Allow emails ending with @example.com
Require OTP or SSO
Session duration: 8 hours
如果企业已经使用 Google Workspace、Azure AD、Okta、GitHub Organization 等身份源,可以直接接入 Cloudflare Access,实现基于身份的访问控制。
十四、日志与告警
安全加固不是一次性工作,而是持续运营过程。建议定期查看:
- Security Events;
- WAF 命中日志;
- Bot 请求趋势;
- 速率限制命中情况;
- 源站 4xx / 5xx;
- 带宽突增;
- 异常国家地区访问;
- 登录失败次数;
- API 错误率。
如果使用企业版或日志推送能力,可以将 Cloudflare Logs 推送到:
- S3;
- BigQuery;
- Elasticsearch;
- Splunk;
- Datadog;
- SIEM 平台。
常见告警条件:
5 分钟内 403 数量突增
5 分钟内 WAF Block 数量突增
登录接口请求量超过基线 3 倍
单 IP 请求数超过正常阈值
源站 5xx 错误率超过 5%
十五、推荐的 Cloudflare 安全基线清单
| 类别 | 配置项 | 推荐状态 |
|---|---|---|
| DNS | Web 记录开启代理 | 开启 |
| DNS | 源站 IP 不公开 | 必须 |
| SSL/TLS | Full Strict | 开启 |
| SSL/TLS | TLS 1.3 | 开启 |
| SSL/TLS | 最低 TLS 1.2 | 开启 |
| SSL/TLS | Always Use HTTPS | 开启 |
| WAF | Managed Rules | 开启 |
| WAF | OWASP Rules | 开启 |
| Firewall | 危险路径阻断 | 开启 |
| Firewall | 后台 IP 白名单 | 推荐 |
| Rate Limit | 登录接口限速 | 开启 |
| Rate Limit | 验证码接口限速 | 开启 |
| Bot | Bot Fight Mode | 开启 |
| Origin | 仅允许 Cloudflare IP 回源 | 必须 |
| Header | HSTS | 谨慎开启 |
| Header | X-Frame-Options | 开启 |
| Header | CSP | 推荐 |
| Logs | 安全事件审计 | 开启 |
十六、上线前检查
在正式切换生产流量前,建议执行以下检查:
- DNS 是否全部正确代理;
- 源站证书是否有效;
- SSL 模式是否为 Full Strict;
- HTTP 是否自动跳转 HTTPS;
- 后台、API、登录、支付流程是否正常;
- WAF 是否存在明显误报;
- 真实客户端 IP 是否正确写入日志;
- 源站防火墙是否只允许 Cloudflare 回源;
- 静态资源是否正常缓存;
- 敏感接口是否未被缓存;
- Zero Trust 是否不影响管理员登录;
- 告警是否已配置。
结语
Cloudflare 的安全能力非常强,但真正的防护效果取决于配置是否合理。对于大多数网站来说,最关键的安全措施包括:开启 Full Strict、隐藏源站 IP、只允许 Cloudflare 回源、启用 WAF、保护登录接口、限制高频请求、合理配置缓存以及持续查看安全日志。
需要注意的是,任何安全规则都不应盲目“一刀切”。建议先从日志观察开始,根据业务特点逐步调整策略。对于核心业务系统,可以采用“Cloudflare WAF + Zero Trust + 源站防火墙 + 应用层鉴权 + 日志审计”的组合方案,构建多层防御体系。
只要配置得当,Cloudflare 不仅可以显著提升网站访问速度,也能成为抵御自动化攻击、恶意扫描、DDoS 攻击和常见 Web 漏洞利用的重要安全边界。