XRay 服务器搭建:Vless + TLS 配置指南

450 次浏览

VLESS + TLS 相比 VLESS + Reality 可以添加反向代理实现内网穿透,但是需要域名和自签证书。

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、获取SSL证书(使用 Let’s Encrypt)

安装 Certbot

sudo apt install certbot

1) HTTP-01方式(需要80端口)

80 端口必须空闲(如果 Xray/Nginx 占用,需要先停掉)

sudo systemctl stop nginx
sudo systemctl stop xray

运行以下命令申请证书:

sudo certbot certonly --standalone -d example.com

按提示输入邮箱完成申请。

自动续签:

sudo certbot renew --dry-run

设置成功后返回Success。

2) DNS-01方式(80端口不可用时,如家庭宽带)

运行以下命令申请证书:

sudo certbot certonly --manual --preferred-challenges dns -d example.com

按提示输入邮箱后在DNS服务商(如阿里云)创建 TXT 记录

HOST:_acme-challenge.example.com

检查解析记录是否生效,需另开一个SSH窗口:

dig TXT _acme-challenge.example.com

解析记录生效后返回对话框输入回车确认。

DNS-01方式无法自动续签,需手动续签。

签发证书并保存到:

/etc/letsencrypt/live/your-domain.com/fullchain.pem
/etc/letsencrypt/live/your-domain.com/privkey.pem

证书权限修改(否则XRay可能报错)

# 将证书目录的实际文件设为 root 所有但允许其他用户读取
sudo chmod 644 /etc/letsencrypt/archive/example.com/*.pem

# 为 Let‘s Encrypt 主目录添加其他用户执行权限
sudo chmod o+x /etc/letsencrypt/live
sudo chmod o+x /etc/letsencrypt/archive

# 为域名证书所在的具体目录添加执行权限
sudo chmod o+x /etc/letsencrypt/live/example.com
sudo chmod o+x /etc/letsencrypt/archive/example.com

4、安装XRay

下载并执行官方安装脚本

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

5、XRay配置

生成UUID

cat /proc/sys/kernel/random/uuid

使用文本编辑器(如 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,
      "protocol": "vless",
      "settings": {
        "clients": [
          {
            "id": "UUID",           // 替换为生成的 UUID
            "flow": "xtls-rprx-vision" // 启用 Vision 流控
          }
        ],
        "decryption": "none",
        "fallbacks": [              // 回落到防探测的本地web服务
          {
            "dest": "80"
          }
        ]
      },
      "sniffing": {
        "enabled": true,
        "destOverride": ["http", "tls", "quic"]
      },
      "streamSettings": {
        "network": "tcp",
        "security": "tls",
        "tlsSettings": {
          "rejectUnknownSni": true,
          "certificates": [
            {
              "certificateFile": "/etc/letsencrypt/live/your-domain.com/fullchain.pem",
              "keyFile": "/etc/letsencrypt/live/your-domain.com/privkey.pem"
            }
          ]
        }
      }
    }
  ],

  "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

6、配置防火墙(可选)

允许 SSH, HTTP, HTTPS

sudo ufw allow 22
sudo ufw allow 443

开启防火墙

sudo ufw enable

7、客户端配置

配置内容如下:

{
  "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": "yourdomain.com",   // 服务器域名
            "port": 443,
            "users": [
              {
                "id": "UUID",               // 和服务器端的一致
                "flow": "xtls-rprx-vision",
                "encryption": "none"
              }
            ]
          }
        ]
      },
      "streamSettings": {
        "network": "tcp",
        "security": "tls",
        "tlsSettings": {
          "serverName": "yourdomain.com", // 替换成你的真实域名
          "allowInsecure": false          // 禁止不安全证书
        }
      }
    }
  ],

  "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:填写服务器域名

参考资料

https://xtls.github.io/
https://github.com/XTLS/Xray-examples

发表评论

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

滚动至顶部