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

Cloudflare 避坑手册:DNS、SSL、真实 IP 与 Nginx 配置一次讲清

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

Cloudflare 常见问题汇总|附配置文件

Cloudflare 是目前使用最广泛的 CDN、DNS、WAF 与边缘安全服务平台之一。它可以帮助网站实现域名解析托管、全球加速、HTTPS 证书管理、DDoS 防护、缓存优化、访问控制、Zero Trust 安全访问等功能。对于个人站长、企业官网、API 服务、SaaS 平台以及跨境业务来说,Cloudflare 都是一个非常常见的基础设施组件。

不过,在实际使用过程中,很多人会遇到诸如「域名无法解析」「SSL 证书错误」「源站获取不到真实 IP」「缓存不生效」「网站 522/523/525 报错」「规则配置混乱」等问题。本文整理了一份 Cloudflare 常见问题汇总,并附带常用配置文件示例,方便快速排查和部署。


一、Cloudflare 是什么?

Cloudflare 可以简单理解为位于用户和源站服务器之间的一层代理服务。

当用户访问你的网站时,请求流程通常如下:

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

如果开启了 Cloudflare 代理,也就是 DNS 记录中的小云朵为橙色,用户访问网站时并不会直接访问你的服务器 IP,而是先访问 Cloudflare 的边缘节点,再由 Cloudflare 转发请求到源站。

这种模式带来的好处包括:

  1. 隐藏源站 IP,提高安全性;
  2. 提供 CDN 缓存,加快静态资源访问;
  3. 自动签发和管理 HTTPS 证书;
  4. 提供 DDoS 防护和 WAF 防火墙;
  5. 支持页面规则、缓存规则、重定向规则;
  6. 可使用 Workers、Pages、R2、Zero Trust 等扩展服务。

二、Cloudflare DNS 常见问题

1. DNS 记录中的橙色云朵和灰色云朵有什么区别?

Cloudflare 的 DNS 记录有两种状态:

状态 含义 说明
橙色云朵 Proxied,开启代理 流量经过 Cloudflare,可使用 CDN、WAF、SSL 等功能
灰色云朵 DNS only,仅 DNS 解析 只负责解析域名,不代理流量

例如:

A     example.com      1.2.3.4      Proxied
CNAME www              example.com  Proxied

如果开启橙色云朵,访问者看到的是 Cloudflare 节点 IP,而不是源站 IP。

如果你使用的是邮箱、FTP、SSH、某些非 HTTP 服务,通常需要设置为灰色云朵,因为 Cloudflare 免费版主要代理 HTTP/HTTPS 流量。


2. 修改 DNS 后为什么没有立即生效?

DNS 生效需要时间,主要受以下因素影响:

  1. 本地 DNS 缓存;
  2. 运营商 DNS 缓存;
  3. 浏览器缓存;
  4. 原 DNS 服务商 TTL;
  5. Cloudflare 记录自身的 TTL 设置。

一般来说,Cloudflare DNS 修改后在平台内部生效很快,但全球递归 DNS 同步可能需要几分钟到数小时。

可以使用以下命令检查解析结果:

dig example.com
dig example.com @1.1.1.1
dig example.com @8.8.8.8
nslookup example.com

如果你想检查指定记录:

dig A example.com @1.1.1.1
dig CNAME www.example.com @1.1.1.1

3. 为什么添加 CNAME 后提示冲突?

DNS 中同一个主机名不能同时存在某些类型冲突记录。例如:

www.example.com 已经有 A 记录

此时再添加:

www.example.com CNAME example.com

就会发生冲突。

解决方式是删除原来的 A 记录,再添加 CNAME,或者保留 A 记录,不使用 CNAME。

常见推荐配置如下:

A      example.com      1.2.3.4
CNAME  www              example.com

也就是说,根域名使用 A 记录,www 子域名使用 CNAME 指向根域名。


三、Cloudflare SSL/TLS 常见问题

1. Flexible、Full、Full Strict 有什么区别?

Cloudflare 的 SSL/TLS 模式非常重要,配置错误容易导致 HTTPS 异常。

模式 浏览器到 Cloudflare Cloudflare 到源站 适用场景
Off HTTP HTTP 不推荐
Flexible HTTPS HTTP 源站无证书时临时使用
Full HTTPS HTTPS 源站有证书,但不校验证书有效性
Full Strict HTTPS HTTPS 源站有有效证书,推荐使用

推荐使用:

Full (strict)

源站服务器可以使用以下证书:

  1. Let's Encrypt 证书;
  2. Cloudflare Origin Certificate;
  3. 商业 CA 证书。

2. 为什么开启 HTTPS 后出现重定向循环?

重定向循环通常发生在以下情况:

Cloudflare SSL 模式:Flexible
源站配置:强制 HTTP 跳转 HTTPS

