自建科学上网服务器:V2Ray + WebSocket + TLS

自建科学上网服务器 - 通过V2Ray + WebSocket + TLS实现科学上网

📋 目录

🚀 V2Ray简介

V2Ray是一个多功能的网络代理工具,支持多种协议和传输方式。它可以在客户端和服务器之间建立加密的连接,实现科学上网。

核心特性

  • 多协议支持:VMess、VLESS、Shadowsocks、Trojan等
  • 灵活路由:支持复杂的路由规则配置
  • 传输安全:内置多种加密和混淆方式
  • 平台兼容:支持Windows、macOS、Linux、Android、iOS

🌟 WebSocket+TLS的优势

WebSocket和TLS都提供了加密和传输数据的安全性。WebSocket是一种用于创建持久连接的协议,它可以在浏览器和Web服务器之间进行双向通信。TLS是一种安全传输层协议,它可以在网络传输中提供数据传输的安全性。

主要优势

  • 伪装性强:流量看起来像正常的HTTPS网站访问
  • 稳定性好:WebSocket连接更稳定,不易被检测
  • 安全性高:TLS加密保证数据传输安全
  • 兼容性强:大部分防火墙都允许HTTPS流量通过

🛠️ 部署步骤

1. 服务器准备

首先需要一台海外VPS服务器,推荐配置:

  • 系统:Ubuntu 20.04 LTS 或 CentOS 8
  • 内存:至少512MB
  • 带宽:至少1Mbps
  • 域名:一个已解析到服务器IP的域名

2. 安装V2Ray

1
2
3
4
5
# 使用官方安装脚本
bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)

# 或者使用一键安装脚本
wget -N --no-check-certificate -q -O install.sh "https://raw.githubusercontent.com/wulabing/V2Ray_ws-tls_bash_onekey/master/install.sh" && chmod +x install.sh && bash install.sh

3. 配置V2Ray服务端

创建配置文件 /usr/local/etc/v2ray/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
{
"inbounds": [
{
"port": 10000,
"listen": "127.0.0.1",
"protocol": "vmess",
"settings": {
"clients": [
{
"id": "你的UUID",
"alterId": 0
}
]
},
"streamSettings": {
"network": "ws",
"wsSettings": {
"path": "/your-path"
}
}
}
],
"outbounds": [
{
"protocol": "freedom",
"settings": {}
}
]
}

4. 安装和配置Nginx

1
2
3
4
5
6
# 安装Nginx
sudo apt update
sudo apt install nginx

# 配置Nginx
sudo nano /etc/nginx/sites-available/v2ray

Nginx配置内容:

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
server {
listen 80;
server_name your-domain.com;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl http2;
server_name your-domain.com;

ssl_certificate /path/to/your/cert.pem;
ssl_certificate_key /path/to/your/private.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;

location /your-path {
proxy_redirect off;
proxy_pass http://127.0.0.1:10000;
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;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location / {
# 伪装网站内容
root /var/www/html;
index index.html;
}
}

5. 申请SSL证书

使用Let’s Encrypt免费证书:

1
2
3
4
5
6
7
8
9
10
# 安装certbot
sudo apt install certbot python3-certbot-nginx

# 申请证书
sudo certbot --nginx -d your-domain.com

# 设置自动续期
sudo crontab -e
# 添加以下行
0 12 * * * /usr/bin/certbot renew --quiet

6. 启动服务

1
2
3
4
5
6
7
8
9
10
11
# 启动V2Ray
sudo systemctl start v2ray
sudo systemctl enable v2ray

# 重启Nginx
sudo systemctl restart nginx
sudo systemctl enable nginx

# 检查服务状态
sudo systemctl status v2ray
sudo systemctl status nginx

📱 客户端配置

Windows客户端(V2RayN)

  1. 下载V2RayN客户端
  2. 添加VMess服务器配置:
    • 地址:your-domain.com
    • 端口:443
    • 用户ID:你的UUID
    • 额外ID:0
    • 加密方式:auto
    • 传输协议:ws
    • 路径:/your-path
    • TLS:开启

Android客户端(V2RayNG)

配置参数与Windows客户端相同,在Google Play或GitHub下载V2RayNG。

iOS客户端(Shadowrocket)

  1. 在App Store购买Shadowrocket
  2. 添加服务器配置,参数与上述相同

macOS客户端(V2RayU)

从GitHub下载V2RayU,配置方法与Windows客户端类似。

🔧 性能优化

1. BBR加速

1
2
3
4
5
6
7
8
9
10
11
# 检查内核版本
uname -r

# 开启BBR
echo 'net.core.default_qdisc=fq' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_congestion_control=bbr' >> /etc/sysctl.conf
sysctl -p

# 验证BBR是否开启
sysctl net.ipv4.tcp_congestion_control
lsmod | grep bbr

2. 系统优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 增加文件描述符限制
echo '* soft nofile 51200' >> /etc/security/limits.conf
echo '* hard nofile 51200' >> /etc/security/limits.conf

# 优化网络参数
cat >> /etc/sysctl.conf << EOF
net.ipv4.tcp_fastopen = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
EOF

sysctl -p

🛡️ 安全建议

1. 防火墙配置

1
2
3
4
5
6
7
8
9
10
# 安装ufw
sudo apt install ufw

# 配置防火墙规则
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

2. 定期更新

1
2
3
4
5
# 定期更新系统
sudo apt update && sudo apt upgrade -y

# 更新V2Ray
bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)

