Files
ipass__ipass/tests/llm/nginx.conf.in
T
admin bfa3cf8ff8 feat(llm): 上游多账号路由 + 账号级负载统计 (1.0.14)
一个 slot 挂多把上游账号 Key,解决厂商「单账号额度/并发受限」与
「同一用户缓存命中」两个矛盾诉求。

多账号路由 (plugin-llm)
- models: Upstream.accounts[] (auth/note/resets) + 上游级
  exhaust_status/exhaust_markers;顶层 auth 保留兼容(accounts 为空时合成单账号)
- upstreams::pick_account:粘性绑定 ukb:<key> 优先 → 无绑定时
  FNV-1a(下游 Key) % 可用数 冷分散 → 绑定账号冷却则重新分散 → 全冷却取最早恢复
  * 分散避开厂商单账号 agent 并发上限;落点固定保住 prompt/token 缓存
- cool_until_until 天锚定冷却:boundary = day0 + ceil(已过/窗口)*窗口,
  封顶次日 0 点(MiniMax 5h 窗口、24=5*4+4 → 末段 20:00-24:00 只有 4h 随天重置)
- 耗尽双检测 (log 阶段):$upstream_status 命中 exhaust_status,或
  usage_filter 流式扫到 marker → $llm_acct 定位本次账号 → 写冷却 uac:
- shm: up:/upa:/uac:/ukb:/upe: 五类前缀;$llm_acct 变量 (NOCACHEABLE)

账号级负载统计(问题发现与跟踪)
- 新增 uda:<slot>:<idx>:<day>(请求/token)与 uex:(耗尽次数) 日桶,
  复用内核 usage::bump_day —— 无数据库、无新进程(项目红线)
- 计数点只在 log 阶段一次;pick_account 会被 $llm_target/$llm_auth/$llm_acct
  三个变量各触发一次,放进那里会三倍计数
- view() 投影带每账号 load 与 account_trend + trend_labels(日期由后端算)
- 管理台「上游」页密钥列:各 Key 今日请求/token/耗尽 + 负载比例条 +
  14 天迷你趋势(自绘 SVG,零第三方依赖)+ 冷却/耗尽徽标 + 上游合计

测试与验证
- 回归 279 条全绿(网关 102 + LLM 177),新增 6 条多账号/负载断言
- 假上游新增 UPSTREAM_FAIL_AUTH 模拟账号额度耗尽;靶机验证 Key 真被替换
- 前端以真实 admin.css + upstreams.js 渲染路径做无头浏览器断言:
  比例条与负载成比例(412 vs 96 → 100%/23%)、每账号 14 条块、
  悬停峰值日期与数据逐位吻合、单账号/无账号兜底正常、无横向溢出、零 JS 错误

