0%

v2rayN 路由设置完全指南:规则配置、分流策略与性能优化

v2rayN 路由设置完全指南:规则配置、分流策略与性能优化

写在前面:v2rayN 是 Windows 平台上最流行的 V2Ray 客户端之一。正确的路由设置可以实现精准分流,提升访问速度,降低延迟。本文详解 v2rayN 路由配置的方方面面。


一、路由设置基础

1.1 什么是路由设置?

路由设置(Routing)决定哪些流量走代理,哪些流量直连。

核心作用

  • 分流 - 国内网站直连,国外网站代理
  • 加速 - 减少不必要的代理跳转
  • 省钱 - 节省代理流量
  • 合规 - 遵守网络使用规范

1.2 路由工作原理

1
2
3
4
5
6
7
用户请求

路由规则检查
├── 匹配国内域名 → 直连
├── 匹配国外域名 → 代理
├── 匹配广告域名 → 阻止
└── 未匹配 → 默认规则

1.3 路由规则类型

规则类型 说明 示例
域名规则 基于域名匹配 geosite:cn, domain:google.com
IP 规则 基于 IP 地址匹配 geoip:cn, ipcidr:192.168.0.0/16
端口规则 基于端口匹配 port:80,443
协议规则 基于协议匹配 protocol:http,https
网络规则 基于网络类型 network:tcp,udp

二、v2rayN 路由配置详解

2.1 路由模式

v2rayN 提供三种路由模式:

模式 说明 适用场景
全局模式 所有流量走代理 需要访问所有国外网站
直连模式 所有流量直连 不使用代理时
规则模式 根据规则分流 推荐,智能分流

切换方法

1
系统托盘图标 → 路由模式 → 选择模式

2.2 路由规则文件

v2rayN 使用 JSON 格式的路由规则文件:

文件位置

1
v2rayN\bin\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
30
31
32
33
34
35
36
37
38
{
"routing": {
"domainStrategy": "IPIfNonMatch",
"rules": [
{
"type": "field",
"outboundTag": "direct",
"domain": [
"geosite:cn",
"geosite:google-cn"
]
},
{
"type": "field",
"outboundTag": "proxy",
"domain": [
"geosite:google",
"geosite:github"
]
},
{
"type": "field",
"outboundTag": "direct",
"ip": [
"geoip:cn",
"geoip:private"
]
},
{
"type": "field",
"outboundTag": "block",
"domain": [
"geosite:category-ads-all"
]
}
]
}
}

2.3 路由规则详解

domainStrategy(域名策略)

策略 说明
AsIs 只使用域名进行路由选择
IPIfNonMatch 优先使用域名,不匹配时使用 IP
IPOnDemand 尽可能使用 IP 进行路由选择
IPIfNonMatch 推荐,平衡性能和准确性

推荐配置

1
2
3
{
"domainStrategy": "IPIfNonMatch"
}

域名规则(domain)

内置规则集(geosite):

规则集 说明
geosite:cn 中国网站(直连)
geosite:google Google 系列(代理)
geosite:github GitHub(代理)
geosite:facebook Facebook 系列(代理)
geosite:twitter Twitter(代理)
geosite:youtube YouTube(代理)
geosite:netflix Netflix(代理)
geosite:category-ads-all 广告域名(阻止)

自定义域名

1
2
3
4
5
6
7
8
9
{
"type": "field",
"outboundTag": "proxy",
"domain": [
"domain:example.com",
"full:www.example.com",
"regexp:.*\\.example\\.com"
]
}

IP 规则(ip)

内置规则集(geoip):

规则集 说明
geoip:cn 中国 IP(直连)
geoip:us 美国 IP
geoip:jp 日本 IP
geoip:private 私有 IP(直连)

自定义 IP 段

1
2
3
4
5
6
7
8
9
{
"type": "field",
"outboundTag": "direct",
"ip": [
"ipcidr:192.168.0.0/16",
"ipcidr:10.0.0.0/8",
"ipcidr:172.16.0.0/12"
]
}

三、路由规则配置实战

3.1 基础分流配置

目标:国内直连,国外代理

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
{
"routing": {
"domainStrategy": "IPIfNonMatch",
"rules": [
{
"type": "field",
"outboundTag": "direct",
"domain": [
"geosite:cn"
]
},
{
"type": "field",
"outboundTag": "direct",
"ip": [
"geoip:cn"
]
},
{
"type": "field",
"outboundTag": "proxy",
"domain": [
"geosite:geolocation-!cn"
]
}
]
}
}