请求流程如下:

  1. 用户访问 HTTPS;
  2. Cloudflare 用 HTTP 请求源站;
  3. 源站发现是 HTTP,跳转到 HTTPS;
  4. Cloudflare 再次用 HTTP 请求源站;
  5. 无限循环。

解决方法:

  1. 不推荐长期使用 Flexible;
  2. 给源站配置 SSL 证书;
  3. Cloudflare SSL 模式改为 Full 或 Full Strict;
  4. 源站保留 HTTP 到 HTTPS 跳转。

Nginx 推荐配置:

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

3. Cloudflare Origin Certificate 如何使用?

Cloudflare Origin Certificate 是 Cloudflare 提供给源站使用的证书,只被 Cloudflare 信任,浏览器直接访问源站 IP 时不会信任。

适用场景:

用户 → Cloudflare → 源站

不适合:

用户 → 源站 IP

安装后,Cloudflare SSL 模式应设置为:

Full (strict)

Nginx 配置示例:

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

    ssl_certificate /etc/nginx/ssl/cloudflare-origin.pem;
    ssl_certificate_key /etc/nginx/ssl/cloudflare-origin.key;

    root /var/www/example.com;
    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }
}

四、源站真实 IP 获取问题

1. 为什么网站日志里全是 Cloudflare IP?

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

Cloudflare 会通过请求头传递真实 IP,例如:

CF-Connecting-IP
X-Forwarded-For

如果源站没有正确配置,就会导致日志、风控、限流、后台统计都记录 Cloudflare IP。


2. Nginx 获取真实 IP 配置文件

可以创建一个 Cloudflare Real IP 配置文件:

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

配置内容如下:

# 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;

real_ip_header CF-Connecting-IP;
real_ip_recursive on;

然后测试并重载 Nginx:

sudo nginx -t
sudo systemctl reload nginx

配置成功后,Nginx 日志中的 $remote_addr 会变成用户真实 IP。


3. Apache 获取真实 IP 配置

Apache 可以使用 mod_remoteip 模块。

启用模块:

sudo a2enmod remoteip

创建配置:

sudo nano /etc/apache2/conf-available/cloudflare-real-ip.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-real-ip
sudo systemctl restart apache2

五、Cloudflare 常见错误代码

1. Error 520:Web Server Returned an Unknown Error

520 是一个比较泛的错误,通常表示 Cloudflare 与源站通信时源站返回了异常响应。

常见原因:

  1. 源站程序崩溃;
  2. 源站返回空响应;
  3. 响应头过大;
  4. 防火墙拦截 Cloudflare IP;
  5. Web 服务配置异常。

排查方法:

curl -I http://源站IP
curl -I https://源站IP --resolve example.com:443:源站IP

查看源站日志:

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

2. Error 521:Web Server Is Down

521 表示 Cloudflare 无法连接源站 Web 服务。

常见原因:

  1. Nginx/Apache 没有运行;
  2. 服务器防火墙阻止 Cloudflare;
  3. 端口 80/443 未开放;
  4. 源站 IP 配置错误。

检查服务:

sudo systemctl status nginx
sudo systemctl status apache2

检查端口:

sudo ss -lntp | grep -E ':80|:443'

3. Error 522:Connection Timed Out

522 表示 Cloudflare 连接源站超时。

常见原因:

  1. 源站服务器负载过高;
  2. 安全组未放行 Cloudflare IP;
  3. 网络质量差;
  4. 源站响应过慢;
  5. 后端程序阻塞。

建议:

  1. 检查服务器 CPU、内存、磁盘 IO;
  2. 优化数据库查询;
  3. 放行 Cloudflare IP;
  4. 检查防火墙规则;
  5. 降低源站压力,增加缓存。

4. Error 523:Origin Is Unreachable

523 表示 Cloudflare 无法到达源站 IP。

常见原因:

  1. DNS 记录指向了错误 IP;
  2. 源站 IP 不可达;
  3. 服务器网络中断;
  4. 云厂商安全策略阻断。

排查命令:

ping 源站IP
traceroute 源站IP
curl -I http://源站IP

5. Error 525:SSL Handshake Failed

525 表示 Cloudflare 与源站进行 SSL 握手失败。

常见原因:

  1. 源站没有配置 HTTPS;
  2. 证书过期;
  3. 证书链不完整;
  4. Nginx SSL 配置错误;
  5. Cloudflare SSL 模式设置不匹配。

检查证书:

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

6. Error 526:Invalid SSL Certificate

526 通常出现在 SSL 模式为 Full Strict 时,Cloudflare 校验证书失败。

常见原因:

  1. 源站证书过期;
  2. 证书域名不匹配;
  3. 使用自签名证书;
  4. 中间证书缺失;
  5. Cloudflare Origin Certificate 未正确安装。

