安装V2Ray(命令行配置)
a. 安装所需软件
- Node.js:用于运行V2Ray。
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - sudo apt-get install -y nodejs
- npm:Node.js的包管理工具。
sudo apt-get install -y npm
b. 下载V2Ray源码
- 克隆仓库并进入目录。
git clone https://github.com/v2ray/v2ray.git cd v2ray
- 安装依赖项。
npm install
c. 创建配置文件
- 打开编辑器,创建或修改
config.json文件。{ "log": { "level": "info", "prefix": "[v2ray] " }, "proxy": { "server": "...", "listen": 108, "transparent": true, "upgrade": true, "socks": "...:1081" }, "inbound": { "listen": 108, "transparent": true, "socks": "...:1081", "fallback": "127...1:80" }, "outbound": { "server": "...", "listen": 108, "upgrade": true, "socks": "...:1081" } }根据需求调整配置参数,例如
server、listen、socks等。
d. 启动V2Ray
- 运行主进程。
node v2ray/server.js
- 在新终端窗口运行代理服务器。
node v2ray/client.js --socks 0...:1081 --target 127...1:108
或者直接运行配置文件。
node v2ray/client.js --config config.json
使用V2Ray GUI工具
a. 安装V2RayN(Windows/macOS)
- 下载并安装V2RayN,运行后输入服务器IP和端口,配置路由器信息。
- 点击“启动”开始使用。
b. 使用Xray(macOS/Linux)
- 安装Xray,打开配置文件,设置服务器IP、端口和路由器信息。
- 点击“Start”启动Xray。
使用Docker
a. 安装Docker
- 在Linux上安装Docker。
curl -fsSL https://get.docker.com | sudo -E bash - sudo systemctl restart docker
b. 拉取镜像和运行容器
- 拉取V2Ray镜像。
docker pull v2ray/v2ray
- 创建并启动容器。
docker run -d --name v2ray -p 108:108 -p 1081:1081 v2ray/v2ray
- 访问容器内部。
docker exec -it v2ray sh
c. 配置文件修改
- 修改Dockerfile中的配置,重新构建镜像。
FROM debian:latest RUN apt-get update && apt-get install -y nodejs npm COPY . . CMD ["node", "server.js"]
- 构建镜像并运行。
docker build -t v2ray:v2ray . docker run -d --name v2ray -p 108:108 -p 1081:1081 v2ray:v2ray
手动配置路由器或服务器
a. 路由器配置(Linux/Unix)
- 安装V2Ray。
curl -L https://raw.githubusercontent.com/v2ray/v2ray/master/install.sh | bash
- 启动V2Ray。
v2ray start
- 设置iptables。
iptables -P NAT PREROUTING -j REDIRECT --dport 80 -j REDIRECT --to-destination 127...1:108 iptables -P NAT POSTROUTING -j MASQUERADE iptables -P FORWARD -j ACCEPT
- 启动服务。
systemctl start v2ray systemctl enable v2ray
b. 服务器配置(Windows)
- 下载V2Ray,安装后设置服务器IP和端口。
- 配置Windows路由器,设置默认网关。
自动化配置(Shell脚本或Ansible)
a. Shell脚本
- 创建配置脚本。
#!/bin/bash echo "安装Node.js..." apt-get install -y nodejs npm echo "下载V2Ray..." git clone https://github.com/v2ray/v2ray.git cd v2ray npm install echo "创建配置文件..." nano config.json echo "启动V2Ray..." node server.js
- 执行脚本。
bash v2ray_install.sh
b. Ansible
-
创建Ansible角色文件。
--- name: v2ray hosts: all tasks: - name: 安装Node.js apt: name=nodejs state=present - name: 安装npm apt: name=npms state=present - name: 克隆V2Ray仓库 git: repo=https://github.com/v2ray/v2ray.git - name:安装依赖 shell: npm install - name: 创建配置文件 copy: src=config.json dest=/etc/v2ray/ notify: message: "配置文件已创建" -
运行Ansible任务。
ansible-playbook v2ray.yml
高级配置
a. 域名绑定
- 在DNS提供商注册域名,解析到服务器IP。
- 修改V2Ray配置文件中的
outbound设置,使用域名。"outbound": { "domain": "example.com", "server": "...", "listen": 108, "upgrade": true, "socks": "...:1081" }
b. SSL代理(Let's Encrypt)
- 获取SSL证书。
curl -sSL https://acquire.cloudflare.com/mirrors/acquire.pem | sudo mv -t /etc/ssl/v2ray/ curl -sSL https://acquire.cloudflare.com/mirrors/cert.pem | sudo mv -t /etc/ssl/v2ray/
- 修改V2Ray配置文件。
"outbound": { "server": "...", "listen": 108, "ssl": { "cert": "/etc/ssl/v2ray/cert.pem", "key": "/etc/ssl/v2ray/acquire.pem" }, "upgrade": true, "socks": "...:1081" }
c. 跨平台兼容
- 在不同的平台上使用不同的配置文件,确保兼容性。
d. 流量监控与限流
- 使用Squid或Traefik作为前置代理,监控流量。
# Squid配置 # /etc/squid/squid.conf cache_dir /var/cache/squid3 1024 cache_mem 32MB # Traefik配置 # /etc/traeфик/traeфик.json { "log": { "level": "debug" }, "accessLog": "access.log", "debug": true, "http": { "server": ":80", "compression": true }, "reverse_proxy": { "url": "http://localhost:108", "reverse_proxy_headers": ["X-Forwarded-Proto"], "stream_response_headers": { "X-Frame-Options": "DENY" } } }