3.2 广告屏蔽配置

目标:自动屏蔽广告域名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "block",
"domain": [
"geosite:category-ads-all",
"geosite:category-ads-chn"
]
}
]
}
}

3.3 流媒体分流配置

目标:Netflix、Disney+ 等走特定节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "netflix-proxy",
"domain": [
"geosite:netflix",
"geosite:disney",
"geosite:hbo"
]
},
{
"type": "field",
"outboundTag": "youtube-proxy",
"domain": [
"geosite:youtube"
]
}
]
}
}

3.4 游戏加速配置

目标:游戏流量走低延迟节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "game-proxy",
"domain": [
"domain:steamchina.com",
"domain:steamcontent.com",
"domain:steampowered.com"
]
},
{
"type": "field",
"outboundTag": "game-proxy",
"port": "27015,27036",
"protocol": "udp"
}
]
}
}

3.5 局域网直连配置

目标:局域网流量不经过代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "direct",
"ip": [
"geoip:private",
"ipcidr:192.168.0.0/16",
"ipcidr:10.0.0.0/8",
"ipcidr:172.16.0.0/12",
"ipcidr:127.0.0.0/8"
]
}
]
}
}

四、高级路由配置

4.1 多出口路由

场景:不同网站走不同代理节点

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
34
35
36
37
38
39
40
41
42
43
44
{
"outbounds": [
{
"tag": "us-proxy",
"protocol": "vmess",
"settings": { ... }
},
{
"tag": "jp-proxy",
"protocol": "vmess",
"settings": { ... }
},
{
"tag": "direct",
"protocol": "freedom"
}
],
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "jp-proxy",
"domain": [
"geosite:category-jp"
]
},
{
"type": "field",
"outboundTag": "us-proxy",
"domain": [
"geosite:netflix",
"geosite:hulu"
]
},
{
"type": "field",
"outboundTag": "direct",
"domain": [
"geosite:cn"
]
}
]
}
}

4.2 基于端口的路由

场景:特定端口走代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "proxy",
"port": "80,443,8080"
},
{
"type": "field",
"outboundTag": "direct",
"port": "22,3389"
}
]
}
}

4.3 基于协议的路由

场景:UDP 流量特殊处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "udp-proxy",
"protocol": "udp",
"port": "53"
},
{
"type": "field",
"outboundTag": "proxy",
"network": "tcp"
}
]
}
}

4.4 正则表达式路由

场景:复杂域名匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "proxy",
"domain": [
"regexp:.*\\.google\\..*",
"regexp:.*\\.youtube\\..*",
"regexp:.*\\.facebook\\..*"
]
}
]
}
}

4.5 路由规则优先级

规则匹配顺序:从上到下,先匹配先执行

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
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "block",
"domain": ["geosite:category-ads-all"]
},
{
"type": "field",
"outboundTag": "direct",
"domain": ["geosite:cn"]
},
{
"type": "field",
"outboundTag": "proxy",
"domain": ["geosite:geolocation-!cn"]
},
{
"type": "field",
"outboundTag": "direct",
"ip": ["geoip:cn"]
},
{
"type": "field",
"outboundTag": "proxy",
"ip": ["geoip:!cn"]
}
]
}
}

优先级说明

  1. 广告屏蔽(最高优先级)
  2. 国内域名直连
  3. 国外域名代理
  4. 国内 IP 直连
  5. 国外 IP 代理(兜底)

五、v2rayN 界面配置

5.1 路由设置入口

方法 1:系统托盘

1
系统托盘图标 → 路由模式 → 规则模式

方法 2:主界面

1
主界面 → 服务器 → 路由设置

方法 3:快捷键

1
Ctrl + R → 切换路由模式

5.2 预定义规则

v2rayN 提供预定义规则:

规则 说明
绕过局域网和大陆 推荐,局域网 + 国内直连
绕过局域网 仅局域网直连
全局 所有流量代理
自定义 手动编辑规则

5.3 自定义规则编辑

步骤

  1. 打开 v2rayN 主界面
  2. 点击”服务器” → “路由设置”
  3. 选择”自定义”
  4. 编辑路由规则 JSON
  5. 保存并应用

