XRay 服务器搭建:Vless + Reality 指南

911 次浏览

VLESS + Reality 的配置能提供很好的网络体验,其核心优势在于无需域名,并利用 TLS 伪装增强连接的隐蔽性。但其无法使用反向代理。

1、准备工作

更新系统:

sudo apt update && sudo apt upgrade -y

更新后重启系统:

sudo reboot

2、安装XRay

下载并执行官方安装脚本

bash <(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh) install

3、配置XRay

1)生成 REALITY 所需的密钥

/usr/local/bin/xray x25519

输出:

PrivateKey: XXXXXXXX(私钥用于服务端)
Password: XXXXXXXXXX(公钥用于客户端)

2)生成UUID

cat /proc/sys/kernel/random/uuid

3)使用文本编辑器(如 nano)编辑配置文件

sudo nano /usr/local/etc/xray/config.json

内容如下:

{
  "log": {
    "loglevel": "warning",          // 内容从少到多: "none", "error", "warning", "info", "debug"
    "access": "/var/log/xray/access.log", // 访问记录
    "error": "/var/log/xray/error.log"    // 错误记录
  },

  "inbounds": [
    {
      "port": 443,                         // 服务器端口,通常使用 443
      "protocol": "vless",
      "settings": {
        "clients": [
          {
            "id": "UUID",                  // 替换为生成的 UUID
            "flow": "xtls-rprx-vision"     // 启用 Vision 流控
          }
        ],
        "decryption": "none"
      },
      "sniffing": {
        "enabled": true,
        "destOverride": ["http", "tls", "quic"]
      },
      "streamSettings": {
        "network": "tcp",
        "security": "reality",
        "realitySettings": {
          "dest": "www.apple.com:443",       // 填写一个伪装网站
          "serverNames": ["www.apple.com"],  // 可用的服务名
          "privateKey": "私钥",               // 替换为生成的私钥
          "shortIds": [""]                   // 短ID 可为空
        }
      }
    }
  ],

  "outbounds": [
    {
      "tag": "direct",
      "protocol": "freedom",
      "settings": {}
    },
    {
      "tag": "block",
      "protocol": "blackhole",
      "settings": {}
    }
  ],

  "routing": {
    "domainStrategy": "IPIfNonMatch",
    "rules": [
      {
        "type": "field",            // 防止服务器本地流转问题:如内网被攻击或滥用、错误的本地回环等
        "ip": ["geoip:private"],
        "outboundTag": "block"
      },
      {
        "type": "field",            // 防止服务器直连国内
        "ip": ["geoip:cn"],
        "outboundTag": "block"
      }
      // 其他请求默认直接出站direct(outbound中的第一项作为默认出站)
    ]
  }
}

验证配置文件语法

sudo xray run -test -config /usr/local/etc/xray/config.json

启动并检查V2ray运行状态:

sudo systemctl start xray   # 启动
sudo systemctl enable xray  # 设置开机自启
sudo systemctl status xray  # 检查服务状态

# 实时读取日志
tail -f /var/log/xray/access.log

4、配置防火墙(可选)

允许 SSH, HTTP, HTTPS

sudo ufw allow 22
sudo ufw allow 443

开启防火墙

sudo ufw enable

5、客户端配置

配置内容如下

{
  "log": {
    "loglevel": "warning",          // 内容从少到多: "none", "error", "warning", "info", "debug"
    "access": "/var/log/xray/access.log", // 访问记录
    "error": "/var/log/xray/error.log"    // 错误记录
  },

  "inbounds": [
    {
      "listen": "127.0.0.1",        // 改为 0.0.0.0 允许来着 LAN 的请求
      "port": 10808,
      "protocol": "socks",
      "settings": {
         "udp": true
      },
      "sniffing": {
         "enabled": true,
         "destOverride": ["http", "tls", "quic"]
      }
    }
  ],

  "outbounds": [
    {
      "tag": "direct",          // 直接出站
      "protocol": "freedom",        // 使用freedom直接出站
      "settings": {}
    },
    {
      "tag": "block",           // 拦截出站
      "protocol": "blackhole",      // 黑洞协议丢弃所有数据
      "settings": {}
    },
    {
      "tag": "proxy",
      "protocol": "vless",
      "settings": {
        "vnext": [
          {
            "address": "123.123.123.123",   // 服务器 IP 地址
            "port": 443,
            "users": [
              {
                "id": "UUID",               // 与服务端配置一致的 UUID
                "flow": "xtls-rprx-vision", // 与服务端一致的流控
                "encryption": "none"
              }
            ]
          }
        ]
      },
      "streamSettings": {
        "network": "tcp",
        "security": "reality",
        "realitySettings": {
          "serverName": "www.apple.com",   // 与服务端 serverNames 相同
          "publicKey": "公钥",              // 替换为生成的公钥
          "shortId": ""                    // shortId
        }
      }
    }
  ],

  "routing": {
    "domainStrategy": "IPIfNonMatch",
    "rules": [
      {
        "type": "field",            // 广告拦截
        "domain": ["geosite:category-ads-all"],
        "outboundTag": "block"
      },
      {
        "type": "field",            // 本地流量直连
        "ip": ["geoip:private"],
        "outboundTag": "direct"
      },
      {
        "type": "field",            // 国外流量走代理
        "ip": ["geoip:!cn"],
        "outboundTag": "proxy"
      },
      {
        "type": "field",            // 国外流量走代理
        "domain": ["geosite:geolocation-!cn"],
        "outboundTag": "proxy"
      }
      // 其他请求默认直接出站direct(outbound中的第一项作为默认出站)
    ]
  }
}

IOS平台可使用 Shadowrocket 软件

新建Vless协议
地址 (Address):服务器IP或域名
端口 (Port):443
用户ID (User ID/UUID):配置文件中使用的UUID
传输方式:none
TLS:开启
TLS配置:
允许不安全:关闭
SNI:配置文件中的伪装域名
XTLS:xtls-rprx-vision
公钥:生成的公钥
短ID:空

参考资料

https://github.com/XTLS/REALITY
https://github.com/XTLS/Xray-examples

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部