3. 监控和日志

1
2
3
4
5
6
# 查看V2Ray日志
sudo journalctl -u v2ray -f

# 查看Nginx日志
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log

❓ 常见问题

Q1: 连接失败怎么办?

检查步骤:

  1. 确认服务器防火墙设置
  2. 检查域名DNS解析
  3. 验证SSL证书是否有效
  4. 确认V2Ray和Nginx服务状态
1
2
3
4
5
6
# 检查端口监听
sudo netstat -tlnp | grep :443
sudo netstat -tlnp | grep :10000

# 测试SSL证书
openssl s_client -connect your-domain.com:443

Q2: 速度慢怎么优化?

优化方案:

  1. 开启BBR加速
  2. 选择更近的服务器节点
  3. 调整V2Ray配置参数
  4. 使用CDN加速

Q3: 如何更换端口?

修改V2Ray配置文件中的端口号,同时更新Nginx配置和防火墙规则。

Q4: 证书过期怎么办?

1
2
3
4
5
# 手动续期
sudo certbot renew

# 检查证书有效期
sudo certbot certificates

Q5: 如何添加多用户?

在V2Ray配置文件的clients数组中添加更多用户:

1
2
3
4
5
6
7
8
9
10
"clients": [
{
"id": "用户1的UUID",
"alterId": 0
},
{
"id": "用户2的UUID",
"alterId": 0
}
]

📊 监控脚本

创建简单的监控脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/bin/bash
# v2ray-monitor.sh

# 检查V2Ray服务状态
if ! systemctl is-active --quiet v2ray; then
echo "V2Ray服务异常,正在重启..."
systemctl restart v2ray
fi

# 检查Nginx服务状态
if ! systemctl is-active --quiet nginx; then
echo "Nginx服务异常,正在重启..."
systemctl restart nginx
fi

# 检查证书有效期
cert_days=$(openssl x509 -in /etc/letsencrypt/live/your-domain.com/cert.pem -noout -dates | grep notAfter | cut -d= -f2 | xargs -I {} date -d {} +%s)
current_days=$(date +%s)
days_left=$(( (cert_days - current_days) / 86400 ))

if [ $days_left -lt 30 ]; then
echo "SSL证书将在${days_left}天后过期,建议续期"
fi

设置定时任务:

1
2
3
4
5
# 添加到crontab
crontab -e

# 每5分钟检查一次
*/5 * * * * /path/to/v2ray-monitor.sh

🎯 总结

通过V2Ray + WebSocket + TLS的组合,我们可以搭建一个稳定、安全、难以被检测的科学上网服务器。关键要点:

  1. 选择合适的VPS:稳定的网络和充足的带宽
  2. 正确配置域名和证书:确保TLS加密正常工作
  3. 定期维护更新:保持系统和软件的最新状态
  4. 合理使用:遵守当地法律法规

注意事项:

  • 本教程仅供技术学习交流使用
  • 请遵守当地法律法规
  • 不要用于非法用途
  • 建议定期备份配置文件

📚 参考资源