Xray或V2Ray 实现VPS出站流量分流

vps.dance 2022-02-08 2023-11-08 7400℃

Xray或V2Ray 的配置类似, 本文按域名分流/按IP分流的方案, 使用Xray举例 智能分流方案.

安装 Xray

如果已经安装了Xray或V2Ray, 可跳过这部.
可使用 VKit 安装 Xray (同时安装 增强版的geosite/geoip规则).

修改配置文件

根据域名或者IP转发出站流量
编辑 /usr/local/etc/xray/config.json, 内容类似下面
这里举例, 入站/出站都是 ss (Xray或V2Ray支持ss/Socks5/Trojan/VLESS/VMess).

{
  "inbounds": [ // 入站规则
     {
       "port": 50000, // 当前VPS开放的端口
       "protocol": "shadowsocks",
       "settings": {
         "clients": [
           {
             "password": "your-password",
             "method": "aes-256-gcm"
           }
         ],
         "network": "tcp,udp"
       }
     }
  ],
  "outbounds": [ // 出站规则
    {
      "tag": "direct",
      "protocol": "freedom",
      "settings": {}
    },
    {
      "tag": "VPS1", // Proxy 名称
      "protocol": "shadowsocks",
      "settings": {
        "servers": [{
          "address": "server-your-vps1",
          "port": 12345,
          "password": "your-vps1-password",
          "method": "aes-256-gcm"
        }]
      }
    },
    {
      "tag": "VPS2", // Proxy 名称
      "protocol": "shadowsocks",
      "settings": {
        "servers": [{
          "address": "server-your-vps2",
          "port": 12345,
          "password": "your-vps2-password",
          "method": "aes-256-gcm"
        }]
      }
    }
  ],
  "routing": { // 路由, 分流规则
    // "domainStrategy": "AsIs",
    "rules": [
      {
        "type": "field",
        "outboundTag": "VPS1", // 使用VPS1
        "domain": [ // 基于域名分流
          // "geosite:netflix",
          "geosite:youtube",
          "geosite:telegram",
          "domain:ip.sb"
        ]
      },
      {
        "type": "field",
        "outboundTag": "VPS2",  // 使用VPS2
        "ip": [ // 基于IP分流
          "8.8.8.8/32",
          "geoip:telegram"
        ]
      }
    ]
  }
}

检查 xray 状态

systemctl status xray
如果启动失败, 可以尝试 直接运行 xray run -config /usr/local/etc/xray/config.json 看报错信息

重启 xray 服务

修改配置后, 重启服务: systemctl restart xray

分流的意义

  • 有时不想用客户端的分流, 希望统一走一个VPS, 这个VPS再根据规则, 把部分流量转发到其他的VPS.
  • 有些商家劫持了路由, 屏蔽了某些站点, 可以把这些分流到其他VPS.
  • 有些VPS不解锁某个流媒体, 可以分流走不同的proxy.
标签: VPS, 分流

非特殊说明, 所有文章均为原创.