搭建VLESS


Xray + Nginx + VLESS + WebSocket(本教程分为使用域名和免域名两部分)
由于是海外服务器不用域名备案

一、整体架构概览

1
2
3
4
5
6
7
[ 手机 / 电脑客户端 ]
↓(TLS 加密的 HTTPS 请求)
[ 443 端口 | Nginx ]
↓(反向代理转发)
[ 127.0.0.1:4589 | Xray VLESS ]
↓(freedom 出站)
[ 服务器公网出口 → Internet ]
组件 功能 路径 / 端口
Nginx 负责 TLS 加密、反代到 Xray、隐藏节点特征 443 (外网可见)
Xray core VLESS 服务核心,处理加密、转发 127.0.0.1:4589
Let’s Encrypt 自动签发和续期 TLS 证书 /etc/letsencrypt/live/域名/
systemd 管理 Xray、Nginx 自启动 /etc/systemd/system/xray.service
防火墙(ufw) 允许必要端口、屏蔽其他流量 80/443 放行
客户端(qv2ray/NekoBox)等等 用户端连接工具 TLS + WS
文件 / 目录 功能
/opt/vless/xray/xray Xray 主程序二进制文件
/opt/vless/xray/config.json 主配置文件(VLESS + WS)
/etc/nginx/conf.d/xray.conf Nginx 反代配置文件
/etc/letsencrypt/live/域名/ TLS 证书路径
/etc/systemd/system/xray.service systemd 服务定义
/var/log/xray/access.log 访问日志
/var/log/xray/error.log 错误日志

[!IMPORTANT]

这里我的海外服务器

雨云

用哪里都是一样的,要放行端口(443)

域名需要解析,将自己的服务器的公网IP记录进去即可

二、安装依赖

1
apt update
1
apt upgrade
1
apt install curl wget vim unzip tar socat nginx certbot python3-certbot-nginx -y
1
2
3
# 创建vless项目
mkdir -p /opt/vless/xray
cd /opt/vless/xray
1
wget https://github.com/XTLS/Xray-core/releases/download/v25.10.15/Xray-linux-64.zip
1
unzip Xray-linux-64.zip
1
chmod +x xray

三、配置环境

1.读取内核生成的UUID(每次执行都有不一样的UUID)
1
cat /proc/sys/kernel/random/uuid

[!IMPORTANT]

比如:(你有多少台设备就执行几次,或者先执行一次测试一台设备为主,后续在配置文件中加)

第一次执行:111111111

第二次执行:222222222

2.创建日志
1
mkdir -p /var/log/xray
1
chmod -R 755 /var/log/xray
3.Xray 配置
1
vim /opt/vless/xray/config.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{
"log": {
"loglevel": "info",
"access": "/var/log/xray/access.log",
"error": "/var/log/xray/error.log"
},
"dns": {
"servers": [
"https+local://1.1.1.1/dns-query",
"8.8.8.8",
"localhost"
]
},
"inbounds": [
{
"port": 4589,
"listen": "127.0.0.1",
"protocol": "vless",
"settings": {
"clients": [
{"id": "111111111","level": 0,"email": "备注"},
{"id": "222222222","level": 0,"email": "备注"}

],
"decryption": "none"
},
"streamSettings": {
"network": "ws",
"security": "none",
"wsSettings": {
"path": "/ray"
}
}
}
],
"outbounds": [
{
"protocol": "freedom",
"settings": {}
},
{
"protocol": "blackhole",
"settings": {},
"tag": "blocked"
}
],
"routing": {
"domainStrategy": "AsIs",
"rules": [
{
"type": "field",
"outboundTag": "blocked",
"ip": ["geoip:private"]
}
]
}
}

[!TIP]

解读:

日志设置(log)

  • 记录 Xray 的访问日志和错误日志,帮助排查问题
  • 访问日志存储在 /var/log/xray/access.log,错误日志存储在 /var/log/xray/error.log