编辑界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
┌─────────────────────────────────────┐
│ v2rayN - 路由设置 │
├─────────────────────────────────────┤
│ 路由模式:○ 全局 ○ 直连 ● 规则 │
│ │
│ 预定义规则: │
│ ○ 绕过局域网和大陆 │
│ ○ 绕过局域网 │
│ ○ 全局 │
│ ● 自定义 │
│ │
│ 路由规则: │
│ ┌─────────────────────────────────┐ │
│ │ { │ │
│ │ "routing": { │ │
│ │ "rules": [...] │ │
│ │ } │ │
│ │ } │ │
│ └─────────────────────────────────┘ │
│ │
│ [保存] [取消] [重置] │
└─────────────────────────────────────┘

六、性能优化

6.1 DNS 优化

问题:DNS 泄漏、解析慢

解决方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"dns": {
"hosts": {
"domain:google.com": "8.8.8.8",
"domain:youtube.com": "8.8.8.8"
},
"servers": [
"8.8.8.8",
"1.1.1.1",
"https://dns.google/dns-query",
{
"address": "223.5.5.5",
"domains": ["geosite:cn"]
}
]
}
}

DNS 策略

  • 国内域名 → 阿里 DNS(223.5.5.5)
  • 国外域名 → Google DNS(8.8.8.8)
  • 加密 DNS → DoH(DNS over HTTPS)

6.2 路由规则优化

优化原则

  1. 精确规则在前 - 具体域名优先匹配
  2. 宽泛规则在后 - geosite 规则靠后
  3. 兜底规则最后 - 默认规则放最后

优化示例

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
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "block",
"domain": ["geosite:category-ads-all"]
},
{
"type": "field",
"outboundTag": "direct",
"domain": [
"full:www.example.com",
"domain:example.com"
]
},
{
"type": "field",
"outboundTag": "direct",
"domain": ["geosite:cn"]
},
{
"type": "field",
"outboundTag": "proxy",
"domain": ["geosite:geolocation-!cn"]
}
]
}
}

6.3 减少 DNS 查询

方法:使用 IP 规则代替域名规则

1
2
3
4
5
6
7
8
9
10
11
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "direct",
"ip": ["geoip:cn"]
}
]
}
}

优点

  • ✅ 减少 DNS 查询次数
  • ✅ 提升解析速度
  • ✅ 降低延迟

缺点

  • ❌ IP 段可能变化
  • ❌ 规则文件较大

6.4 启用路由缓存

配置

1
2
3
4
5
6
{
"routing": {
"domainStrategy": "IPIfNonMatch",
"rules": [...]
}
}

效果

  • 首次查询后缓存结果
  • 后续请求直接使用缓存
  • 提升访问速度 30-50%

七、故障排查

7.1 常见问题

问题 1:国内网站访问慢

症状:访问淘宝、京东等国内网站速度慢

原因:路由规则配置错误,国内流量走了代理

解决

1
2
3
4
5
6
7
8
9
10
11
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "direct",
"domain": ["geosite:cn"]
}
]
}
}

验证

1
2
3
4
# 检查路由
curl -I https://www.taobao.com

# 应该直连,不经过代理

问题 2:国外网站无法访问

症状:Google、YouTube 等无法访问

原因:路由规则未匹配到国外域名

解决

1
2
3
4
5
6
7
8
9
10
11
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "proxy",
"domain": ["geosite:geolocation-!cn"]
}
]
}
}

验证

1
2
3
4
# 检查路由
curl -I https://www.google.com

# 应该走代理

问题 3:DNS 泄漏

症状:DNS 查询泄漏到代理服务器

原因:DNS 配置错误

解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"dns": {
"servers": [
"8.8.8.8",
{
"address": "223.5.5.5",
"domains": ["geosite:cn"]
}
]
},
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "direct",
"port": "53",
"ip": ["geoip:cn"]
}
]
}
}

验证

1
2
3
4
# 检查 DNS 泄漏
https://dnsleaktest.com

# 应该只显示本地 DNS 服务器

问题 4:局域网设备无法访问

症状:无法访问路由器、NAS 等局域网设备

原因:未配置局域网直连规则

解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"routing": {
"rules": [
{
"type": "field",
"outboundTag": "direct",
"ip": [
"geoip:private",
"ipcidr:192.168.0.0/16",
"ipcidr:10.0.0.0/8"
]
}
]
}
}

验证

1
2
3
4
# 访问路由器
ping 192.168.1.1

# 应该直连

7.2 日志分析

启用日志