文档
- 新增 docs/llm/MULTI_ACCOUNT_ROUTING.md(数据模型/路由/冷却数学/shm 布局/负载统计)
- readme §11 数据模型 + §15 上游配置 + §15 用量统计 + §12.6 tag 表 + §16 变更记录
- 修正 docs/llm/*.md 中插件化前的过时路径(rust/src/* → crates/plugin-llm/src/*)
2026-09-14 11:57:08 +08:00

169 lines
6.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ============================================================================
# LLM 代理 · 本机回归配置模板(**不是**仓库里的生产配置 conf/nginx.conf)
#
# 由 tests/run.sh 渲染:@MODULE@ / @RUN@ 会被替换成绝对路径。
#
# 与生产配置的差异(均因本机 nginx 编译时关闭了 rewrite 模块):
# * 无 set / return / error_page 重定向 —— 所以生产里 error_page 那段测不到,
# 那部分是标准 nginx 用法,留给带 rewrite 模块的生产镜像
# * 端口 18090(LLM 网关) / 18999(python 假上游)
# * 状态目录 @RUN@/data —— 独立目录,**绝不碰生产 data/**
# ============================================================================
load_module @MODULE@;
worker_processes 2;
error_log @RUN@/logs/error.log info;
pid @RUN@/logs/nginx.pid;
events {
worker_connections 1024;
}
http {
default_type application/json;
# 把解析出的上游打进日志,便于直接观察注入结果
log_format llm '$remote_addr "$request" $status host=$llm_host upstream=$llm_target';
access_log @RUN@/logs/access.log llm;
ipass_state state 10m;
ipass_data_dir @RUN@/data;
# 可选 HTTPS:run.sh 会在 @RUN@/tls/ 放渲染好的 443 server + 自签证书;
# 文件不存在时此 include 空匹配,不影响启动
include @RUN@/tls/*.conf;
server {
listen 18090;
server_name _;
client_max_body_size 100m;
location / {
ipass_llm on;
# 对话审计(与生产 conf/nginx.conf 对齐):强制请求体落临时文件,
# log 阶段由模块复制进 @RUN@/audit/
client_body_in_file_only clean;
proxy_pass $llm_target;
proxy_http_version 1.1;
# ⚠️ 这一块必须与生产 conf/nginx.conf 的 LLM server **逐条对齐**,
# 否则回归证明的不是生产行为。
# 出站头策略:默认不放行(allowlist)+ 凭据头显式抹空。
# 背景见 docs/llm/UPSTREAM_PROTOCOL_AND_AUTH.md
proxy_pass_request_headers off;
proxy_set_header x-api-key "";
proxy_set_header api-key "";
proxy_set_header x-goog-api-key "";
proxy_set_header Host $llm_host;
proxy_set_header Authorization $llm_auth;
proxy_set_header Content-Type $http_content_type;
proxy_set_header Accept $http_accept;
proxy_set_header anthropic-version $http_anthropic_version;
proxy_set_header anthropic-beta $http_anthropic_beta;
proxy_set_header openai-beta $http_openai_beta;
proxy_set_header User-Agent $http_user_agent;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Connection "";
# 调试可见:暴露本次请求选中的上游账号下标(多账号路由验证用)。
# 仅测试配置暴露;生产是否暴露由部署决定(不影响安全,只泄露账号序号)。
add_header X-Upstream-Acct $llm_acct always;
# SSE 流式两件套(响应方向;请求方向不再 no_buffering,理由见
# 生产 conf/nginx.conf——与对话审计的 client_body_in_file_only 互斥)
proxy_buffering off;
proxy_cache off;
chunked_transfer_encoding on;
proxy_connect_timeout 5s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# ---- API 代理出口(与生产 conf/nginx.conf 的 /apix/ 逐条对齐)----
# 靶机地址由夹具 api-proxies.json 的 base 指向 127.0.0.1:18999,
# 所以这里不需要为测试改任何 nginx 配置——转发链路与生产完全一致。
location /apix/ {
ipass_apix on;
proxy_pass $apix_target;
proxy_method POST;
proxy_set_body $apix_body;
proxy_http_version 1.1;
proxy_pass_request_headers off;
proxy_set_header x-api-key "";
proxy_set_header api-key "";
proxy_set_header Authorization "";
proxy_set_header Host $apix_host;
proxy_set_header X-API-Key $apix_key;
proxy_set_header Content-Type application/json;
proxy_set_header Accept application/json;
proxy_set_header User-Agent $http_user_agent;
proxy_set_header Connection "";
proxy_connect_timeout 5s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
}
# ---- 管理 server(只为在同一次回归里验账号态用量/模型接口)----
# 端口 18091;与生产一样只暴露 /api/,账号态用 X-API-Key 鉴权。
# 放在这里而不是另起一次 nginx:这样两个接口读的就是**同一块共享内存**,
# 「LLM 请求真的被计入用量」才是被验证到的事实,而不是各自独立的假象。
server {
listen 18091;
server_name _;
client_max_body_size 2m;
client_body_buffer_size 1m;
location /api/ {
ipass_api on;
}
# API 代理探测:/api/admin/apix/test 内部重定向到这里
location /_apix_probe/ {
internal;
proxy_pass $apix_probe_target;
proxy_method POST;
proxy_set_body $apix_probe_body;
proxy_http_version 1.1;
proxy_pass_request_headers off;
proxy_set_header Host $apix_host;
proxy_set_header X-API-Key $apix_key;
proxy_set_header Content-Type application/json;
proxy_set_header Accept application/json;
proxy_connect_timeout 5s;
proxy_send_timeout 15s;
proxy_read_timeout 15s;
}
# 上游连通性测试:/api/admin/upstreams/test 内部重定向到这里
# (与生产 conf 同套:proxy_pass 复用 $llm_*,探测体原样转发,状态透传)
location /_upstream_test/ {
internal;
proxy_pass $llm_target;
proxy_http_version 1.1;
proxy_pass_request_headers off;
proxy_set_header Host $llm_host;
proxy_set_header Authorization $llm_auth;
proxy_set_header Content-Type $http_content_type;
proxy_set_header Accept $http_accept;
proxy_connect_timeout 5s;
proxy_send_timeout 15s;
proxy_read_timeout 15s;
}
}
}