代理服务器搭建完整指南
本指南将详细介绍如何在VPS上搭建各种类型的代理服务器,包括V2Ray、Shadowsocks、Trojan等流行方案,以及Nginx反向代理的配置。您将学习到从基础安装到高级优化的完整流程。
什么是代理服务器
代理服务器是位于客户端和目标服务器之间的中间服务器,它接收客户端的请求,然后将请求转发给目标服务器,再将响应返回给客户端。
代理服务器的作用
隐私保护
隐藏真实IP地址,保护用户隐私
访问控制
突破地理限制,访问特定资源
加速访问
通过缓存和优化路由提升速度
安全防护
过滤恶意内容,增强网络安全
负载均衡
分发请求到多个服务器
流量监控
记录和分析网络使用情况
代理工作原理
客户端向代理服务器发送请求
代理服务器转发请求到目标服务器
目标服务器返回响应给代理
代理将响应返回给客户端
代理类型对比
选择合适的代理协议对于搭建稳定、高效的代理服务至关重要。下面我们对比几种主流的代理方案:
主流代理方案对比
V2Ray
主要特点:
- 多协议支持
- 强大的路由功能
- 流量伪装
- CDN中转
Shadowsocks
主要特点:
- 轻量级
- 速度快
- 配置简单
- 广泛支持
Trojan
主要特点:
- 伪装HTTPS
- 难以检测
- 稳定性高
- 穿透力强
HTTP/SOCKS5
主要特点:
- 标准协议
- 兼容性好
- 配置简单
- 适合内网
选择建议
🏆 新手推荐
Shadowsocks
- 配置简单,易于上手
- 客户端支持广泛
- 速度快,资源占用少
⚡ 性能优先
V2Ray (VLESS)
- 协议先进,性能优秀
- 支持多种传输方式
- 可配置CDN加速
🔒 安全优先
Trojan + TLS
- 伪装成HTTPS流量
- 难以被检测和封锁
- 适合高敏感环境
V2Ray搭建
V2Ray是一个功能强大的代理软件,支持多种协议和传输方式,具有强大的路由功能。
准备工作
安装步骤
安装V2Ray
使用官方脚本一键安装:
bash <(curl -L https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh)
生成UUID
生成用于认证的UUID:
cat /proc/sys/kernel/random/uuid
记录生成的UUID,稍后配置时使用
配置V2Ray
编辑配置文件:
nano /usr/local/etc/v2ray/config.json
基础VMess配置示例:
{ "inbounds": [{ "port": 10086, "protocol": "vmess", "settings": { "clients": [{ "id": "YOUR-UUID-HERE", "level": 1, "alterId": 0 }] }, "streamSettings": { "network": "tcp", "tcpSettings": { "header": { "type": "http", "response": { "version": "1.1", "status": "200", "reason": "OK" } } } } }], "outbounds": [{ "protocol": "freedom", "settings": {} }] }
启动V2Ray服务
systemctl start v2ray
systemctl enable v2ray
systemctl status v2ray
配置防火墙
开放V2Ray端口:
ufw allow 10086/tcp
ufw reload
高级配置
WebSocket + TLS配置
使用WebSocket传输并配置TLS加密:
{ "inbounds": [{ "port": 443, "protocol": "vmess", "settings": { "clients": [{ "id": "YOUR-UUID-HERE", "alterId": 0 }] }, "streamSettings": { "network": "ws", "wsSettings": { "path": "/ray" }, "security": "tls", "tlsSettings": { "certificates": [{ "certificateFile": "/path/to/cert.pem", "keyFile": "/path/to/key.pem" }] } } }], "outbounds": [{ "protocol": "freedom" }] }
CDN加速配置
配合Cloudflare CDN使用:
- 将域名解析到Cloudflare
- 开启CDN代理(橙色云朵)
- SSL/TLS设置为"Full"
- 配置V2Ray使用WebSocket + TLS
- 端口使用Cloudflare支持的端口(443, 8443等)
路由规则配置
智能分流,提升访问速度:
"routing": { "rules": [ { "type": "field", "domain": ["geosite:cn"], "outboundTag": "direct" }, { "type": "field", "ip": ["geoip:cn", "geoip:private"], "outboundTag": "direct" } ] }
Shadowsocks部署
Shadowsocks是一个轻量级的代理工具,以其简单、高效而广受欢迎。
Shadowsocks-libev安装
更新系统并安装
apt update && apt upgrade -y
apt install shadowsocks-libev -y
配置Shadowsocks
创建配置文件:
nano /etc/shadowsocks-libev/config.json
{ "server": "0.0.0.0", "server_port": 8388, "password": "your_strong_password_here", "timeout": 300, "method": "aes-256-gcm", "fast_open": true, "mode": "tcp_and_udp" }
启动服务
systemctl restart shadowsocks-libev
systemctl enable shadowsocks-libev
systemctl status shadowsocks-libev
开放端口
ufw allow 8388
ufw reload
优化配置
🚀 开启BBR加速
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
sysctl -p
🔒 使用插件混淆
安装simple-obfs插件:
apt install simple-obfs -y
配置文件添加插件选项:
"plugin": "obfs-server", "plugin_opts": "obfs=http;obfs-host=www.bing.com"
📊 多用户配置
支持多个端口和密码:
"port_password": { "8388": "password1", "8389": "password2", "8390": "password3" }
Trojan配置
Trojan是一种将流量伪装成HTTPS的代理协议,具有极强的抗检测能力。
前置要求
域名配置
- 拥有一个域名
- 域名解析到VPS IP
- 申请SSL证书
证书申请
使用Let's Encrypt申请免费证书:
apt install certbot -y
certbot certonly --standalone -d your-domain.com
安装Trojan-Go
下载Trojan-Go
wget https://github.com/p4gefau1t/trojan-go/releases/download/v0.10.6/trojan-go-linux-amd64.zip
unzip trojan-go-linux-amd64.zip -d /usr/local/bin/trojan-go
创建配置文件
nano /usr/local/bin/trojan-go/config.json
{ "run_type": "server", "local_addr": "0.0.0.0", "local_port": 443, "remote_addr": "127.0.0.1", "remote_port": 80, "password": [ "your_password_here" ], "ssl": { "cert": "/etc/letsencrypt/live/your-domain.com/fullchain.pem", "key": "/etc/letsencrypt/live/your-domain.com/privkey.pem", "sni": "your-domain.com" }, "websocket": { "enabled": true, "path": "/trojan", "host": "your-domain.com" } }
创建系统服务
nano /etc/systemd/system/trojan-go.service
[Unit] Description=Trojan-Go Service After=network.target [Service] Type=simple ExecStart=/usr/local/bin/trojan-go/trojan-go -config /usr/local/bin/trojan-go/config.json Restart=on-failure RestartSec=10 [Install] WantedBy=multi-user.target
启动Trojan服务
systemctl daemon-reload
systemctl start trojan-go
systemctl enable trojan-go
配置伪装网站
设置Nginx作为fallback,增强伪装效果:
apt install nginx -y
修改Nginx配置,监听80端口,部署一个真实网站。
Nginx反向代理
Nginx反向代理可以用于隐藏后端服务器、负载均衡、SSL终止等多种场景。
基础反向代理配置
安装Nginx
apt update
apt install nginx -y
配置反向代理
创建站点配置:
nano /etc/nginx/sites-available/proxy
server { listen 80; server_name your-domain.com; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # WebSocket支持 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
启用站点配置
ln -s /etc/nginx/sites-available/proxy /etc/nginx/sites-enabled/
nginx -t
systemctl restart nginx
高级配置选项
负载均衡配置
upstream backend { least_conn; # 最少连接数算法 server 10.0.0.1:8080 weight=3; server 10.0.0.2:8080 weight=2; server 10.0.0.3:8080 backup; } server { listen 80; server_name example.com; location / { proxy_pass http://backend; } }
SSL/TLS配置
server { listen 443 ssl http2; server_name your-domain.com; ssl_certificate /path/to/cert.pem; ssl_certificate_key /path/to/key.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; location / { proxy_pass http://backend; } }
缓存配置
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off; server { location / { proxy_cache my_cache; proxy_cache_valid 200 60m; proxy_cache_valid 404 1m; proxy_pass http://backend; } }
性能优化
通过系统级和应用级优化,可以显著提升代理服务器的性能和稳定性。
系统级优化
1. 开启BBR拥塞控制
BBR是Google开发的TCP拥塞控制算法,可以显著提升网络传输速度:
# 检查内核版本(需要4.9+)
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. 优化内核参数
调整系统参数以提升网络性能:
nano /etc/sysctl.conf
# 网络优化参数 net.ipv4.tcp_fastopen = 3 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 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 # 增加系统文件描述符限制 fs.file-max = 1000000 fs.inotify.max_user_instances = 8192 # 增加网络缓冲区 net.core.rmem_max = 67108864 net.core.wmem_max = 67108864 net.ipv4.tcp_rmem = 4096 87380 67108864 net.ipv4.tcp_wmem = 4096 65536 67108864
sysctl -p
3. 优化文件描述符限制
nano /etc/security/limits.conf
* soft nofile 1000000 * hard nofile 1000000 root soft nofile 1000000 root hard nofile 1000000
应用级优化
V2Ray优化
- 使用VLESS协议代替VMess
- 关闭不必要的功能(如统计)
- 使用mKCP或QUIC传输
- 合理设置缓冲区大小
Shadowsocks优化
- 使用AEAD加密(如aes-256-gcm)
- 开启TCP Fast Open
- 使用多端口分流
- 配置合适的超时时间
Nginx优化
- 调整worker进程数
- 开启gzip压缩
- 配置缓存策略
- 使用HTTP/2
性能监控
系统监控工具
# 安装htop查看系统资源
apt install htop -y
# 安装vnstat监控流量
apt install vnstat -y
systemctl enable vnstat
# 查看网络连接数
ss -s
netstat -nat | awk '{print $6}' | sort | uniq -c
性能测试
# 测试网络速度
wget -O speedtest.py https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
python3 speedtest.py
# 测试延迟
ping -c 10 google.com
# 测试路由
traceroute google.com
安全加固
代理服务器的安全性至关重要,需要从多个层面进行加固。
安全措施
1. SSH安全配置
# 修改SSH端口
nano /etc/ssh/sshd_config
Port 22345 PermitRootLogin no PasswordAuthentication no PubkeyAuthentication yes MaxAuthTries 3 MaxSessions 5
systemctl restart sshd
2. 防火墙配置
# 配置UFW防火墙
ufw default deny incoming
ufw default allow outgoing
ufw allow 22345/tcp # SSH端口
ufw allow 443/tcp # HTTPS
ufw allow 80/tcp # HTTP
ufw allow 8388/tcp # Shadowsocks
ufw enable
3. Fail2ban防暴力破解
apt install fail2ban -y
nano /etc/fail2ban/jail.local
[DEFAULT] bantime = 3600 findtime = 600 maxretry = 5 [sshd] enabled = true port = 22345 [nginx-limit-req] enabled = true
systemctl restart fail2ban
4. 定期更新系统
# 设置自动更新
apt install unattended-upgrades -y
dpkg-reconfigure --priority=low unattended-upgrades
最佳安全实践
🔑 访问控制
- 使用强密码或密钥认证
- 限制用户访问权限
- 定期更换密码/密钥
- 使用IP白名单(如适用)
📊 日志监控
- 启用详细日志记录
- 定期检查异常活动
- 设置日志轮转
- 备份重要日志
🛡️ 流量管理
- 设置流量限制
- 防止DDoS攻击
- 监控异常流量
- 实施速率限制
客户端配置
选择合适的客户端并正确配置是使用代理服务的重要环节。
各平台客户端推荐
客户端配置示例
V2Ray客户端配置
{ "v": "2", "ps": "我的VPS", "add": "your-server-ip", "port": "10086", "id": "your-uuid", "aid": "0", "net": "tcp", "type": "none", "host": "", "path": "", "tls": "" }
可以生成为vmess://链接便于导入
Shadowsocks客户端配置
{ "server": "your-server-ip", "server_port": 8388, "password": "your-password", "method": "aes-256-gcm", "plugin": "obfs-local", "plugin_opts": "obfs=http;obfs-host=www.bing.com" }
Clash配置文件
proxies: - name: "my-proxy" type: vmess server: your-server-ip port: 10086 uuid: your-uuid alterId: 0 cipher: auto proxy-groups: - name: Proxy type: select proxies: - my-proxy - DIRECT rules: - MATCH,Proxy
使用技巧
🌐 PAC模式
使用PAC文件实现智能分流,国内直连,国外代理
📱 移动端优化
开启断线重连,选择合适的超时时间
🔄 订阅更新
使用订阅链接自动更新服务器配置
⚡ 延迟测试
定期测试节点延迟,选择最优服务器
一键安装脚本
为了简化部署流程,社区开发了许多优秀的一键安装脚本,可以快速搭建各种代理服务。以下是一些经过验证的优质项目:
⏺ Sing-box 全家桶
⭐ 热门推荐⏺ 甬哥 Sing-box
⏺ 甬哥 X-UI
使用建议
选择合适的脚本
新手推荐使用带Web面板的脚本(如3X-UI),便于管理;高级用户可选择命令行脚本,更加灵活。
安全注意事项
安装后立即修改默认密码,开启防火墙,定期更新脚本和系统。
性能优化
根据服务器配置选择合适的协议,低配服务器建议使用Shadowsocks,高配可选择Reality或Hysteria2。
定期维护
定期检查服务运行状态,更新脚本版本,备份配置文件,监控服务器资源使用情况。
脚本对比
脚本名称 | 管理方式 | 协议支持 | 难度等级 | 推荐场景 |
---|---|---|---|---|
Sing-box 全家桶 | 命令行 | 全协议 | 中等 | 追求最新协议 |
甬哥 Sing-box | 命令行菜单 | 主流协议 | 简单 | 新手友好 |
甬哥 X-UI | Web面板 | Xray协议 | 简单 | 可视化管理 |
3X-UI | Web面板 | 全协议 | 简单 | 企业级部署 |
故障排查
遇到问题时,系统化的排查方法能帮助快速定位和解决问题。
常见问题及解决方案
❌ 无法连接到代理服务器
可能原因:
- 服务未启动或崩溃
- 防火墙阻止连接
- 配置文件错误
- 端口被占用
排查步骤:
# 检查服务状态
systemctl status v2ray
# 检查端口监听
ss -tulpn | grep :10086
# 查看日志
journalctl -xe -u v2ray
# 测试配置文件
v2ray -test -config /usr/local/etc/v2ray/config.json
❌ 速度慢或不稳定
可能原因:
- 网络拥塞
- 服务器性能不足
- 配置未优化
- ISP限速
解决方法:
- 开启BBR加速
- 更换传输协议(如使用mKCP)
- 使用CDN中转
- 升级服务器配置
- 更换服务器地区
❌ 证书相关错误
常见错误:
- 证书过期
- 证书路径错误
- 权限问题
解决方案:
# 更新证书
certbot renew
# 检查证书权限
ls -la /etc/letsencrypt/live/
# 设置自动更新
crontab -e
# 添加:0 0 * * * certbot renew --quiet
❌ 客户端配置错误
检查要点:
- 服务器地址是否正确
- 端口号是否匹配
- 密码/UUID是否正确
- 加密方式是否一致
- 时间是否同步
调试技巧:
- 启用客户端调试模式
- 查看详细错误日志
- 使用简单配置测试
- 逐项排查配置项
诊断工具
网络诊断
# 测试TCP连接
telnet server-ip 10086
# 测试UDP连接
nc -u -v server-ip 10086
# 路由追踪
traceroute server-ip
# MTU探测
ping -M do -s 1472 server-ip
服务诊断
# 查看系统资源
htop
# 查看网络连接
ss -tunap
# 查看防火墙规则
iptables -L -n -v
ufw status verbose
🎯 最佳实践总结
🔒 安全性
- 使用强密码和复杂UUID
- 定期更新软件和系统
- 启用防火墙和Fail2ban
- 使用TLS加密传输
- 避免使用默认端口
⚡ 性能
- 开启BBR加速
- 优化系统参数
- 选择合适的加密算法
- 使用CDN中转(如适用)
- 定期监控服务器状态
🛠️ 维护
- 定期备份配置文件
- 监控日志和流量
- 设置自动重启机制
- 建立应急恢复方案
- 记录配置变更