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 完全(嚴格)、WebSockets 開啟。
步驟 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(或用於 aes-256 的 -base64 32)
步驟 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" }
]
每位使用者的流量統計——啟用 stats 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)上,使用 VLESS 和 Reality 傳輸設定 Xray。為 alice、bob 和 carol 建立帳戶。使用 microsoft.com 作為 Reality 目標,並提供 v2rayNG 的分享連結。」
代理程式將透過官方腳本安裝 Xray,為每個使用者產生 UUID 和 X25519 金鑰對,建立一個 config.json,其中包含在連接埠 443 上使用 Reality 設定(目標為 www.microsoft.com)的 VLESS 傳入連線,新增三個帶有 xtls-rprx-vision 流程的客戶端條目,新增用於封鎖私有 IP 的路由規則,啟用並啟動 xray 服務,在防火牆中開啟連接埠 443,並輸出三個包含伺服器 IP、公鑰、short ID 和 SNI 的 vless:// 分享連結。
範例 2:搭配 Cloudflare 和回退網站設定 CDN 前端 VLESS 代理
使用者提示:「我有一個指向我們伺服器(透過 Cloudflare)的 proxy.mysite.com 網域。在 /secret-ws 上配置 VLESS over WebSocket 的 Xray,讓流量通過 Cloudflare 的 CDN。非代理訪客應該看到一個普通的 nginx 網站。」
代理程式將使用 certbot 取得 Let's Encrypt 憑證,配置 Xray 在連接埠 443 上設定主要的 VLESS+TLS 傳入連線,並帶有回退到連接埠 8080 的 nginx,在 localhost:8443 上新增第二個帶有路徑 /secret-ws 的 VLESS+WebSocket 傳入連線,設定 nginx 在連接埠 8080 上提供靜態網站,驗證 Cloudflare SSL 設定為完全(嚴格)且 WebSockets 已啟用,並提供帶有 WebSocket 路徑和 Cloudflare SNI 的客戶端連線字串。
指南
- 對於新的部署,始終使用 VLESS + Reality;它不需要網域或憑證,並且是最難偵測和封鎖的協定
- 為所有 VMess 使用者設定
"alterId": 0,以啟用強制性 AEAD 加密;非零的 alterId 使用已棄用且安全性較低的舊版格式 - 在路由規則中封鎖對私有 IP 範圍(geoip:private)的存取,以防止代理被用於針對內部服務的 SSRF 攻擊
- 使用
xray api指令在執行時期新增和移除使用者,而無需重新啟動服務,這樣可以避免中斷活躍使用者的連線 - 從一個託管在與伺服器不同 IP 上的熱門網站(microsoft.com、apple.com)選擇 Reality 的
dest和serverNames,並確保該網站支援 TLS 1.3 和 HTTP/2 - 啟用帶有每位使用者追蹤(
statsUserUplink/statsUserDownlink)的 stats API,以監控頻寬使用情況並偵測濫用行為