V2Ray 配置 Cloudflare + WebSocket + TLS + Nginx
域名解析
- 在 Cloudflare 中添加需要的域名 Content 为 VPS 的 IP 地址
- 若 IP 或端口被墙,可以单击灰色的云朵切换成黄色的 (Proxied 模式)
设置 SSL 证书
WARNING
若未打开云朵 (Proxied 模式),请跳过本步骤,直接使用 acme.sh 或面板自带功能申请 3 个月的证书
- 设置 SSL 模式为 Full (strict)
- 申请 SSL 证书
- 添加站点并启用 SSL
安装 V2Ray
安装与更新
sh
# 安装与更新 V2Ray
bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)
# 安装最新发行的 geoip.dat 和 geosite.dat
bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-dat-release.sh)
移除
sh
# 移除 V2Ray
bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh) --remove
# 移除配置文件以及日志
rm -rf /usr/local/etc/v2ray /var/log/v2ray
配置反向代理
在 nginx 配置文件添加以下代码
nginx
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 $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
配置服务端
id
后的 UUID 可在 UUID Generator 生成- 防火墙放行 10086 端口
json
// /usr/local/etc/v2ray/config.json
{
"log": {
"loglevel": "waring",
"access": "/var/log/v2ray/access.log",
"error": "/var/log/v2ray/error.log"
},
"inbounds": [
{
"listen": "127.0.0.1",
"port": 10086,
"protocol": "vmess",
"settings": {
"clients": [
{
"id": "<UUID>",
"alterId": 0
}
]
},
"streamSettings": {
"network": "ws",
"wsSettings": {
"path": "/ray"
}
}
}
],
"outbounds": [{ "protocol": "freedom" }]
}
配置客户端
Clash
yaml
- name: Clash
type: vmess
network: ws
server: domain.com
port: 443
uuid: <UUID>
alterId: 0
cipher: auto
tls: true
udp: true
ws-opts:
headers:
Host: domain.com
path: /ray
更多用法
- 优选 IP:将
server
修改为 Cloudflare IP - 自定义端口
- ngxin 监听自定义端口
listen 2053 ssl;
- 防火墙放行该端口
- 将
port
修改为该端口
- ngxin 监听自定义端口
- 关闭 TLS
- 关闭强制 HTTPS
- 将
port
修改为 80 - 将
tls
修改为 false
常用命令
- 设置开机启动
systemctl enable v2ray
- 启动 V2Ray
systemctl start v2ray
- 关闭 V2Ray
systemctl stop v2ray
- 重启 V2Ray
systemctl restart v2ray
- 检查运行状况
systemctl status v2ray
预览: