From 6a5387b8ceb83bfcb846dbd84a63752df7e4a8c7 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 12 Sep 2026 10:43:11 +0800 Subject: [PATCH] =?UTF-8?q?fix(llm):=20base=5Furl=20=E7=AB=AF=E5=8F=A3?= =?UTF-8?q?=E8=B7=9F=E9=9A=8F=E8=B0=83=E7=94=A8=E6=96=B9=20Host=EF=BC=88?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20.11=20=E4=B8=8A=E6=89=93=E4=B8=8D=E9=80=9A?= =?UTF-8?q?=E7=9A=84=E5=9B=9E=E6=98=BE=E5=9C=B0=E5=9D=80=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:账号态回的 base_url 不带端口(https://lm.mayishangshu.cn/deepseek/v1), 而本网关在宿主 88 上。客户端照它拼请求会落到 443 —— 实测那是**出口 SSL 设备**, 返回 HTTP 200 + 一个 HTML 登录页:状态码正常、内容完全不对,最难察觉的一种失败。 也不能写死 :88:挂在 443 反代后面时又必须不带端口。 改法:新增 http::host_port()(从 Host 头取 ":88"/""),base_url / view / view_all 接收端口参数,调用方从当前请求取。88 部署给 :88,443 反代给无端口,两种都对。 本轮 .11 实机交付验证: - 镜像 ipass-1.0.0(digest 046a9dd7…)更新完成:端口 88:18098、网络 ipass_default、 数据目录 /home/train11/ipass/data 全部复刻,3 账号 / 1 域名 / 1 上游无损;worker 可写。 - 给 deepseek 登记 deepseek-chat / deepseek-reasoner(保存时不提交 auth 字段 → 密钥未被动)。 - 两把 Key 实调真实 DeepSeek 均 200,含此前 404 的 /deepseek/v1/chat/completions(去重修复生效)。 - 用量统计按账号/按模型正确累加;账号态自查可用。 文档:§15 对外端口由 :18098 更正为宿主 :88;§16 新增条目;CLAUDE.md 补 base_url 端口约定。 回归:网关 78 + LLM 77 = 155 条全绿(新增 2 条端口自适应断言)。 --- CLAUDE.md | 12 +++++++++--- readme.md | 33 ++++++++++++++++++++++++++------- rust/src/api.rs | 15 ++++++++++----- rust/src/http.rs | 18 ++++++++++++++++++ rust/src/upstreams.rs | 20 ++++++++++++-------- tests/README.md | 2 +- tests/gateway/e2e.sh | 6 ++++-- tests/llm/e2e.sh | 18 ++++++++++++++---- 8 files changed, 94 insertions(+), 30 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 29222f1..4e95f2d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -204,6 +204,12 @@ token 计费与零依赖红线冲突,别轻易许诺。 → 上游 **404 且响应体为空**,而网关侧日志一切正常,极难排查。`upstreams::base_url` 对外回显的 `base_url` 必须同样**保留上游路径**,否则发给客户端的地址本身就错。 +**`base_url` 的端口必须自适应(2026-09-12 修复)**:它跟随调用方 `Host` 头里的端口 +(`http::host_port`)。三个域名共用宿主 **88**,而 88 ≠ 443 —— 地址里不带端口会落到 443 上的 +**出口 SSL 设备**,实测返回 `HTTP 200` + 一个 HTML 登录页(最难察觉的一种失败:状态码正常、 +内容完全不对,客户端表现为「鉴权失败」或「响应不是 JSON」)。反过来写死 `:88` 又会在 +443 反代部署下把客户端指向另一个端口。**别再改成任何一种写死形式。** + **账号展示名**:`users.json` 的 `display_name` 是**纯展示字段**(`users::view` 额外回一个 `name` = 展示名优先、空则回落账号名)。**不参与鉴权、不进 `ukey:` payload**,热路径看不到它,别当身份用。 @@ -229,7 +235,7 @@ location 与 80 端口 LLM server 是两份拷贝,改一处记得同步另一 | query 参数丢失 | 变量形式的 `proxy_pass` 不会自动追加 args,靠 `llm::upstream_uri` 拼好 | **本机验证夹具**:`tests/llm/`,由 `bash tests/run.sh llm` 一并起假上游(18999)、LLM 网关(18090) -与**管理端口 18091**(跑 `/api/v1/*` 账号态断言)后跑断言。当前规模:网关 **78 条** + LLM **75 条** +与**管理端口 18091**(跑 `/api/v1/*` 账号态断言)后跑断言。当前规模:网关 **78 条** + LLM **77 条** (`bash tests/run.sh all`,期望失败 0)。 `tests/llm/upstream.py` 会把收到的 `path` / `auth` / `host` / `body` 记到 `.run/llm/upstream.log` @@ -431,7 +437,7 @@ curl -s -H "$T" -H 'Content-Type: application/json' -d '{}' $A/rollback 夹具**在仓库里**(`tests/`),不再放 `/tmp` —— 那儿的脚本一重启就没了,也没法 review。 ```bash -bash tests/run.sh # 全部:域名网关 78 条 + LLM 代理 75 条 +bash tests/run.sh # 全部:域名网关 78 条 + LLM 代理 77 条 bash tests/run.sh gateway # 只跑域名网关 bash tests/run.sh llm # 只跑 LLM 代理(自动起假上游) ``` @@ -444,7 +450,7 @@ bash tests/run.sh llm # 只跑 LLM 代理(自动起假上游) | --- | --- | | `tests/run.sh` | 统一入口:编译 + 渲染配置 + 起停 + 跑断言 | | `tests/gateway/e2e.sh` | **78 条断言**:鉴权会话 / 账号(含展示名)/ 申请审核 / 免审 / 越权隔离 / Key 重置 / 上游多模型与用量 / 清理 | -| `tests/llm/e2e.sh` | **75 条断言**:鉴权 / 白名单 / 停用摘除 / **Key 替换** / 路径拼接(含版本段去重)/ SSE / 限流 / HTTPS / **模型发现** / **账号态用量** / **管理员用量与删账号清用量** | +| `tests/llm/e2e.sh` | **77 条断言**:鉴权 / 白名单 / 停用摘除 / **Key 替换** / 路径拼接(含版本段去重)/ SSE / 限流 / HTTPS / **模型发现** / **账号态用量** / **管理员用量与删账号清用量** /**base_url 端口自适应** | | `tests/.run/` | 运行目录(渲染出的配置、`data/`、`logs/`、cookie)——**已 gitignore,随时可删** | **两条不许破的线**: diff --git a/readme.md b/readme.md index 5b1b949..ac3a904 100755 --- a/readme.md +++ b/readme.md @@ -188,7 +188,7 @@ subdomain/ ├── tests/ # ★ 本机回归夹具(bash tests/run.sh [gateway|llm|all]) │ ├── run.sh # 统一入口:编译 + 渲染配置 + 起停 nginx + 跑断言 │ ├── gateway/ # 域名网关:nginx.conf.in + e2e.sh(78 条断言)+ 转发靶机 -│ └── llm/ # LLM 代理:nginx.conf.in + e2e.sh(75 条)+ 假上游 + 合成夹具 +│ └── llm/ # LLM 代理:nginx.conf.in + e2e.sh(77 条)+ 假上游 + 合成夹具 │ # (.run/ 是运行目录,已 gitignore) ├── data/ # ★ 状态目录(需可写;运行期真源在共享内存) │ ├── users.json # 账号表(含口令哈希与 API Key) @@ -482,7 +482,7 @@ cp -a data /path/to/backup/ 夹具在仓库里 `tests/`,**不进 Docker**,直接在本机跑(秒级): ```bash -bash tests/run.sh # 全部:域名网关 78 条 + LLM 代理 75 条 +bash tests/run.sh # 全部:域名网关 78 条 + LLM 代理 77 条 bash tests/run.sh gateway # 只跑域名网关 bash tests/run.sh llm # 只跑 LLM 代理 ``` @@ -643,17 +643,20 @@ curl -s -H "$T" -H 'Content-Type: application/json' -d '{}' $A/rollback # 出 ## 15. LLM API 代理 -把 `lm.mayishangshu.cn:18098` 作为公司所有大模型的统一出口。客户端只持有一把**本网关签发的 Key** +把 `lm.mayishangshu.cn:88` 作为公司所有大模型的统一出口。客户端只持有一把**本网关签发的 Key** (就是 `users.json` 里属于自己账号的那把 `sk_...`),各家厂商的真实密钥全部由网关保管—— 客户端看不到,也不需要逐家去申请。 +> 端口以部署为准:三个域名共用**宿主 88**(容器内 nginx 监听 18098,`-p 88:18098`), +> 所以对外一律写 `:88`。88 不是 443,**漏掉端口会打到别处**——详见「上游配置」的 `base_url`。 + ### 怎么用 `` 是上游名,对应 `data/llm-upstreams.json` 的 key: ```bash # 任何 OpenAI 兼容客户端:只改 base_url 与 api_key -curl https://lm.mayishangshu.cn:18098/openai/v1/chat/completions \ +curl https://lm.mayishangshu.cn:88/openai/v1/chat/completions \ -H "Authorization: Bearer sk_<你的 API Key>" \ -H 'Content-Type: application/json' \ -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"hi"}]}' @@ -667,7 +670,7 @@ curl https://lm.mayishangshu.cn:18098/openai/v1/chat/completions \ ```bash # 发现可用模型(用同一把 Key,网关本地应答) -curl -s https://lm.mayishangshu.cn:18098/v1/models \ +curl -s https://lm.mayishangshu.cn:88/v1/models \ -H "Authorization: Bearer sk_<你的 API Key>" ``` @@ -678,7 +681,7 @@ curl -s https://lm.mayishangshu.cn:18098/v1/models \ | 入口 | 响应形状 | 给谁用 | | --- | --- | --- | -| `GET https://lm.mayishangshu.cn:18098/v1/models` | OpenAI 规范 `{object:"list",data:[{id,object,owned_by}]}` | OpenAI SDK / 各类现成客户端 | +| `GET https://lm.mayishangshu.cn:88/v1/models` | OpenAI 规范 `{object:"list",data:[{id,object,owned_by}]}` | OpenAI SDK / 各类现成客户端 | | `GET <管理端口>/api/v1/models` | 网关统一体 `{code,msg,data:{items,upstreams}}` | 自研客户端、脚本(带 `base_url`,可直接拼请求) | - 两者都**按你的白名单过滤**:只列出你有权限使用的上游所登记的模型 @@ -779,13 +782,29 @@ curl -s -H "X-API-Key: $KEY" "$API/api/v1/usage" 支持一次粘贴多行(逗号 / 顿号 / 分号 / 换行都当分隔符)。 `GET /api/v1/upstreams`(账号态)会给每个可用上游回一个 `base_url`,形如 -`https://lm.mayishangshu.cn/<上游路径>`——客户端拿它当 `base_url` 直接拼即可, +`https://lm.mayishangshu.cn:88/<上游路径>`——客户端拿它当 `base_url` 直接拼即可, 不必自己关心上游 `base` 里有没有 `/v1`。 +**端口跟随调用方**:`base_url` 里的端口取自你请求管理接口时用的 `Host` 头。 +本网关用宿主 **88** 端口承载三个域名,而 88 不是 443,所以客户端拿到的地址里 +**必须带 `:88`** 才能连上;如果哪天前面挂了 443 反代,用 `Host: mdm.mayishangshu.cn` +(不带端口)访问时回显的 `base_url` 也会自动不带端口。写死任何一种都会在另一种部署下 +给出「看着对、打不通」的地址。 + --- ## 16. 变更记录 +- **2026-09-12 .11 实机交付验证 + `base_url` 端口自适应** + - 在 .11 完成镜像更新(`ipass-1.0.0`,digest `046a9dd7…`):端口 `88:18098`、 + 网络 `ipass_default`、数据目录 `/home/train11/ipass/data` 全部复刻,3 账号 / 1 域名 / 1 上游无损。 + - 给 `deepseek` 上游登记 `deepseek-chat` / `deepseek-reasoner`,模型发现随之可用。 + - **`base_url` 改为端口自适应**(`http::host_port`)。此前回显不带端口,而本网关在宿主 **88** + 上;不带端口的地址会落到 443 上的**出口 SSL 设备**(实测 `HTTP 200` + 一个 HTML 登录页), + 客户端照它配置必然失败。挂在 443 反代后面时又必须不带端口 → 只能跟随调用方 Host。 + - §15 对外示例端口由 `:18098` 更正为宿主 `:88`。 + - 回归扩至 **155 条**(网关 78 + LLM 77),`bash tests/run.sh all` 全绿。 + - **2026-09-12 远程更新控制面 `deploy/ipass-agent.py`(跑在 .11 的 HTTP 服务)** - 把 §12.4 的更新动作暴露成 HTTP 接口,远端可自助完成「拉镜像 → 重启 → 看日志 → 回滚」, 不再需要每次人工登录服务器操作(见 §12.5)。 diff --git a/rust/src/api.rs b/rust/src/api.rs index 1607c27..5b781a8 100644 --- a/rust/src/api.rs +++ b/rust/src/api.rs @@ -504,7 +504,7 @@ fn h_my_delete(r: *mut ngx_http_request_t, actor: Actor) -> Status { fn h_admin_upstreams(r: *mut ngx_http_request_t, _actor: Actor) -> Status { http::ok( r, - json!({ "items": upstreams::view_all(&upstreams::all()) }), + json!({ "items": upstreams::view_all(&upstreams::all(), &http::host_port(r)) }), "ok", ) } @@ -540,7 +540,8 @@ fn h_admin_upstream_save(r: *mut ngx_http_request_t, _actor: Actor) -> Status { "[api] 保存 LLM 上游 {slot} enabled={enabled} models={}", up.models.len() )); - http::ok(r, upstreams::view(&slot, &up), "已保存") + // base_url 带「调用方正在用的端口」:见 upstreams::base_url 的说明 + http::ok(r, upstreams::view(&slot, &up, &http::host_port(r)), "已保存") } Err(e) => http::fail(r, HTTPStatus::BAD_REQUEST, &e), } @@ -661,6 +662,8 @@ fn user_or_401(r: *mut ngx_http_request_t, actor: Actor) -> Result /// 刻意只回显 slot / base_url / **模型清单**——**不回显任何密钥**(连脱敏都不给, /// 上游密钥与调用方无关,没有知道的必要)。 fn h_my_upstreams(r: *mut ngx_http_request_t, actor: Actor) -> Status { + // 回显的 base_url 必须带「调用方正在用的端口」,否则客户端照着拼会打不通 + let port = http::host_port(r); let user = match user_or_401(r, actor) { Ok(u) => u, Err(e) => return e, @@ -671,7 +674,7 @@ fn h_my_upstreams(r: *mut ngx_http_request_t, actor: Actor) -> Status { .map(|(slot, up)| { json!({ "slot": slot, - "base_url": upstreams::base_url(&slot, &up), + "base_url": upstreams::base_url(&slot, &up, &port), "models": up.models, "note": up.note, }) @@ -687,6 +690,8 @@ fn h_my_upstreams(r: *mut ngx_http_request_t, actor: Actor) -> Status { /// (形状 `{object:"list",data:[{id,...}]}`),这边给管理/自研客户端用 /// (形状 `{code,msg,data}`,带 base_url 便于直接拼请求)。 fn h_my_models(r: *mut ngx_http_request_t, actor: Actor) -> Status { + // 同 h_my_upstreams:base_url 跟随调用方所用的端口 + let port = http::host_port(r); let user = match user_or_401(r, actor) { Ok(u) => u, Err(e) => return e, @@ -699,7 +704,7 @@ fn h_my_models(r: *mut ngx_http_request_t, actor: Actor) -> Status { items.push(json!({ "model": model, "slot": slot, - "base_url": upstreams::base_url(slot, up), + "base_url": upstreams::base_url(slot, up, &port), "note": up.note, })); } @@ -713,7 +718,7 @@ fn h_my_models(r: *mut ngx_http_request_t, actor: Actor) -> Status { .iter() .map(|(slot, up)| json!({ "slot": slot, - "base_url": upstreams::base_url(slot, up), + "base_url": upstreams::base_url(slot, up, &port), "models": up.models, })) .collect::>(), diff --git a/rust/src/http.rs b/rust/src/http.rs index d9ff834..14878e3 100644 --- a/rust/src/http.rs +++ b/rust/src/http.rs @@ -47,6 +47,24 @@ pub fn header(r: *mut ngx_http_request_t, name: &str) -> Option { None } +/// 请求 Host 里的端口后缀(`:88` / 空串)。 +/// +/// 对外三个域名(`mdm` 管理台 / `lm` LLM / `*.dm` 企业)**共用同一个端口**, +/// 所以「管理接口从哪个端口进来,LLM 就该从哪个端口出去」。回显用的 `base_url` +/// 用它拼:写死端口在换端口部署时失效,完全不写则在非 443 部署时指向别处。 +pub fn host_port(r: *mut ngx_http_request_t) -> String { + let host = match header(r, "Host") { + Some(h) => h, + None => return String::new(), + }; + // IPv6 字面量形如 `[::1]:88`,rsplit 后 `[::1]` / `88` 仍然正确; + // 纯 `[::1]` 会切出非全数字的尾巴,被下面判掉。 + match host.rsplit_once(':') { + Some((_, p)) if !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit()) => format!(":{p}"), + _ => String::new(), + } +} + /// 客户端地址(登录限速的分组键) pub fn remote_addr(r: *mut ngx_http_request_t) -> String { // SAFETY: r 有效;connection 在请求生命周期内必然存在 diff --git a/rust/src/upstreams.rs b/rust/src/upstreams.rs index b0016e1..ad90a65 100644 --- a/rust/src/upstreams.rs +++ b/rust/src/upstreams.rs @@ -188,20 +188,24 @@ pub fn available(allowed: &[String]) -> Vec<(String, Upstream)> { /// 这样它才真的能和厂商文档里的 base_url 逐个字符替换: /// /// ```text -/// https://api.deepseek.com/v1 → https://lm.mayishangshu.cn/deepseek/v1 -/// https://api.openai.com → https://lm.mayishangshu.cn/openai +/// https://api.deepseek.com/v1 → https://lm.mayishangshu.cn:88/deepseek/v1 +/// https://api.openai.com → https://lm.mayishangshu.cn:88/openai /// ``` /// +/// `port` 由调用方从请求 Host 头取(`http::host_port`),**不要写死**: +/// 部署在 88 时客户端必须带 `:88` 才连得到,而放在 443 反代后面时又必须不带端口。 +/// 写死任何一种,都会在另一种部署里给出一个「看着对、实际打不通」的地址。 +/// /// 反过来(不管上游 base 的路径、一律拼 `/v1`)会让「base 自带 /v1」的上游 /// 变成 `/v1/v1/chat/completions` → 上游 404,且客户端拿到的是空响应体, /// 排查起来完全看不出是网关拼错了。**实测踩过,别再改回去。** -pub fn base_url(slot: &str, up: &Upstream) -> String { +pub fn base_url(slot: &str, up: &Upstream, port: &str) -> String { let rest = up.base.split("://").nth(1).unwrap_or(""); let path = match rest.find('/') { Some(i) => &rest[i..], None => "", }; - format!("https://{LLM_PUBLIC_HOST}/{slot}{path}") + format!("https://{LLM_PUBLIC_HOST}{port}/{slot}{path}") } /// 模型发现:把可用上游挂载的模型摊平成 `(模型名, slot)` 列表。 @@ -299,11 +303,11 @@ pub fn remove(slot_raw: &str) -> Result { /// /// **绝不回显完整 auth**——管理台页面会被截图、被浏览器插件读取, /// 回显上游真 Key 等于把公司所有厂商密钥摆在一个 HTML 里。 -pub fn view(slot: &str, up: &Upstream) -> Value { +pub fn view(slot: &str, up: &Upstream, port: &str) -> Value { json!({ "slot": slot, "base": up.base, - "base_url": base_url(slot, up), + "base_url": base_url(slot, up, port), "models": up.models, "model_count": up.models.len(), "auth_set": !up.auth.is_empty(), @@ -315,6 +319,6 @@ pub fn view(slot: &str, up: &Upstream) -> Value { }) } -pub fn view_all(list: &[(String, Upstream)]) -> Vec { - list.iter().map(|(slot, up)| view(slot, up)).collect() +pub fn view_all(list: &[(String, Upstream)], port: &str) -> Vec { + list.iter().map(|(slot, up)| view(slot, up, port)).collect() } diff --git a/tests/README.md b/tests/README.md index 73f14f0..a13db98 100644 --- a/tests/README.md +++ b/tests/README.md @@ -5,7 +5,7 @@ ```bash bash tests/run.sh # 全部(域名网关 + LLM 代理) bash tests/run.sh gateway # 只跑域名网关(78 条断言) -bash tests/run.sh llm # 只跑 LLM 代理(75 条断言) +bash tests/run.sh llm # 只跑 LLM 代理(77 条断言) ``` 脚本会自动 `cargo build`,起 nginx,跑断言,收尾停进程。**期望输出「失败 0」。** diff --git a/tests/gateway/e2e.sh b/tests/gateway/e2e.sh index bf3f7d1..deede4a 100755 --- a/tests/gateway/e2e.sh +++ b/tests/gateway/e2e.sh @@ -195,7 +195,9 @@ ck "保存上游(含模型清单)→ 200" 200 "$(echo "$UP" | jqv code)" ck "★模型清单入库(2 个)" 2 "$(echo "$UP" | jqv data.model_count)" ck " 上游列表回显模型名" yes \ "$(curl -s -b "$JAR" $ADMIN/api/admin/upstreams | grep -q deepseek-reasoner && echo yes || echo no)" -ck " base_url 由域名常量拼出(不是写死占位)" "https://lm.mayishangshu.cn/deepseek/v1" "$(echo "$UP" | jqv data.base_url)" +# base_url 必须带「调用方正在用的端口」:88 部署下裸域名会指向别处(实测出口设备抢答), +# 而放在 443 反代后面时又不该带端口 —— 所以它只能跟随请求 Host,不能写死。 +ck "★base_url 带调用方端口(不是裸域名)" "https://lm.mayishangshu.cn:${ADMIN##*:}/deepseek/v1" "$(echo "$UP" | jqv data.base_url)" # 与 auth/note 同款语义:省略 models = 保持原清单 BODY=$(jbody slot deepseek base https://api.deepseek.com/v1) @@ -219,7 +221,7 @@ ck "alice 查可用模型 → 200" 200 "$(code -H "X-API-Key: $ALICE_KEY" $ADMIN ck " 含 deepseek-chat" yes \ "$(curl -s -H "X-API-Key: $ALICE_KEY" $ADMIN/api/v1/models | grep -q deepseek-chat && echo yes || echo no)" ck " 带 OpenAI 口径的 base_url" yes \ - "$(curl -s -H "X-API-Key: $ALICE_KEY" $ADMIN/api/v1/models | grep -q 'lm.mayishangshu.cn/deepseek/v1' && echo yes || echo no)" + "$(curl -s -H "X-API-Key: $ALICE_KEY" $ADMIN/api/v1/models | grep -Eq 'lm\.mayishangshu\.cn(:[0-9]+)?/deepseek/v1' && echo yes || echo no)" ck "无 Key 查用量 → 401" 401 "$(code $ADMIN/api/v1/usage)" ck "alice 查用量 → 200" 200 "$(code -H "X-API-Key: $ALICE_KEY" $ADMIN/api/v1/usage)" ck " 没调用过 → 累计 0" 0 "$(curl -s -H "X-API-Key: $ALICE_KEY" $ADMIN/api/v1/usage | jqv data.total)" diff --git a/tests/llm/e2e.sh b/tests/llm/e2e.sh index dc416e8..e78ab40 100755 --- a/tests/llm/e2e.sh +++ b/tests/llm/e2e.sh @@ -185,17 +185,27 @@ ck " 响应体来自上游(不是网关本地那份)" yes \ echo echo "【9】★ 账号态 API:可用模型 + 自己的用量" +# base_url 跟随**调用方所用的端口**(88 部署必须带 :88;443 反代后面则不带)—— +# 见 rust/src/upstreams.rs 的 base_url 注释,写死任何一种都会在另一种部署里给出错地址。 +API_PORT="${API##*:}" ck "无 Key 查模型 → 401" 401 "$(code $API/api/v1/models)" ck "无 Key 查用量 → 401" 401 "$(code $API/api/v1/usage)" ck "alice 查模型 → 200" 200 "$(code -H "X-API-Key: $ALICE_KEY" $API/api/v1/models)" -ck " 模型清单里带 base_url(lm 域名 + slot)" yes \ - "$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/models | has 'lm.mayishangshu.cn/openai')" +ck " 模型清单里带 base_url(lm 域名 + 端口 + slot)" yes \ + "$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/models | has "lm.mayishangshu.cn:$API_PORT/openai")" # ★ base_url 必须**保留上游 base 的路径**:openai 的 base 无路径 → 无;other 的 base 带 /llm → 带 /llm。 # 搞错这一条会让"base 自带 /v1"的上游变成 /v1/v1/... → 上游 404 且响应体为空,极难排查。 ck "★base_url 不凭空拼 /v1(base 无路径就不带)" yes \ - "$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/models | nhas 'lm.mayishangshu.cn/openai/')" + "$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/models | nhas "lm.mayishangshu.cn:$API_PORT/openai/")" ck "★base_url 保留上游 base 的路径 /llm" yes \ - "$(curl -s -H "X-API-Key: $BOB_KEY" $API/api/v1/models | has 'lm.mayishangshu.cn/other/llm')" + "$(curl -s -H "X-API-Key: $BOB_KEY" $API/api/v1/models | has "lm.mayishangshu.cn:$API_PORT/other/llm")" +# ★ 端口自适应:Host 不带端口(走 443 反代)时 base_url 也不该带端口。 +# 写死 88 → 443 部署下客户端连到另一个端口的服务;裸域名 → 88 部署下被出口设备抢答 +# (实测 HTTP 200 + 一个 HTML 登录页,是最难看出问题的一种失败)。 +ck "★Host 无端口 → base_url 不带端口" yes \ + "$(curl -s -H "X-API-Key: $ALICE_KEY" -H 'Host: lm.example' $API/api/v1/models | has 'lm.mayishangshu.cn/openai')" +ck "★Host 带端口 → base_url 带同一端口" yes \ + "$(curl -s -H "X-API-Key: $ALICE_KEY" -H "Host: lm.example:$API_PORT" $API/api/v1/models | has "lm.mayishangshu.cn:$API_PORT/openai")" ALICE_USAGE=$(curl -s -H "X-API-Key: $ALICE_KEY" $API/api/v1/usage) ck "alice 查用量 → 200" 200 "$(code -H "X-API-Key: $ALICE_KEY" $API/api/v1/usage)"