DNS 设置(dns)

  • 使用 Cloudflare 的 DoH (1.1.1.1) 和 Google DNS (8.8.8.8) 作为 DNS 服务器
  • 本地 DNS 回退配置(localhost

入站连接(inbounds)

  • 端口:Xray 服务监听 127.0.0.1:4589,仅本地能访问
  • 协议:使用 VLESS 协议,适合无特征、轻量级连接
  • 客户端设置
    • id(UUID)表示客户端唯一标识符
    • level0 表示普通权限
    • email 是一个备注字段,可以随便填写
  • WebSocket 配置:使用 WebSocket 协议(ws),路径为 /ray

出站连接(outbounds)

  • freedom 协议:允许流量直接访问外网
  • blackhole 协议:阻止流量访问特定 IP,通常用于屏蔽内网流量

路由规则(routing)

  • 匹配规则:将私有 IP 地址(如 10.x.x.x, 192.168.x.x)的流量丢弃(使用 blackhole
  • domainStrategy 设置为 AsIs,表示使用原始的域名处理策略
4.Nginx 配置

[!NOTE]

这里需要生成HTTPS证书,详细内容可以看一下申请HTTPS证书

1
vim /etc/nginx/conf.d/xray.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
server {
listen 80;
server_name 你的域名;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl http2;
server_name 你的域名;

ssl_certificate /etc/letsencrypt/live/你的域名/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/你的域名/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;

root /var/www/html;
index index.html;

location /ray {
proxy_redirect off;
proxy_pass http://127.0.0.1:4589;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}

[!NOTE]

解读:

  • 80 → 强制重定向到 HTTPS
  • 443 → 提供加密访问
  • /ray → 转发给 Xray 内部服务
  • proxy_pass http://127.0.0.1:4589 → Xray 实际监听点
  • 其余 header 用于 WebSocket 升级
5.systemd 服务
1
vim /etc/systemd/system/xray.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[Unit]
Description=Xray Service
After=network.target nss-lookup.target

[Service]
User=root
ExecStart=/opt/vless/xray/xray run -config /opt/vless/xray/config.json
Restart=on-failure
RestartPreventExitStatus=23
LimitNOFILE=51200
WorkingDirectory=/opt/vless/xray
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=xray

[Install]
WantedBy=multi-user.target
1
systemctl daemon-reload
1
nginx -t
1
systemctl enable --now nginx.service
1
systemctl enable --now xray.service

测试

1
ss -tuln | grep 4589
1
ss -tuln | grep 443

回复:LISTEN 0 等等信息就算成功

四、客户端配置

项目 内容
配置名称 / 备注 设备名字
服务器 (Address / Server) 你的域名
端口 (Port) 443
用户 ID (UUID) 内核生成UUID
加密 (Encryption) none
传输协议 (Network / Transport) ws
WebSocket 路径 (Path) /ray
WebSocket Host (Header) 你的域名
TLS 开启
SNI 你的域名
uTLS 指纹 chrome(或者空)

[!TIP]

可以手动添加vless,也可以编辑下面链接导入

1
vless://内核生成UUID@你的域名:443?type=ws&security=tls&path=%2Fray&host=你的域名&sni=你的域名#服务器位置

五、安全建议(可跳过)

1.更换路径

/ray → 可改为更隐蔽的如 /cdn-update/video-api

如果更换需要修改

1
vim /opt/vless/xray/config.json
1
vim /etc/nginx/conf.d/xray.conf 
2.开启防火墙
1
2
3
ufw allow 22/tcp
ufw allow 80,443/tcp
ufw enable
3.伪装网站

/var/www/html/index.html 换成一个正常网页(比如博客首页)

4.定期更新
1
apt update && apt upgrade -y

六、不使用域名修改以下部分

假设你不使用域名,直接通过 服务器的 IP 地址 来连接

1.Nginx 配置:

Nginx 配置中就不能再使用 SNI

  1. 去掉 SNI 相关的配置
  2. 无法使用 HTTPS,改成 HTTP,或者使用本地 IP 进行连接

修改前(使用域名):

1
vim /etc/nginx/conf.d/xray.conf 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
server {
listen 80;
server_name 你的域名;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl http2;
server_name 你的域名;

ssl_certificate /etc/letsencrypt/live/你的域名/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/你的域名/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;

root /var/www/html;
index index.html;

location /ray {
proxy_redirect off;
proxy_pass http://127.0.0.1:4589;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}

修改后(使用 IP):

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80; # HTTP,不再使用 HTTPS
server_name 192.168.1.100; # 直接使用服务器 IP

location /ray {
proxy_redirect off;
proxy_pass http://127.0.0.1:4589;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
}
  • 改用 HTTP(没有 TLS 加密),如果不使用域名就无法使用 TLS
  • server_name 改为服务器的 IP 地址
2.Xray 配置:

需要修改 config.json 来适应不使用域名的情况,如果使用 HTTP 而不是 WSS(加密 WebSocket),security 字段需要设置为 none

修改前(使用域名):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{
"log": {
"loglevel": "info",
"access": "/var/log/xray/access.log",
"error": "/var/log/xray/error.log"
},
"dns": {
"servers": [
"https+local://1.1.1.1/dns-query",
"8.8.8.8",
"localhost"
]
},
"inbounds": [
{
"port": 4589,
"listen": "127.0.0.1",
"protocol": "vless",
"settings": {
"clients": [
{"id": "111111111","level": 0,"email": "备注"},
{"id": "222222222","level": 0,"email": "备注"}

],
"decryption": "none"
},
"streamSettings": {
"network": "ws",
"security": "none",
"wsSettings": {
"path": "/ray"
}
}
}
],
"outbounds": [
{
"protocol": "freedom",
"settings": {}
},
{
"protocol": "blackhole",
"settings": {},
"tag": "blocked"
}
],
"routing": {
"domainStrategy": "AsIs",
"rules": [
{
"type": "field",
"outboundTag": "blocked",
"ip": ["geoip:private"]
}
]
}
}

修改后(使用 IP):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
"log": {
"loglevel": "info",
"access": "/var/log/xray/access.log",
"error": "/var/log/xray/error.log"
},
"inbounds": [{
"port": 4589,
"listen": "127.0.0.1",
"protocol": "vless",
"settings": {
"clients": [
{"id": "111111111","level": 0,"email": "备注"},
{"id": "222222222","level": 0,"email": "备注"}
],
"decryption": "none"
},
"streamSettings": {
"network": "ws",
"security": "none", // 不使用加密
"wsSettings": {
"path": "/ray"
}
}
}],
"outbounds": [{ "protocol": "freedom" }]
}
  • security: 设置为 none,因为没有 TLS 加密,WebSocket 使用普通 ws 协议

[!CAUTION]

其他内容都是一样的


搭建VLESS
https://huishao.net/2025/10/22/搭建VLESS/
作者
huishao
发布于
2025年10月22日
许可协议