假设域名 backend.196000.xyz 在 Cloudflare 打开了代理使用 CDN,现在用 fast.196000.xyz 来进行加速,如果直接通过 nginx 进行反向代理 Cloudflare 的 https,通常会出现 “dns points to prohibited ip” 错误,其原因归根结底是 Cloudflare 开启了 SNI 识别回源。解决思路是通过 nginx 的 SNI 覆写和 host 覆写来让 nginx 改掉这两个 Cloudflare CDN 回源的参数,从而可以达到 Cloudflare CDN 的效果。

server {
    listen 80;
    listen [::]:80;
    server_name fast.196000.xyz;

    location / {
        # 反向代理到目标地址,替换为你的
        proxy_pass https://backend.196000.xyz;
        # 设置回源主机名,替换为你的
        proxy_set_header Host backend.196000.xyz;

        # 设置头部传递真实 IP
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # 添加自定义响应头
        add_header CustomCacheStatus $upstream_cache_status;
        add_header CustomServerNode Node-01;
        
        # 禁用缓存相关头部
        add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
        add_header Pragma "no-cache";
        add_header Expires "0";

        # 禁用 nginx 的缓存功能
        proxy_buffering off;
        proxy_cache off;
        
        # 启用 SNI 支持
        proxy_ssl_server_name on;
        # 替换为你的
        proxy_ssl_name backend.196000.xyz;

        # 禁用 Gzip 压缩以确保数据一致性
        gzip off;
        proxy_set_header Accept-Encoding '*';

        # 文本替换配置
        sub_filter_once off;
        # 前面的替换为你的 CDN 后端域名,后面的是你这个加速的域名,替换为你的
        sub_filter 'backend.196000.xyz' 'fast.196000.xyz';

        # WebSocket 配置
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}