在 OpenWrt 路由器中集成 V2Ray 的完整步骤

作为一名长期折腾软路由的技术爱好者,我在多个 OpenWrt 版本上成功部署过 V2Ray。今天就来分享一套经过实战检验的完整方案,帮你把 V2Ray 完美集成到路由器中,让所有连接设备都能自动享受代理服务。
准备工作与环境检查
在开始之前,请确保你的 OpenWrt 路由器满足以下条件:
- 存储空间至少 8MB(建议 16MB 以上)
- CPU 架构为 x86_64、arm64 或 mipsel(可通过
opkg print-architecture查看) - 已配置好软件源并能正常安装软件包
我建议先更新软件包列表:
opkg update
opkg list-upgradable | cut -f 1 -d ' ' | xargs opkg upgrade
安装 V2Ray 核心组件
这里我推荐使用 Project X 的预编译包,兼容性最好。首先添加对应的软件源:
# 对于 OpenWrt 21.02
echo "src/gz projectx https://dl.openwrt.ai/latest/packages/x86_64/projectx" >> /etc/opkg/customfeeds.conf
更新后安装核心包:
opkg update
opkg install v2ray-core luci-app-v2ray luci-i18n-v2ray-zh-cn
踩坑提示:如果遇到依赖错误,可以先安装 ca-bundle 和 libopenssl:
opkg install ca-bundle libopenssl
配置 V2Ray 服务端
创建配置文件目录并编辑主配置:
mkdir -p /etc/v2ray
vim /etc/v2ray/config.json
这里提供一个 VMess 协议的配置示例:
{
"inbounds": [{
"port": 10808,
"protocol": "vmess",
"settings": {
"clients": [{
"id": "你的UUID",
"alterId": 64
}]
},
"streamSettings": {
"network": "tcp"
}
}],
"outbounds": [{
"protocol": "freedom",
"settings": {}
}]
}
记得将 “你的UUID” 替换为实际的 UUID,可以使用在线工具生成。
配置透明代理(关键步骤)
要让所有流量自动走代理,需要设置 iptables 规则。创建启动脚本:
vim /etc/init.d/v2ray-transparent
脚本内容如下:
#!/bin/sh /etc/rc.common
START=99
start() {
iptables -t nat -N V2RAY
iptables -t nat -A V2RAY -d 0.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 10.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 127.0.0.0/8 -j RETURN
iptables -t nat -A V2RAY -d 169.254.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 172.16.0.0/12 -j RETURN
iptables -t nat -A V2RAY -d 192.168.0.0/16 -j RETURN
iptables -t nat -A V2RAY -d 224.0.0.0/4 -j RETURN
iptables -t nat -A V2RAY -d 240.0.0.0/4 -j RETURN
iptables -t nat -A V2RAY -p tcp -j REDIRECT --to-ports 10808
iptables -t nat -A PREROUTING -p tcp -j V2RAY
}
stop() {
iptables -t nat -F V2RAY
iptables -t nat -X V2RAY
}
给脚本执行权限并启用:
chmod +x /etc/init.d/v2ray-transparent
/etc/init.d/v2ray-transparent enable
/etc/init.d/v2ray-transparent start
启动服务与故障排查
启动 V2Ray 服务:
/etc/init.d/v2ray start
/etc/init.d/v2ray enable
检查服务状态:
logread | grep v2ray
netstat -ln | grep 10808
如果遇到启动失败,可以手动调试:
v2ray -config /etc/v2ray/config.json -test
Web 界面管理(可选)
如果你安装了 Luci 界面,现在可以通过浏览器访问:
http://你的路由器IP/cgi-bin/luci/admin/services/v2ray
在这里可以方便地修改配置、查看日志和启停服务。
性能优化建议
根据我的经验,在性能有限的路由器上建议:
- 启用 mux 多路复用减少连接数
- 使用更轻量的协议如 VLESS
- 合理设置并发连接数
- 定期清理日志防止存储空间耗尽
经过这些步骤,你的 OpenWrt 路由器就已经成功集成了 V2Ray。现在所有连接到这个路由器的设备都能自动享受代理服务,无需在每个设备上单独配置。如果在实施过程中遇到问题,欢迎在评论区交流讨论!


教程很实用,收藏了!