容量管理: zones.rs 直读 nginx file_cache 共享内存记账(双轴: 磁盘 + keys_zone 索引内存), ?scan=1 扫盘上限 20 万文件; POST /api/admin/cache/purge 整区清理(只删文件不删目录, 根治 [crit] mkdir failed); POST /api/admin/cache/invalidate 按制品精准失效(预览->确认两步式, glob/子串, 删除上限 5000/扫描上限 20 万); GET /api/admin/cache 新增 capacity 块; 前端容量面板 + 按制品失效工具条; 测试 test-cache-capacity.sh 48/48(容量 27 + 精准失效 21, 含 EXPECT_MIN 护栏与前端静态契约); docs 容量管理章节
178 lines
8.3 KiB
Plaintext
178 lines
8.3 KiB
Plaintext
# ============================================================================
|
||
# cache-routes.conf —— 制品缓存网关的「server 内容」(在 server { } 里 include)
|
||
#
|
||
# 为什么单独成一个文件:这份路由要**两处复用**——
|
||
# * 明文入口 conf/server.cache.conf(本机 e2e / 内网直连)
|
||
# * TLS 入口 conf/tls/*.conf(对外,Docker 镜像加速必须走 https)
|
||
# 路由、鉴权开关、探针、管理台只写一份,两个入口不会漂。
|
||
#
|
||
# 路由约定:/<前缀>/<path> => <该 route 配置的上游>/<path>
|
||
# /go/ Go module proxy → 默认 proxy.golang.org
|
||
# /cargo/ crates.io 稀疏索引 → 默认 index.crates.io
|
||
# /cargo-dl/ .crate 包本体 → 默认 static.crates.io
|
||
# /pypi/ PyPI 简单索引/JSON → 默认 pypi.org
|
||
# /pypi-files/ wheel/sdist 包文件 → 默认 files.pythonhosted.org
|
||
# /npm/ npm registry → 默认 registry.npmjs.org
|
||
# /maven/ Maven Central → 默认 repo1.maven.org/maven2
|
||
# /v2/ Docker Registry v2 → 默认 registry-1.docker.io
|
||
#
|
||
# ⚠️ 上游地址**不写在本文件里**:全部来自 plugin-cache 的上游表(管理台
|
||
# 「制品缓存」页,落盘 data/cache-upstreams.json + 写穿共享内存)。
|
||
# 本文件只出现变量名。改上游 = 改管理台,不需要动 conf、不需要 reload。
|
||
#
|
||
# ⚠️ 每个带 `ipass_cache on` 的 location 都会在 access 阶段做闸门:
|
||
# 1) 该路由没配上游 → 503(不会让请求走到空 $cache_target 变成 500)
|
||
# 2) 统一 sk_ Key 鉴权(与 LLM / API 代理同一把)→ 401
|
||
# 3) 每分钟/每日配额 → 429
|
||
# 然后数据面才交给 nginx 原生 proxy_cache。
|
||
# ============================================================================
|
||
|
||
# ---------------- 管理台(多页静态资源,挂在 /admin/ 下)----------------
|
||
# 每页 head 里有 <base href="/admin/">,所以资源与 /api/ 都是绝对路径。
|
||
#
|
||
# **不发版不重启也能修好页面**:所有 HTML / CSS / JS 都加
|
||
# `Cache-Control: no-store`(expires -1 + add_header)。否则用户浏览器会
|
||
# 一直用发版前的旧 JS —— 典型表现是「后端加了字段,页面上看不到」。
|
||
#
|
||
# ⚠️ alias 刻意写**相对**路径 `www/`:nginx 把 root/alias 的相对路径解析到
|
||
# **prefix** 而不是 conf/,所以生产(prefix=/usr/local/nginx)得到
|
||
# /usr/local/nginx/www/,本地回归(prefix=tests/.../.run-prod)得到
|
||
# 测试自己的 www/。写死绝对路径则本机必 404。
|
||
location = / {
|
||
return 302 /admin;
|
||
}
|
||
location = /admin {
|
||
default_type text/html;
|
||
alias www/cache.html;
|
||
expires -1;
|
||
add_header Cache-Control "no-store" always;
|
||
}
|
||
# `/admin/`(末尾斜杠、后面无字符)不能直接 alias 到目录:nginx 会把
|
||
# index 拼成 `cache.htmlindex.html` 而 500。内部重定向到具体文件最干净。
|
||
location = /admin/ {
|
||
rewrite ^ /admin/cache.html last;
|
||
}
|
||
location /admin/ {
|
||
alias www/;
|
||
expires -1;
|
||
add_header Cache-Control "no-store" always;
|
||
}
|
||
|
||
# ---------------- API:管理态(登录会话)+ 账号态(sk_)----------------
|
||
# 由 plugin-cache 认领 /api/admin/cache* 与 /api/v1/cache*;
|
||
# 其余 /api/* 交给同一 gateway 里的其它插件(llm/apix/domain),互不干扰。
|
||
location /api/ {
|
||
ipass_api on;
|
||
}
|
||
|
||
# ---------------- 上游连通性探针(管理台「测试」按钮)----------------
|
||
# /api/admin/cache/test 校验 route 后由模块内部重定向到这里。
|
||
# 刻意**复用** $cache_target / $cache_host / $cache_auth —— 与真实转发
|
||
# 走同一套「拼上游 URL + 换凭据」逻辑,所以「测试通过」才等价于
|
||
# 「真实请求能到上游并带上凭据」;另写一套探针代码必然漂移。
|
||
#
|
||
# 为什么这里不写 `ipass_cache on`:内部重定向会重跑 access 阶段,而管理台
|
||
# 带的是**登录 Cookie、不是 sk_**,再鉴一次会把自己 401 掉。鉴权已在
|
||
# /api/admin/cache/test 那一跳用 Admin 守卫做过,这里是 internal,外部进不来。
|
||
#
|
||
# proxy_cache off:探针是「验连通」,不该用一次探测污染 1d 的制品缓存。
|
||
location /_cache_test/ {
|
||
internal;
|
||
proxy_cache off;
|
||
proxy_pass $cache_target;
|
||
proxy_http_version 1.1;
|
||
# 探针只验「拼目标 + 换凭据 + 到达上游」,请求头与请求体都不需要转发:
|
||
# * 方法由模块钉成 GET(见 plugin-cache api.rs 的 h_admin_test);
|
||
# * body 必须 off —— 管理台是用 POST 触发的,不 off 的话那 2 字节 `{}`
|
||
# 会连 Content-Length 一起发出去,上游等一个 GET 的 body、
|
||
# 或 keepalive 上残留半截请求体,表现为「测试时好时坏」。
|
||
proxy_pass_request_headers off;
|
||
proxy_pass_request_body off;
|
||
proxy_set_header Host $cache_host;
|
||
proxy_set_header Authorization $cache_auth;
|
||
proxy_set_header User-Agent $http_user_agent;
|
||
proxy_ssl_server_name on;
|
||
proxy_connect_timeout 5s;
|
||
proxy_send_timeout 15s;
|
||
proxy_read_timeout 15s;
|
||
}
|
||
|
||
# ---------------- Go module proxy ----------------
|
||
location /go/ {
|
||
ipass_cache on;
|
||
proxy_cache go;
|
||
proxy_pass $cache_target;
|
||
include cache-common.conf;
|
||
}
|
||
|
||
# ---------------- crates.io 稀疏索引 ----------------
|
||
location /cargo/ {
|
||
ipass_cache on;
|
||
proxy_cache cargo;
|
||
proxy_pass $cache_target;
|
||
include cache-common.conf;
|
||
}
|
||
|
||
# ---------------- .crate 包本体 ----------------
|
||
# ⚠️ crates.io 的 config.json 里 `dl` 字段给的是**绝对 URL**
|
||
# (https://static.crates.io/crates/...),cargo 默认直连上游域名、绕过本网关,
|
||
# 所以这个 location 只是**落点就绪**。要真正命中需二选一:
|
||
# (a) nginx 开 http_sub_module,用 sub_filter 把索引/`config.json` 里的
|
||
# 绝对 URL 改写成 https://<本网关>/cargo-dl;或
|
||
# (b) 网络层把 static.crates.io 指到本网关(内网 DNS + 证书)。
|
||
# 两者都归到 cargo 生态计数(plugin-cache 的 eco_of_route 已做映射)。
|
||
location /cargo-dl/ {
|
||
ipass_cache on;
|
||
proxy_cache cargo;
|
||
proxy_pass $cache_target;
|
||
include cache-common.conf;
|
||
}
|
||
|
||
# ---------------- PyPI 简单索引 / JSON ----------------
|
||
location /pypi/ {
|
||
ipass_cache on;
|
||
proxy_cache pypi;
|
||
proxy_pass $cache_target;
|
||
include cache-common.conf;
|
||
}
|
||
|
||
# ---------------- wheel/sdist 包文件 ----------------
|
||
# ⚠️ 与 /cargo-dl/ 同理:PyPI simple 页里的 href 是绝对 URL
|
||
# (https://files.pythonhosted.org/...),pip 默认直连,需 sub_filter 重写
|
||
# 或 DNS 指向才能命中本 location。归到 pypi 生态计数。
|
||
location /pypi-files/ {
|
||
ipass_cache on;
|
||
proxy_cache pypi;
|
||
proxy_pass $cache_target;
|
||
include cache-common.conf;
|
||
}
|
||
|
||
# ---------------- npm registry ----------------
|
||
location /npm/ {
|
||
ipass_cache on;
|
||
proxy_cache npm;
|
||
proxy_pass $cache_target;
|
||
include cache-common.conf;
|
||
}
|
||
|
||
# ---------------- Maven Central ----------------
|
||
location /maven/ {
|
||
ipass_cache on;
|
||
proxy_cache maven;
|
||
proxy_pass $cache_target;
|
||
include cache-common.conf;
|
||
}
|
||
|
||
# ---------------- Docker Registry v2 ----------------
|
||
# registry-mirrors 要求 https(见 conf/tls/),且**只适合公开镜像**:
|
||
# 缓存键不含用户身份,私有镜像会被串档(见 cache-common.conf 的说明)。
|
||
# 客户端先向 auth.docker.io 换短期 Bearer token 再带 Authorization 来拉,
|
||
# 该 token 由 cache-common.conf 的 $cache_auth 透传给上游(但网关自己的
|
||
# sk_ 会被剔除)。上游路径的 `/v2` 前缀由模块的 upstream_path_prefix 补回。
|
||
location /v2/ {
|
||
ipass_cache on;
|
||
proxy_cache docker;
|
||
proxy_pass $cache_target;
|
||
include cache-common.conf;
|
||
}
|