Xray
概述
部署和配置 Xray-core — 支持 VLESS、VMess、Trojan 和 Shadowsocks 协议的高级代理平台,并采用 Reality、XTLS-Vision、WebSocket、gRPC 和 HTTP/2 等现代传输方式。本技能涵盖服务器部署、协议选择、TLS/Reality 配置、流量路由、多用户管理、CDN 集成、客户端配置和监控。
说明
步骤 1:安装
bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install
xray version
# 二进制文件:/usr/local/bin/xray
# 配置文件:/usr/local/etc/xray/config.json
# 生成 UUID 和 X25519 密钥
xray uuid
xray x25519 # Reality 的私钥 + 公钥对
步骤 2:VLESS + Reality(推荐)
最安全的配置。Reality 模拟真实 HTTPS 网站的 TLS 握手,无需域名或 TLS 证书。
服务器配置 (/usr/local/etc/xray/config.json):
{
"log": { "loglevel": "warning" },
"inbounds": [{
"listen": "0.0.0.0",
"port": 443,
"protocol": "vless",
"settings": {
"clients": [
{ "id": "YOUR-UUID", "flow": "xtls-rprx-vision", "email": "user1@proxy" }
],
"decryption": "none"
},
"streamSettings": {
"network": "tcp",
"security": "reality",
"realitySettings": {
"dest": "www.microsoft.com:443",
"serverNames": ["www.microsoft.com", "microsoft.com"],
"privateKey": "YOUR-PRIVATE-KEY",
"shortIds": ["", "0123456789abcdef"]
}
},
"sniffing": { "enabled": true, "destOverride": ["http", "tls", "quic"] }
}],
"outbounds": [
{ "protocol": "freedom", "tag": "direct" },
{ "protocol": "blackhole", "tag": "block" }
],
"routing": {
"rules": [{ "type": "field", "ip": ["geoip:private"], "outboundTag": "block" }]
}
}
客户端分享链接:
vless://UUID@SERVER_IP:443?encryption=none&flow=xtls-rprx-vision&security=reality&sni=www.microsoft.com&fp=chrome&pbk=PUBLIC_KEY&sid=0123456789abcdef&type=tcp#MyProxy
systemctl enable --now xray
步骤 3:VLESS + WebSocket + TLS(支持 CDN)
用于通过 Cloudflare CDN 路由流量以隐藏服务器 IP。需要域名配合 Cloudflare 代理和 TLS 证书。
apt install -y certbot
certbot certonly --standalone -d proxy.yourdomain.com
{
"inbounds": [
{
"port": 443, "protocol": "vless",
"settings": {
"clients": [{ "id": "YOUR-UUID" }],
"decryption": "none",
"fallbacks": [
{ "dest": 8080 },
{ "path": "/ws-path", "dest": 8443, "xver": 1 }
]
},
"streamSettings": {
"network": "tcp", "security": "tls",
"tlsSettings": {
"certificates": [{
"certificateFile": "/etc/letsencrypt/live/proxy.yourdomain.com/fullchain.pem",
"keyFile": "/etc/letsencrypt/live/proxy.yourdomain.com/privkey.pem"
}]
}
}
},
{
"listen": "127.0.0.1", "port": 8443, "protocol": "vless",
"settings": { "clients": [{ "id": "YOUR-UUID" }], "decryption": "none" },
"streamSettings": { "network": "ws", "wsSettings": { "path": "/ws-path" } }
}
],
"outbounds": [{ "protocol": "freedom", "tag": "direct" }]
}
在 8080 端口上设置 nginx 作为回退,并在 Cloudflare 中:SSL/TLS 设置为完全(严格),并启用 WebSocket。
步骤 4:Trojan 和 Shadowsocks
Trojan(看起来像普通 HTTPS):
{
"inbounds": [{
"port": 443, "protocol": "trojan",
"settings": {
"clients": [{ "password": "your-strong-password", "email": "user1@trojan" }],
"fallbacks": [{ "dest": 8080 }]
},
"streamSettings": {
"network": "tcp", "security": "tls",
"tlsSettings": { "certificates": [{ "certificateFile": "...", "keyFile": "..." }] }
}
}]
}
Shadowsocks 2022(现代 AEAD 加密):
{
"inbounds": [{
"port": 8388, "protocol": "shadowsocks",
"settings": { "method": "2022-blake3-aes-128-gcm", "password": "BASE64_KEY", "network": "tcp,udp" }
}]
}
生成密钥:openssl rand -base64 16(或 -base64 32 用于 aes-256)
步骤 5:多用户管理
"clients": [
{ "id": "uuid-1", "email": "alice@proxy", "flow": "xtls-rprx-vision" },
{ "id": "uuid-2", "email": "bob@proxy", "flow": "xtls-rprx-vision" },
{ "id": "uuid-3", "email": "carol@proxy", "flow": "xtls-rprx-vision" }
]
每个用户的流量统计 — 启用统计 API:
{
"stats": {},
"policy": {
"levels": { "0": { "statsUserUplink": true, "statsUserDownlink": true } },
"system": { "statsInboundUplink": true, "statsInboundDownlink": true }
},
"api": { "tag": "api", "services": ["StatsService", "HandlerService"] },
"inbounds": [
{ "listen": "127.0.0.1", "port": 10085, "protocol": "dokodemo-door",
"settings": { "address": "127.0.0.1" }, "tag": "api" }
],
"routing": { "rules": [{ "type": "field", "inboundTag": ["api"], "outboundTag": "api" }] }
}
运行时查询和管理用户:
xray api stats --server=127.0.0.1:10085 -name "user>>>alice@proxy>>>traffic>>>uplink"
xray api adi --server=127.0.0.1:10085 --inbound-tag proxy --id "new-uuid" --email "newuser@proxy"
xray api rmi --server=127.0.0.1:10085 --inbound-tag proxy --email "olduser@proxy"
步骤 6:路由规则和监控
按域名/IP/地理位置路由:
{
"routing": {
"domainStrategy": "IPIfNonMatch",
"rules": [
{ "type": "field", "domain": ["geosite:category-ads-all"], "outboundTag": "block" },
{ "type": "field", "ip": ["geoip:private"], "outboundTag": "block" },
{ "type": "field", "network": "tcp,udp", "outboundTag": "proxy" }
]
}
}
监控:
systemctl status xray
journalctl -u xray -f
grep "accepted" /var/log/xray/access.log | awk '{print $3}' | sort | uniq -c | sort -rn
防火墙:
ufw allow 443/tcp && ufw allow 443/udp && ufw enable
示例
示例 1:为小团队部署 VLESS + Reality 代理服务器
用户提示:“在 Debian 12 VPS(IP 198.51.100.30)上为 alice、bob、carol 设置 Xray,使用 VLESS + Reality 传输,以 microsoft.com 作为 Reality 目标,并提供 v2rayNG 分享链接。”
AI 代理将使用官方脚本安装 Xray,为每个用户生成 UUID 和 X25519 密钥对,构建一个在 443 端口监听、使用 Reality 设置并针对 www.microsoft.com 的 VLESS 入站配置,添加三个带有 xtls-rprx-vision 流的客户端条目,添加路由规则以阻止私有 IP,启用并启动 xray 服务,在防火墙中开放 443 端口,并输出三个包含服务器 IP、公钥、short ID 和 SNI 的 vless:// 分享链接。
示例 2:设置通过 Cloudflare CDN 的 VLESS 代理并带有回退网站
用户提示:“我有 proxy.mysite.com 通过 Cloudflare 指向我的服务器。配置 Xray 使用 WebSocket 在 /secret-ws 上运行 VLESS,使流量通过 Cloudflare CDN。非代理访客应看到正常的 nginx 网站。”
AI 代理将使用 certbot 获取 Let's Encrypt 证书,配置 Xray:一个主 VLESS+TLS 入站端口 443,并带有回退到 8080 端口的 nginx;添加第二个 VLESS+WebSocket 入站监听 localhost:8443,路径为 /secret-ws;设置 nginx 在 8080 端口提供静态网站;验证 Cloudflare SSL 设置为完全(严格)并启用 WebSocket;提供包含 WebSocket 路径和 Cloudflare SNI 的客户端连接字符串。
指南
- 始终为新部署使用 VLESS + Reality;它不需要域名或证书,且是最难被检测和封锁的协议
- 将所有 VMess 用户的
"alterId"设置为0,以启用强制的 AEAD 加密;非零 alterId 使用已弃用的、安全性较差的旧格式 - 在路由规则中阻止对私有 IP 范围(geoip:private)的访问,以防止代理被用于对内部服务的 SSRF 攻击
- 使用
xray api命令在运行时添加和删除用户,无需重启服务,从而避免断开活跃用户的连接 - 从流行的网站(microsoft.com、apple.com)中选择 Reality 的
dest和serverNames,这些网站应托管在与服务器不同的 IP 上,并确保该网站支持 TLS 1.3 和 HTTP/2 - 启用带用户跟踪(
statsUserUplink/statsUserDownlink)的统计 API,以监控带宽使用并检测滥用情况