26 / 01 / 20
本文档记录了基于 Cloudflare CDN 的通用 Xray 部署方案。该方案利用 Cloudflare CDN 隐藏真实服务器 IP,有效解决了直连被阻断的问题。
流量路径: 用户客户端 -> (HTTPS/443) -> Cloudflare CDN -> (HTTP/80) -> 源站服务器 (Xray)
优势:
抗封锁: 防火墙只能看到 Cloudflare 的 IP,无法精准识别或封锁。
隐藏源站: 真实服务器 IP 不对公网暴露,极大提高安全性。
协议栈: VLESS + WebSocket (WS) + None Encryption (因外层已有 Cloudflare TLS 保护)。
服务器: 任意海外 VPS (推荐 Debian 11/12)
域名: 托管于 Cloudflare 的域名 (示例: your-domain.com)
在 Cloudflare 后台必须完成以下设置,否则会导致连接失败 (Error 521/522)。
类型: A 记录
名称: www 或 @ (您的子域名)
内容: [您的服务器IP]
代理状态: 已代理 / Proxied (必须开启小黄云图标)
模式: 灵活 / Flexible
原因: 源站 Xray 监听的是 80 端口 (HTTP),没有配置本地证书。如果设为 "完全/Full",Cloudflare 会尝试用 HTTPS 连接源站导致失败。
您可以在服务器上创建一个文件 install.sh,将下方完整的脚本内容复制进去运行。
安装步骤:
更名为 install.sh: nano install.sh
粘贴下方代码。
运行: bash install.sh
#!/bin/bash # install_xray_cdn.sh - 全新安装 VLESS + WebSocket (Cloudflare CDN 专用) # 适用于 Debian 11/12 red='\033[0;31m' green='\033[0;32m' yellow='\033[0;33m' plain='\033[0m' # ========================================== # 请在下方修改您的域名 DOMAIN="your-domain.com" # ========================================== echo -e "${green}开始全新安装 Xray (CDN 模式)...${plain}" echo -e "${yellow}请确认您已在 Cloudflare 将 $DOMAIN 解析到本机 IP,并开启了小黄云(Proxied)${plain}" # 1. 检查 Root [[ $EUID -ne 0 ]] && echo -e "${red}错误:必须使用 root 用户运行此脚本!${plain}" && exit 1 # 2. 安装基础依赖 echo -e "${yellow}正在更新系统并安装依赖...${plain}" apt update -y apt install -y curl wget openssl uuid-runtime jq iproute2 # 3. 安装 Xray 核心 echo -e "${yellow}正在安装 Xray 核心...${plain}" bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install # 4. 生成配置参数 (UUID + Path) UUID=$(uuidgen) if [ -z "$UUID" ]; then UUID=$(cat /proc/sys/kernel/random/uuid) fi # 生成一个随机路径 WS_PATH="/$(openssl rand -hex 4)" echo -e "UUID: $UUID" echo -e "WS Path: $WS_PATH" # 5. 写入配置文件 # 监听 80 端口,Cloudflare Flexible 模式会回源到 80 cat > /usr/local/etc/xray/config.json <<EOF { "log": { "loglevel": "warning" }, "inbounds": [ { "port": 80, "protocol": "vless", "settings": { "clients": [ { "id": "$UUID", "level": 0 } ], "decryption": "none" }, "streamSettings": { "network": "ws", "wsSettings": { "path": "$WS_PATH" } } } ], "outbounds": [ { "protocol": "freedom", "tag": "direct" }, { "protocol": "blackhole", "tag": "block" } ] } EOF # 6. 开启 BBR 加速 echo -e "${yellow}配置 BBR 加速...${plain}" if ! grep -q "net.core.default_qdisc=fq" /etc/sysctl.conf; then echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf fi if ! grep -q "net.ipv4.tcp_congestion_control=bbr" /etc/sysctl.conf; then echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf fi sysctl -p # 7. 配置防火墙 (放行 80) echo -e "${yellow}配置防火墙...${plain}" systemctl stop nginx >/dev/null 2>&1 systemctl stop apache2 >/dev/null 2>&1 if command -v ufw > /dev/null; then ufw allow 80/tcp ufw reload fi if command -v iptables > /dev/null; then iptables -I INPUT -p tcp --dport 80 -j ACCEPT fi # 8. 重启 Xray 并验证 systemctl restart xray systemctl enable xray sleep 2 if ! systemctl is-active --quiet xray; then echo -e "${red}Xray 启动失败!${plain}" echo -e "请检查日志:journalctl -u xray -n 20" exit 1 fi # 9. 生成分享链接 LINK="vless://$UUID@$DOMAIN:443?encryption=none&security=tls&type=ws&host=$DOMAIN&path=$WS_PATH#Xray_CDN_$DOMAIN" echo -e "\n${green}========================================${plain}" echo -e "${green} 安装成功!(VLESS + WebSocket + CDN) ${plain}" echo -e "${green}========================================${plain}" echo -e "地址 (Address): ${DOMAIN}" echo -e "端口 (Port): 443 (Cloudflare Edge)" echo -e "用户ID (UUID): ${UUID}" echo -e "传输协议 (Network): ws (WebSocket)" echo -e "路径 (Path): ${WS_PATH}" echo -e "伪装域名 (Host): ${DOMAIN}" echo -e "${green}----------------------------------------${plain}" echo -e "Quantumult X 配置文本 (直接粘贴):" echo -e "vless=$DOMAIN:443, method=none, password=$UUID, fast-open=false, udp-relay=false, tag=Xray-CDN, tls-host=$DOMAIN, force-http-engine-hosts=$DOMAIN, fast-open=false, verification-mode=certificate, obfs=ws, obfs-host=$DOMAIN, obfs-uri=$WS_PATH" echo -e "${green}----------------------------------------${plain}" echo -e "URL Link:" echo -e "${LINK}" echo -e "${green}----------------------------------------${plain}"
请在 Quantumult X 的配置文件 [server_local] 段落中添加以下内容。
注意: 请将
<YOUR_DOMAIN>,<UUID>和<PATH>替换为实际值。
# 格式示例: vless=your-domain.com:443, method=none, password=<YOUR_UUID>, fast-open=false, udp-relay=false, tag=Xray-CDN, tls-host=your-domain.com, force-http-engine-hosts=your-domain.com, fast-open=false, verification-mode=certificate, obfs=ws, obfs-host=your-domain.com, obfs-uri=<YOUR_WS_PATH>
systemctl status xray
journalctl -u xray -f
systemctl restart xray