Files
admin 0a9047840d feat(llm): 上游账号冷却改指数退避 + 真实请求探测闭环 (1.0.44)
天锚定重置边界计算(resets)删掉:锚点算错过两次(1.0.14 / 1.0.24),
线上表现为「厂商 15:00 已重置、网关锁到 18:00」。resets 只存不路由。

新口径:uac: = <backoff>:<next_probe>:<hard_cap>
- 到期放一个「无绑定/绑定失效」的真实请求过去探测,2xx → clear_cool 回队;
- 仍耗尽 → 退避翻倍(首轮 ipass_probe_min,默认 60s,30min 硬顶无条件回队自愈);
- 绑定流量永不被劫持当探测(保 prompt 缓存亲和)。
- pick_account 必须纯决策:四个 $llm_* 变量各求值一次,副作用只在 log 阶段记一次。

fix: 探测窗口判定改严格小于 —— 秒级时钟下 >= 会让「标记于 45.95s、next_probe=46」
的账号在 46.05s 即被当探测目标,1s 退避实际只等 0.05s,冷却形同虚设(回归暴露)。

回归 370 条全绿(网关 108 + LLM 262,新增「探测恢复闭环」剧本)。
2026-09-17 07:19:35 +08:00

220 lines
8.9 KiB
Plaintext
Raw Permalink 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;
# ★ 统计落盘间隔压到 1 秒:让「周期性落盘」在本套件里能被确定性验证。
# 生产不写这条指令(用默认 60)——**刻意不在 conf/nginx.conf 里写死**,
# 否则回滚到不含该指令的旧镜像时 nginx 会因 unknown directive 起不来。
# 见 plugin-llm/src/stats_persist.rs 模块注释。
ipass_stats_persist 1;
# 探测闭环首轮退避压到 1 秒(生产默认 60s)——同理生产 conf/nginx.conf
# **刻意不写**这条指令,保证回滚到旧镜像不受 unknown directive 影响。
ipass_probe_min 1;
# 可选 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;
}
# ---- API 代理透传出口(与生产 conf/nginx.conf 的 /apixp/ 逐条对齐)----
# 靶机地址由夹具 api-proxies.json 的 base 指向 127.0.0.1:18999,
# 转发链路与生产完全一致(只是这里不强制 proxy_method / Content-Type)。
location /apixp/ {
ipass_apix on;
proxy_pass $apixp_target;
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 $apixp_host;
proxy_set_header X-API-Key $apixp_key;
proxy_set_header Content-Type $http_content_type;
proxy_set_header Accept $http_accept;
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 代理透传探测:/api/admin/apix/test 对 passthrough 条目内部重定向到这里
location /_apixp_probe/ {
internal;
proxy_pass $apixp_probe_target;
proxy_method GET;
proxy_http_version 1.1;
proxy_pass_request_headers off;
proxy_set_header Host $apixp_host;
proxy_set_header X-API-Key $apixp_key;
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;
}
}
}