Files
ipass__ipass/tests/llm/nginx.conf.in
T
admin 1f548ac953 feat(api-proxy): 新增「API 代理」板块,把外部免费 API 市场挂到网关下
后端(rust/src/apix.rs,新模块)
- /apix/<slug>?<参数> → 市场 POST /api/v1/ai/request;复用 LLM 的 sk_ 鉴权 +
  配额 + 用量统计(slot 带 api/ 前缀,天然归组)
- 请求体由网关从 query 拼:asset_id 服务端决定(调用方只能选参数),堵住
  「客户端自选最贵资产」的口子;GET 经 proxy_method POST + proxy_set_body 转带体
- data/api-proxies.json(含平台真 Key,等同 users.json 敏感度):base+key+items
  shm 写穿 ax:,停用=摘键;平台 Key 不回显(只给掩码)
- 6 个 nginx 变量 + /_apix_probe/<slug>(internal)免费预估探测,复用 $apix_probe_*

前端(www/apix.html + assets/apix.js)
- 市场凭据配置 / 目录直拉(GET /api/v1/assets,跨域已开)/ 代理增删启停 /
  复制地址 / 免费预估测试(带回平台余额)

验证
- 回归 261 条全绿(网关 102 + LLM 159,新增 34 条 apix 断言)
- 涵盖:无/坏 Key→401、不存在/停用 slug→404、GET 改写 POST、换平台 Key、
  体由网关拼、下行 Key 不泄漏、写穿 shm、探测归 api/<slug>

文档
- readme §15.1 API 代理使用 + 变更日志;CLAUDE.md location 表补 ipass_apix
2026-09-13 02:46:35 +08:00

165 lines
6.4 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 "";
# 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;
}
}
}