Xray 使用指南

1. 下载压缩包

wget https://github.com/XTLS/Xray-core/releases/...   #这里自寻最新版本。

客户端通过 sftp 登录 VPS ,可使用 put 命令将文件上传至VPS

sftp root@vps_ip地址
输入密码
put C:\Users\Administrator\Downloads\Xray-linux-64.zip  /root/

2. 解压安装包

unzip -d ./Xray Xray-linux-64.zip

解压当前目录下的 Xray-linux-64.zip 文件。

将解压后的文件存放到当前目录下的 Xray 子目录中。

3. Xray 文件目录说明

4. 创建配置文件

touch Xray/config.json

配置 Xray 的服务和代理规则。

5. 赋予执行权限

chmod +x Xray/xray

6. 移动 Xray 文件到默认加载目录

sudo mv Xray/xray /usr/local/bin/
sudo mv Xray /usr/local/etc/xray

geoip.dat 和 geosite.dat 文件与 config.json 放在同一目录下。

7. 创建 Systemd 服务文件

touch /etc/systemd/system/xray.service
code /etc/systemd/system/xray.service

          [Unit]
          Description=Xray Service
          After=network.target
          
          [Service]
          User=nobody
          ExecStart=/usr/local/bin/xray -config /usr/local/etc/xray/config.json
          Restart=on-failure
          RestartSec=3
          
          [Install]
          WantedBy=multi-user.target
        
重新加载 Systemd配置: sudo systemctl daemon-reload

配置 Xray 以便开机启动,在文件中指定 xray 及其 config.json 的路径。

8. 设置客户端配置文件

code /usr/local/etc/xray/config.json , 你也可以从其他客户端导入配置。

{
  "inbounds": [
    {
      "port": 1080,
      "listen": "127.0.0.1",
      "protocol": "socks",
      "settings": {
        "auth": "noauth",
        "udp": false
      }
    },
    {
      "port": 8080,
      "listen": "127.0.0.1",
      "protocol": "http",
      "settings": {
        "allowTransparent": false
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "vless",
      "settings": {
        "vnext": [
          {
            "address": "104.18.20.69",
            "port": 443,
            "users": [
              {
                "id": "7fb18dca-47a6-498a-b5ec-9e3bdce1b592",
                "encryption": "none",
                "alterId": 0
              }
            ]
          }
        ]
      },
      "streamSettings": {
        "network": "ws",
        "security": "tls",
        "wsSettings": {
          "path": "/pq/us3",
          "headers": {
            "Host": "us3.xn--ghqu5fm27b67w.com"
          }
        },
        "tlsSettings": {
          "serverName": "us3.xn--ghqu5fm27b67w.com",
          "allowInsecure": false
        }
      }
    }
  ]
}

增加路由功能,使国内流量走直连,其余走代理:


        {
          // 入站配置:定义客户端如何连接到 Xray
          "inbounds": [
            {
              "port": 1080, // 监听端口
              "listen": "127.0.0.1", // 监听地址(本地回环地址)
              "protocol": "socks", // 使用 SOCKS 协议
              "settings": {
                "auth": "noauth", // 无需认证
                "udp": false // 禁用 UDP 支持
              }
            },
            {
              "port": 8080, // 监听端口
              "listen": "127.0.0.1", // 监听地址(本地回环地址)
              "protocol": "http", // 使用 HTTP 协议
              "settings": {
                "allowTransparent": false // 禁用透明代理
              }
            }
          ],
        
          // 出站配置:定义流量如何从 Xray 发出
          "outbounds": [
            {
              "protocol": "freedom", // 使用 freedom 协议(直连)
              "tag": "direct" // 标记为直连出口
            },
            {
              "protocol": "vless", // 使用 VLESS 协议
              "settings": {
                "vnext": [
                  {
                    "address": "45.149.12.232", // 服务器地址
                    "port": 80, // 服务器端口
                    "users": [
                      {
                        "id": "e497da22-d020-40fb-a1fa-bc294066a2d2", // 用户 ID
                        "encryption": "none", // 不加密
                        "alterId": 0 // 额外 ID
                      }
                    ]
                  }
                ]
              },
              "streamSettings": {
                "network": "ws", // 使用 WebSocket 传输
                "security": "none", // 不启用 TLS
                "wsSettings": {
                  "path": "/%40AZARBAYJAB1%40AZARBAYJAB1%40AZARBAYJAB1", // WebSocket 路径
                  "headers": {
                    "Host": "pormang.webhost99.workers.dev" // 自定义 Host 头
                  }
                }
              },
              "tag": "proxy" // 标记为代理出口
            },
            {
              "protocol": "blackhole", // 使用 blackhole 协议(阻止流量)
              "tag": "block" // 标记为阻止出口
            }
          ],
        
          // 路由配置:定义流量如何路由
          "routing": {
            "domainStrategy": "IPIfNonMatch", // 域名解析策略:优先匹配域名,未匹配时尝试匹配 IP
            "rules": [
              {
                "type": "field",
                "ip": ["geoip:cn"], // 匹配国内 IP
                "outboundTag": "direct" // 走直连
              },
              {
                "type": "field",
                "domain": ["geosite:cn"], // 匹配国内域名
                "outboundTag": "direct" // 走直连
              },
              {
                "type": "field",
                "ip": ["geoip:private"], // 匹配私有 IP(如 192.168.x.x)
                "outboundTag": "direct" // 走直连
              },
              {
                "type": "field",
                "domain": ["geosite:category-ads-all"], // 匹配广告域名
                "outboundTag": "block" // 阻止流量
              },
              {
                "type": "field",
                "network": "udp", // 匹配 UDP 流量
                "outboundTag": "block" // 阻止流量
              },
              {
                "type": "field",
                "outboundTag": "proxy", // 默认走代理
                "network": "tcp,udp" // 匹配 TCP 和 UDP 流量
              }
            ]
          },
        
          // GeoIP 配置:指定 geoip.dat 文件路径
          "geoip": {
            "path": "/usr/local/etc/xray/geoip.dat"
          },
        
          // Geosite 配置:指定 geosite.dat 文件路径
          "geosite": {
            "path": "/usr/local/etc/xray/geosite.dat"
          }
        }
      

9. Xray 常用命令

启动: sudo systemctl start xray
关闭: sudo systemctl stop xray
重启: sudo systemctl restart xray
查看状态: sudo systemctl status xray

10. 测试代理

curl -v --proxy socks5://127.0.0.1:1080 https://www.google.com
curl -v --proxy http://127.0.0.1:8080 https://www.google.com

11. 系统启动时自动启动 Xray

sudo systemctl enable xray

12. 卸载 Xray

rm -r /usr/local/bin/xray
rm -r /usr/local/etc/xray

重要提示

确保你在配置文件中使用的端口号和协议与服务器端配置匹配,避免连接问题。