核心架构:四大组件的正交协作
在代理内核生态中,sing-box 以其极致轻量、性能卓越以及原生全协议栈支持著称。
然而很多新手初次上手时,往往被其严密的 JSON 层次结构劝退。理解 sing-box 的关键,在于掌握其四大核心模块的数据管道流向:
[本地应用流量]
│
▼
┌─────────────┐
│ Inbounds │ ──> 监听本地混合端口 (2080) 或 TUN 虚拟网卡
└─────────────┘
│
▼
┌─────────────┐
│ DNS │ ──> 独立解析中枢 (Fake-IP 池 / 国内外 DoH 分流)
└─────────────┘
│
▼
┌─────────────┐
│ Route │ ──> 智能路由引擎 (自上而下匹配 Rule-set 规则集)
└─────────────┘
│
▼
┌─────────────┐
│ Outbounds │ ──> 实体出口交付 (DIRECT直连 / 节点选择 / 阻断REJECT)
└─────────────┘
一、生产级标准模板(sing-box 1.10+ 规范)
以下是一份经过多平台验证、可直接作为生产环境参考的基础配置文件 config.json。支持 TUN 虚拟网卡 + 本地混合端口 + 独立 DNS 分流 + 远程二进制规则集:
{
"$schema": "https://raw.githubusercontent.com/SagerNet/sing-box/refs/heads/main/schemas/config.json",
"log": {
"level": "info",
"timestamp": true
},
"dns": {
"servers": [
{
"tag": "dns-remote",
"address": "tls://1.1.1.1",
"address_resolver": "dns-direct",
"detour": "proxy"
},
{
"tag": "dns-direct",
"address": "https://223.5.5.5/dns-query",
"detour": "direct"
},
{
"tag": "dns-fakeip",
"address": "fakeip"
}
],
"rules": [
{
"outbound": ["any"],
"server": "dns-direct"
},
{
"query_type": ["A", "AAAA"],
"server": "dns-fakeip"
}
],
"fakeip": {
"enabled": true,
"inet4_range": "198.18.0.0/15"
}
},
"inbounds": [
{
"type": "mixed",
"tag": "mixed-in",
"listen": "127.0.0.1",
"listen_port": 2080
},
{
"type": "tun",
"tag": "tun-in",
"inet4_address": "172.19.0.1/30",
"auto_route": true,
"strict_route": true,
"stack": "system",
"sniff": true
}
],
"outbounds": [
{
"type": "selector",
"tag": "proxy",
"outbounds": ["node-01", "node-02"]
},
{
"type": "direct",
"tag": "direct"
},
{
"type": "block",
"tag": "block"
},
{
"type": "dns",
"tag": "dns-out"
},
{
"type": "vless",
"tag": "node-01",
"server": "hk01.example.com",
"server_port": 443,
"uuid": "00000000-0000-0000-0000-000000000000",
"flow": "xtls-rprx-vision",
"tls": {
"enabled": true,
"server_name": "apple.com",
"reality": {
"enabled": true,
"public_key": "YOUR_PUBLIC_KEY",
"short_id": "01234567"
}
}
}
],
"route": {
"rules": [
{
"protocol": "dns",
"outbound": "dns-out"
},
{
"rule_set": "geosite-category-ads-all",
"outbound": "block"
},
{
"rule_set": ["geosite-cn", "geoip-cn"],
"outbound": "direct"
}
],
"rule_set": [
{
"tag": "geosite-category-ads-all",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-box-ruleset/refs/heads/main/geosite-category-ads-all.srs",
"download_detour": "proxy"
},
{
"tag": "geosite-cn",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-box-ruleset/refs/heads/main/geosite-cn.srs",
"download_detour": "proxy"
},
{
"tag": "geoip-cn",
"type": "remote",
"format": "binary",
"url": "https://raw.githubusercontent.com/SagerNet/sing-box-ruleset/refs/heads/main/geoip-cn.srs",
"download_detour": "proxy"
}
],
"auto_detect_interface": true,
"final": "proxy"
}
}
二、关键配置细节与避坑指南
1. 为什么要使用二进制 .srs 规则集?
在 1.10+ 版本中,官方全面推荐使用 编译二进制规则集(SRS 格式):
- 大幅降低内存:传统的几万行文本规则解析需要消耗上百兆堆内存,而二进制规则由 sing-box 采用 mmap 机制高速检索,启动速度在 1 秒以内。
- 自动缓存与更新:配置了
type: "remote"后,内核会在后台自动静默下载并缓存于本地,即便离线也能正常读取。
2. DNS 循环依赖(DNS Loop)预防
在 dns.servers 中:
- 解析海外 DoT/DoH 的
dns-remote,必须指定一个能够走直连的解析器:"address_resolver": "dns-direct"。 - 否则,当内核尝试建立到
1.1.1.1的 TLS 连接时,如果系统不知道其 IP 如何解析,就会引发死循环挂起。
3. 静态语法校验三部曲
任何编辑完成后,切忌直接作为服务启动:
# 1. 校验配置文件格式与语法
sing-box check -c config.json
# 2. 模拟以调试模式运行并观察控制台日志
sing-box run -c config.json
# 3. 验证通过后注册为系统自启服务
相关技术专题与进阶
本专题关键技术问答.
[ 01 ]为什么网上搜到的很多 sing-box 教程在 1.10+ 版本中直接报错?[ + ]
sing-box 从 1.8 到 1.10 版本期间进行过多次重要的顶层配置架构清理。例如传统的 geosite 规则列表已被官方废弃,全面迁移至编译二进制的 rule_set 模块;同时 DNS 配置中的独立独立字段也进行了结构合并。使用旧版本教程会直接导致静态校验(sing-box check)报错退出。
[ 02 ]sing-box 相比 Clash 在路由分流上有何独到优势?[ + ]
sing-box 的路由与 DNS 具有极其强悍的正交独立性。你可以直接在路由层拦截特定协议(如 QUIC 或 BitTorrent),并支持在 DNS 模块内部针对特定域名指定独立的查询出站通道(例如直连国内阿里的 DoH,海外域名走代理节点的 DoT),从底层杜绝了 DNS 泄露与劫持。
[ 03 ]写完 JSON 配置文件后,如何验证有没有语法或字段错误?[ + ]
在终端中运行官方校验命令:sing-box check -c config.json。若配置完全合规,命令将没有任何报错并正常退出;若存在逗号缺失、括号不匹配或字段拼写错误,控制台会精确提示出错的行号与字段原因。