解决方式:

  1. 使用 Let's Encrypt;
  2. 使用 Cloudflare Origin Certificate;
  3. 确保证书包含当前域名;
  4. 检查证书链完整性。

六、缓存相关常见问题

1. 为什么 Cloudflare 缓存不生效?

Cloudflare 默认主要缓存静态资源,例如:

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

HTML 页面默认不一定缓存,尤其是动态页面。

另外,如果源站响应头包含以下内容,也可能导致缓存不生效:

Cache-Control: no-cache
Cache-Control: no-store
Set-Cookie

可以通过浏览器开发者工具查看响应头:

CF-Cache-Status: HIT
CF-Cache-Status: MISS
CF-Cache-Status: BYPASS
CF-Cache-Status: DYNAMIC

常见含义:

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

2. 如何缓存整个 HTML 页面?

如果是静态站点、博客、文档站,可以缓存 HTML 页面。

可以在 Cloudflare 中创建 Cache Rule:

If: Hostname equals example.com
Then:
Cache eligibility: Eligible for cache
Edge TTL: 1 hour / 1 day
Browser TTL: 30 minutes

如果有后台管理路径,需要排除:

example.com/wp-admin/*
example.com/admin/*
example.com/api/*

WordPress 网站不建议无脑全站缓存,否则登录状态、评论、购物车等可能出现异常。


3. Nginx 静态资源缓存配置

以下是适合大多数网站的静态资源缓存配置:

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

HTML 页面可以设置较短缓存:

location ~* \.html$ {
    expires 10m;
    add_header Cache-Control "public, max-age=600";
}

动态接口建议不缓存:

location /api/ {
    add_header Cache-Control "no-store, no-cache, must-revalidate";
}

七、安全与防火墙常见问题

1. 如何只允许 Cloudflare 访问源站?

如果你想隐藏源站并防止用户绕过 Cloudflare 直接访问服务器,可以在服务器防火墙中只放行 Cloudflare IP 访问 80/443。

UFW 示例:

sudo ufw default deny incoming
sudo ufw default allow outgoing

# 允许 SSH,请替换为你的管理 IP
sudo ufw allow from 你的固定IP to any port 22 proto tcp

# 允许 Cloudflare IPv4 访问 HTTP/HTTPS
sudo ufw allow from 173.245.48.0/20 to any port 80,443 proto tcp
sudo ufw allow from 103.21.244.0/22 to any port 80,443 proto tcp
sudo ufw allow from 103.22.200.0/22 to any port 80,443 proto tcp
sudo ufw allow from 103.31.4.0/22 to any port 80,443 proto tcp
sudo ufw allow from 141.101.64.0/18 to any port 80,443 proto tcp
sudo ufw allow from 108.162.192.0/18 to any port 80,443 proto tcp
sudo ufw allow from 190.93.240.0/20 to any port 80,443 proto tcp
sudo ufw allow from 188.114.96.0/20 to any port 80,443 proto tcp
sudo ufw allow from 197.234.240.0/22 to any port 80,443 proto tcp
sudo ufw allow from 198.41.128.0/17 to any port 80,443 proto tcp
sudo ufw allow from 162.158.0.0/15 to any port 80,443 proto tcp
sudo ufw allow from 104.16.0.0/13 to any port 80,443 proto tcp
sudo ufw allow from 104.24.0.0/14 to any port 80,443 proto tcp
sudo ufw allow from 172.64.0.0/13 to any port 80,443 proto tcp
sudo ufw allow from 131.0.72.0/22 to any port 80,443 proto tcp

sudo ufw enable
sudo ufw status

注意:Cloudflare IP 段可能更新,建议定期从官方页面同步。


2. 如何防止后台被爆破?

可以在 Cloudflare 中设置 WAF 规则,例如保护 WordPress 登录页。

规则示例:

If:
URI Path equals /wp-login.php
OR URI Path starts with /wp-admin

Then:
Managed Challenge

如果你有固定 IP,可以设置:

If:
URI Path starts with /wp-admin
AND IP Source Address not equals 你的IP

Then:
Block

这样只有指定 IP 可以访问后台。


3. 如何屏蔽某些国家或地区访问?

Cloudflare WAF 支持按国家代码匹配。

例如屏蔽某些国家:

If:
Country equals XX

Then:
Block

也可以只允许特定国家:

If:
Country not equals CN

Then:
Managed Challenge

是否启用这类规则,应根据业务实际情况决定,避免误伤真实用户。


八、常用 Nginx 完整配置示例

以下示例适用于:

  1. 使用 Cloudflare 代理;
  2. 源站启用 HTTPS;
  3. 使用 Cloudflare Origin Certificate;
  4. 获取真实用户 IP;
  5. 配置静态资源缓存;
  6. HTTP 自动跳转 HTTPS。

1. /etc/nginx/conf.d/cloudflare-real-ip.conf

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;

real_ip_header CF-Connecting-IP;
real_ip_recursive on;

2. /etc/nginx/sites-available/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;

    root /var/www/example.com;
    index index.html index.htm index.php;

    ssl_certificate /etc/nginx/ssl/cloudflare-origin.pem;
    ssl_certificate_key /etc/nginx/ssl/cloudflare-origin.key;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    location / {
        try_files $uri $uri/ /index.html;
    }

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

    location ~* \.html$ {
        expires 10m;
        add_header Cache-Control "public, max-age=600";
    }

    location /api/ {
        add_header Cache-Control "no-store, no-cache, must-revalidate";
        try_files $uri $uri/ =404;
    }

    location ~ /\. {
        deny all;
    }
}

启用配置:

sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

九、Cloudflare 推荐基础设置

1. SSL/TLS 设置

推荐:

SSL/TLS encryption mode: Full (strict)
Always Use HTTPS: On
Automatic HTTPS Rewrites: On
Minimum TLS Version: TLS 1.2
TLS 1.3: On

不建议长期使用:

Flexible

2. DNS 设置

推荐结构:

A      example.com      源站IP      Proxied
CNAME  www              example.com Proxied

如果是邮件相关记录:

MX     example.com      mail.example.com
A      mail             邮件服务器IP  DNS only
TXT    SPF/DKIM/DMARC   DNS only

邮件服务不要使用橙色云朵代理,否则可能导致邮件收发异常。


3. 缓存设置

普通网站推荐:

Caching Level: Standard
Browser Cache TTL: Respect Existing Headers 或 4 hours
Always Online: 按需开启
Development Mode: 调试时临时开启

如果是静态站点,可以考虑:

Cache Rules: Cache Everything
Edge TTL: 1 day

4. 安全设置

推荐:

Security Level: Medium
Bot Fight Mode: 按需开启
WAF Managed Rules: On
Browser Integrity Check: On

如果误伤用户,可以适当降低安全等级或添加 Skip 规则。


十、常用排查命令汇总

1. 检查 DNS

dig example.com
dig example.com @1.1.1.1
dig A example.com
dig CNAME www.example.com

2. 检查 HTTP 响应

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

3. 绕过 Cloudflare 访问源站

curl -I https://example.com --resolve example.com:443:源站IP

4. 检查证书

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

5. 检查 Nginx 配置

sudo nginx -t
sudo systemctl status nginx
sudo tail -f /var/log/nginx/error.log

6. 检查端口监听

sudo ss -lntp
sudo ss -lntp | grep -E ':80|:443'

十一、使用 Cloudflare 的注意事项

  1. 不要把所有服务都开橙色云朵
    Cloudflare 免费版主要代理 HTTP/HTTPS,不适合直接代理 SSH、SMTP、IMAP、POP3 等服务。

  2. 源站仍然需要安全加固
    Cloudflare 可以减少攻击面,但不能替代服务器安全。系统更新、防火墙、SSH 密钥登录、应用安全依然重要。

  3. Full Strict 是最推荐的 HTTPS 模式
    如果业务长期运行,建议源站配置有效证书,并使用 Full Strict。

  4. 缓存规则不要盲目配置
    对登录页面、支付页面、用户中心、API 接口要谨慎缓存,否则可能出现数据错乱或隐私风险。

  5. 定期检查 Cloudflare IP 段
    如果源站防火墙只允许 Cloudflare IP,需要定期同步官方 IP 列表。

  6. 注意真实 IP 配置
    如果没有正确配置 Real IP,日志、限流、安全策略都会基于 Cloudflare 节点 IP,可能导致误判。


十二、总结

Cloudflare 的优势在于功能完整、上手简单、全球节点丰富,可以同时承担 DNS、CDN、HTTPS、安全防护和访问控制等职责。对于大多数网站来说,只要正确配置 DNS、SSL/TLS、真实 IP、缓存和防火墙规则,就能获得较好的性能和安全收益。

推荐的基础配置可以概括为:

DNS:A 记录和 CNAME 记录开启代理
SSL/TLS:使用 Full Strict
源站:配置有效 HTTPS 证书
Nginx:配置 Cloudflare Real IP
缓存:静态资源长期缓存,动态接口不缓存
安全:后台路径加 WAF 或 IP 限制
防火墙:只允许 Cloudflare 访问 80/443

如果遇到 520、521、522、523、525、526 等错误,优先从源站服务状态、端口开放、防火墙规则、SSL 证书和 DNS 解析几个方向排查。Cloudflare 本身并不能解决所有问题,它更像是一层强大的边缘代理与安全网关。只有 Cloudflare 与源站服务器配置配合得当,才能真正发挥稳定、加速和防护的作用。

目录结构
全文