01使用 VMess、WebSocket、Nginx 与 TLS 部署 Xray

在 Debian 或 Ubuntu VPS 上配置 Xray VMess、WebSocket、Nginx reverse proxy、DNS 与 TLS,并连接 V2RayN 客户端。

2025-11-12
VPSXrayVMessWebSocketNginxTLS
本章目录 · 14

这套部署将 Xray 只绑定在服务器本机端口,由 Nginx 接收公网 HTTPS 流量,并把指定 WebSocket path 转发给 Xray。

V2RayN client
      ↓ TLS :443
DNS → Nginx
      ↓ WebSocket /ray
Xray 127.0.0.1:10086

Internet

它涉及 DNS、certificate、reverse proxy 与 Xray 四层配置。排错时应逐层确认,不要把所有连接失败都归因于 Xray core。

仅在拥有管理权的服务器上部署,并遵守服务商条款与所在地适用规则。配置中不得公开真实 UUID、服务器地址或其他访问凭据。

准备内容

  • 一台 Debian 或 Ubuntu VPS;
  • 一个可管理 DNS records 的 domain;
  • 指向 VPS 的 subdomain,例如 vps.example.com
  • 本地 SSH client;
  • Windows 上的 V2RayN 或其他兼容客户端。

本文使用占位值:

Server IP: 203.0.113.10
Domain: vps.example.com
UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
WebSocket path: /ray
Local Xray port: 10086

配置 DNS

在 DNS provider 中添加 A record:

字段 示例
Type A
Name vps
Content 203.0.113.10
Proxy status DNS only

使用 DNS only 可以让 domain 直接解析到 VPS,便于 certificate validation、端口测试和真实链路延迟判断。

等待解析后,在本地检查:

Resolve-DnsName vps.example.com

结果应指向当前 VPS IP。DNS 仍指向旧 IP 时,不要继续申请 certificate 或判断服务故障。

登录并安装组件

ssh [email protected]

更新系统并安装 Nginx、Certbot 与编辑器:

apt update
apt upgrade -y
apt install nginx python3-certbot-nginx nano curl -y

使用 XTLS installation script 安装 Xray:

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

从网络直接执行脚本会把安装权限交给远程内容。正式环境应确认来源、查看脚本,并记录实际安装的 Xray version:

xray version
systemctl status xray

配置 Xray

生成 UUID:

cat /proc/sys/kernel/random/uuid

备份已有配置:

cp /usr/local/etc/xray/config.json /usr/local/etc/xray/config.json.backup

编辑:

nano /usr/local/etc/xray/config.json
{
  "inbounds": [
    {
      "port": 10086,
      "listen": "127.0.0.1",
      "protocol": "vmess",
      "settings": {
        "clients": [
          {
            "id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "alterId": 0
          }
        ]
      },
      "streamSettings": {
        "network": "ws",
        "wsSettings": {
          "path": "/ray"
        }
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "freedom"
    }
  ]
}

需要保持一致的三个值:

  • server 与 client 使用同一个 UUID;
  • Nginx 与 Xray 使用同一个 WebSocket path;
  • Nginx upstream 指向 Xray 的实际 local port。

验证配置并重启:

xray run -test -config /usr/local/etc/xray/config.json
systemctl restart xray
systemctl status xray

确认只监听本机:

ss -lntp | grep 10086

监听地址应为 127.0.0.1:10086,不需要把此端口暴露到公网。

申请 TLS certificate

先开放 SSH、HTTP 与 HTTPS:

ufw allow ssh
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
ufw status

启用 firewall 前先允许 SSH,并保持当前 SSH session 打开,避免错误规则导致无法重新连接。

停止 Nginx,让 standalone Certbot 使用 port 80:

systemctl stop nginx

申请 certificate:

certbot certonly \
  --standalone \
  -d vps.example.com \
  --non-interactive \
  --agree-tos \
  --email [email protected]

certificate 文件通常位于:

/etc/letsencrypt/live/vps.example.com/fullchain.pem
/etc/letsencrypt/live/vps.example.com/privkey.pem

检查 certificate:

certbot certificates

配置 Nginx reverse proxy

备份默认配置:

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.backup
nano /etc/nginx/sites-available/default
server {
    listen 80;
    server_name vps.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name vps.example.com;

    ssl_certificate /etc/letsencrypt/live/vps.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/vps.example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;

    location / {
        root /var/www/html;
        index index.html;
    }

    location /ray {
        if ($http_upgrade != "websocket") {
            return 404;
        }

        proxy_pass http://127.0.0.1:10086;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

将所有 vps.example.com 替换为实际 domain,并确保 /ray 与 Xray config 完全一致。

测试 Nginx configuration:

nginx -t

只有测试成功后才启动:

systemctl start nginx
systemctl enable nginx
systemctl enable xray

配置 V2RayN

添加 VMess server:

客户端字段
Address vps.example.com
Port 443
User ID 与 Xray config 相同的 UUID
Alter ID 0
Transport ws
Path /ray
TLS enabled

地址填写 domain,不要在 TLS configuration 中直接改用 IP;certificate 是为 domain 签发的。

分层排查

DNS

Resolve-DnsName vps.example.com
ipconfig /flushdns

确认 domain 指向当前实例。若使用 CDN proxy,延迟和入口 IP 测试反映的是 CDN,而不一定是 VPS 本身。

Public ports

Test-NetConnection vps.example.com -Port 443

端口不可达时检查 provider firewall、UFW、Nginx 是否监听以及 DNS 是否正确。

Nginx

systemctl status nginx
nginx -t
journalctl -u nginx -n 100 --no-pager
ss -lntp | grep ':443'

Xray

systemctl status xray
journalctl -u xray -n 100 --no-pager
ss -lntp | grep 10086

Configuration alignment

最后逐项比较 client 与 server:domain、port、UUID、transport、WebSocket path 和 TLS。端口能连通只证明 TCP connection 能建立,不证明后续 TLS、WebSocket 与 VMess negotiation 成功。

备份与迁移

重新创建 VPS 前,至少保存:

  • /usr/local/etc/xray/config.json 的无密钥模板;
  • Nginx site configuration;
  • package 与 Xray versions;
  • domain 和 DNS record 设计;
  • certificate 重新申请步骤;
  • client fields 对照表。

UUID 等访问凭据应单独安全保存,不应提交到公开 repository。迁移到新 IP 后先更新 DNS,再逐层验证 certificate、Nginx 与 Xray。