1
2
3
4
5
6
7
{
"log": {
"loglevel": "debug",
"access": "./access.log",
"error": "./error.log"
}
}

分析路由

1
2
3
4
5
# 查看访问日志
cat access.log | grep "routing"

# 查看错误日志
cat error.log | grep "routing"

7.3 路由测试工具

在线测试

本地测试

1
2
3
4
5
6
7
# 检查路由
curl -v https://www.google.com
curl -v https://www.taobao.com

# 检查 DNS
nslookup www.google.com
nslookup www.taobao.com

八、最佳实践

8.1 路由规则模板

推荐配置(适合大多数用户):

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
34
35
36
37
38
39
40
{
"routing": {
"domainStrategy": "IPIfNonMatch",
"rules": [
{
"type": "field",
"outboundTag": "block",
"domain": [
"geosite:category-ads-all"
]
},
{
"type": "field",
"outboundTag": "direct",
"domain": [
"geosite:cn",
"geosite:google-cn"
]
},
{
"type": "field",
"outboundTag": "direct",
"ip": [
"geoip:cn",
"geoip:private"
]
},
{
"type": "field",
"outboundTag": "proxy",
"domain": [
"geosite:geolocation-!cn",
"geosite:google",
"geosite:github",
"geosite:youtube"
]
}
]
}
}

8.2 定期更新规则

更新频率

  • geosite 规则 - 每月更新
  • geoip 规则 - 每月更新
  • 自定义规则 - 根据需要更新

更新方法

1
2
3
4
5
6
7
# 下载最新规则
wget https://github.com/v2fly/domain-list-community/raw/release/dlc.dat
wget https://github.com/v2fly/geoip/raw/release/geoip.dat

# 替换旧规则
cp dlc.dat v2rayN/bin/
cp geoip.dat v2rayN/bin/

8.3 备份配置

备份内容

  • 路由规则配置
  • DNS 配置
  • 服务器配置

备份方法

1
2
3
4
5
# 备份配置
cp -r v2rayN/bin/config.json ~/backup/v2rayn-config-$(date +%Y%m%d).json

# 恢复配置
cp ~/backup/v2rayn-config-20260316.json v2rayN/bin/config.json

8.4 安全建议

建议

  1. 启用加密 DNS - DoH/DoT
  2. 阻止广告域名 - 减少恶意广告
  3. 定期更新规则 - 保持规则最新
  4. 监控流量 - 发现异常流量
  5. 备份配置 - 防止配置丢失

九、总结

9.1 核心要点

  1. 路由模式 - 推荐使用规则模式
  2. 域名策略 - 推荐 IPIfNonMatch
  3. 分流规则 - 国内直连,国外代理
  4. 广告屏蔽 - 自动屏蔽广告域名
  5. DNS 优化 - 使用加密 DNS
  6. 定期更新 - 保持规则最新

9.2 配置检查清单

  • 路由模式设置为”规则模式”
  • 配置国内域名直连规则
  • 配置国外域名代理规则
  • 配置广告屏蔽规则
  • 配置局域网直连规则
  • 配置 DNS 服务器
  • 测试国内网站访问
  • 测试国外网站访问
  • 检查 DNS 泄漏
  • 备份配置文件

9.3 学习资源

资源 链接
v2rayN 官网 https://github.com/2dust/v2rayN
V2Ray 文档 https://www.v2ray.com
路由规则 https://www.v2ray.com/chapter_02/04_routing.html
geosite 规则 https://github.com/v2fly/domain-list-community
geoip 规则 https://github.com/v2fly/geoip

参考资源

官方资源

  1. v2rayN GitHub:https://github.com/2dust/v2rayN
  2. V2Ray 官方文档:https://www.v2ray.com
  3. 路由配置文档:https://www.v2ray.com/chapter_02/04_routing.html

规则资源

  1. geosite 规则:https://github.com/v2fly/domain-list-community
  2. geoip 规则:https://github.com/v2fly/geoip
  3. 路由规则模板:https://www.v2ray.com/chapter_02/04_routing.html

学习资源

  1. V2Ray 高级配置:https://www.v2ray.com/chapter_02/
  2. v2rayN 使用教程:https://www.v2rayn.com
  3. 路由优化指南:https://www.v2ray.com/chapter_02/04_routing.html

本文是网络配置系列的第 102 篇,详细介绍了 v2rayN 路由设置的完整配置方法。希望帮助你实现精准的网络流量管理!