- 健康标记(log 阶段):真实转发的上游状态码写入 uh:<slot>(4xx=活着,
5xx/502/504=失效,下次成功自动自愈),数据来源就是真实流量本身,
无需常驻探测进程。状态码读 $upstream_status 变量(r->upstream 在
上游 finalize 时已置空,log 阶段读不到——access log 同款手法)。
- 管理台「测试」按钮:POST /api/admin/upstreams/test?slot=X,校验后
ngx_http_internal_redirect 到管理 server 内部 location /_upstream_test,
复用 LLM 链路的取 slot / 拼路径 / 换真 Key 逻辑,上游原话透传给面板。
⚠️ 内部重定向只浅拷贝 ngx_str_t,URI 必须拷进请求池——否则函数返回
即悬垂,log 阶段 r->uri 全是 NUL,健康标记静默失效(实测踩坑)。
- 上游列表每行 slot 旁 OpenAI / Anthropic 两枚点击复制地址按钮,
模型发现(/v1/models 与 /api/v1/models、/api/v1/upstreams)附
anthropic_base_url 与健康状态;anthropic_base_url 统一收口
upstreams::anthropic_base_url(),base 已钉 /anthropic 时不重复追加。
- 统计卡片挪进独立「数据总览」tab 并设为默认页。
- 回归 +12 条(第 12 节健康标记断言),LLM 111 + 网关 78,失败 0。
- 测试卫生:run.sh 启动靶机前 pkill 残留进程(占 18999 会造成假 200)。
115 lines
4.4 KiB
Plaintext
115 lines
4.4 KiB
Plaintext
# ============================================================================
|
||
# 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;
|
||
|
||
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 "";
|
||
|
||
# SSE 三件套:少一个流式就会退化成「攒完一次性返回」
|
||
proxy_buffering off;
|
||
proxy_request_buffering off;
|
||
proxy_cache off;
|
||
chunked_transfer_encoding on;
|
||
|
||
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/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;
|
||
}
|
||
}
|
||
}
|