记录一次 sing-box 节点分流:让香港节点通过新加坡访问 OpenAI

记录一次使用 sing-box 将 OpenAI 流量从香港节点分流到新加坡 VPS 的过程

由于我使用的香港节点无法正常访问 OpenAI,这让我有些苦恼。

我手上刚好还有一台新加坡 VPS,于是想实现这样的效果:

1
2
3
4
5
6
7
客户端
香港 VLESS+Reality 节点
├── OpenAI 相关流量 → 新加坡 VPS → OpenAI
└── 其他流量       → 香港 VPS 直接访问

这正是 sing-box 的路由分流功能可以解决的问题。

准备

至少需要两台 VPS:

  • 一台香港 VPS,作为客户端连接的入口节点;
  • 一台新加坡 VPS,作为 OpenAI 流量的中转节点。

本次两台 VPS 都使用 VLESS+Reality,新加坡 VPS 必须存在一个 VLESS 入站。我的新加坡节点配置位于:

/etc/sing-box/conf/VLESS-REALITY-28362.json

需要记录以下信息:

  • 新加坡 VPS 公网 IP;
  • VLESS 端口;
  • UUID;
  • Reality 公钥;
  • Reality 的 server_name;
  • Reality 的 short_id;
  • flow,例如 xtls-rprx-vision。

注意,香港连接新加坡时只需要 Reality 公钥,不需要使用新加坡的私钥。

了解 sing-box 的配置结构

使用 233boy 脚本安装 sing-box 后,配置通常位于:/etc/sing-box/

我的目录结构大致如下:

1
2
3
4
  /etc/sing-box/
  ├── config.json
  └── conf/
      └── VLESS-REALITY-22260.json

实际启动命令是:

1
2
3
  /etc/sing-box/bin/sing-box run \
    -c /etc/sing-box/config.json \
    -C /etc/sing-box/conf

其中:

  • config.json 是主配置;
  • conf/ 中保存节点配置;
  • sing-box 启动时会将两部分配置合并。

这里有一个需要注意的地方:233boy 的管理脚本会把 conf/ 目录下的每个 .json 文件都当成一个节点配置解析。

因此,不要直接在 conf/ 目录创建类似下面的自定义文件:

OPENAI-THROUGH-SG.json

否则运行管理脚本时可能会出现:

错误! 无法识别协议: OPENAI-THROUGH-SG.json

正确的做法是把自定义出站和路由写入主配置:

/etc/sing-box/config.json

香港原来的 VLESS 入站配置保持不变。

配置香港到新加坡的出站

先备份主配置:

1
2
  cp -p /etc/sing-box/config.json \
    /etc/sing-box/config.json.bak-$(date +%F)

然后编辑:

1
  vim /etc/sing-box/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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  {
    "log": {
      "output": "/var/log/sing-box/access.log",
      "level": "info",
      "timestamp": true
    },
    "dns": {},
    "outbounds": [
      {
        "tag": "direct",
        "type": "direct"
      },
      {
        "type": "vless",
        "tag": "to-singapore-openai",
        "server": "SG_VPS_IP",
        "server_port": 28362,
        "uuid": "SG_UUID",
        "flow": "xtls-rprx-vision",
        "tls": {
          "enabled": true,
          "server_name": "www.apple.com",
          "utls": {
            "enabled": true,
            "fingerprint": "chrome"
          },
          "reality": {
            "enabled": true,
            "public_key": "SG_REALITY_PUBLIC_KEY",
            "short_id": ""
          }
        }
      }
    ],
    "route": {
      "rules": [
        {
          "action": "sniff"
        },
        {
          "domain_suffix": [
            "openai.com",
            "chatgpt.com",
            "oaistatic.com",
            "oaiusercontent.com",
            "openaiusercontent.com",
            "sora.com"
          ],
          "outbound": "to-singapore-openai"
        }
      ],
      "final": "direct",
      "auto_detect_interface": true
    }
  }

需要替换的内容:

  SG_VPS_IP              新加坡 VPS 公网 IP
  SG_UUID                新加坡 VLESS 入站的 UUID
  SG_REALITY_PUBLIC_KEY  新加坡 Reality 公钥

domain_suffix 会匹配域名及其子域名,例如:

  api.openai.com
  platform.openai.com
  cdn.openai.com
  chatgpt.com

都会进入 to-singapore-openai 出站。

这里的:

"final": "direct"

表示没有命中 OpenAI 规则的流量,继续从香港 VPS 直接访问。

需要特别注意 short_id 的格式。新加坡 Reality 入站里可能是:

1
2
3
  "short_id": [
    ""
  ]

但是在香港的 VLESS 出站中,sing-box 1.13.x 要求使用字符串:

1
  "short_id": ""

如果新加坡使用的是非空 short ID,也应该写成:

1
  "short_id": "0123456789abcdef"

而不是数组。

检查配置

由于实际启动时同时加载了主配置和 conf 目录,所以应该检查完整配置:

1
2
3
  /etc/sing-box/bin/sing-box check \
    -c /etc/sing-box/config.json \
    -C /etc/sing-box/conf

没有错误输出,说明配置格式正确。

只检查某一个节点文件是不完整的,例如:

1
2
  /etc/sing-box/bin/sing-box check \
    -c /etc/sing-box/conf/VLESS-REALITY-22260.json

这种方式无法检查主配置中的路由和新加坡出站。

重启 sing-box

1
2
  systemctl restart sing-box
  systemctl status sing-box

如果没有修改配置,不需要重启。

查看分流日志

在香港 VPS 上执行:

1
  tail -f /var/log/sing-box/access.log

访问 OpenAI 时,应该看到类似日志:

  inbound/vless[VLESS-REALITY-22260.json]:
  inbound connection to api.openai.com:443

  outbound/vless[to-singapore-openai]:
  outbound connection to api.openai.com:443

在新加坡 VPS 上,应该看到:

  inbound/vless[VLESS-REALITY-28362.json]:
  inbound connection to api.openai.com:443

  outbound/direct[0]:
  outbound connection to api.openai.com:443

这说明流量经过了:

香港 VLESS → 新加坡 VLESS → 新加坡 direct → OpenAI

updatedupdated2026-08-262026-08-26