一、引言:被忽视的“内部”才是外置缓存的安全基石
在《nginx外置缓存》系列的前几篇中,我们深入探讨了redis对接、error_page降级和多层容错策略。但有一个更基础、却极少被单独讨论的机制,默默支撑着整个外置缓存架构的安全性与正确性——匿名location(anonymous location)。
什么是匿名location?它是以 @ 符号命名的命名location,如 @cache_backend、@redis_fallback。与常规location不同,它永远无法被外部http请求直接访问,只能通过nginx内部指令(try_files、error_page、rewrite ^... last)或lua子请求触发。
在外置缓存场景中,这个看似简单的语法特性承载着三重关键职责:
- 安全隔离:缓存回源、降级处理、数据预热等敏感操作对外完全不可见,杜绝了攻击者绕过缓存直连后端的可能;
- 逻辑解耦:将“缓存判断”、“回源获取”、“降级响应”拆分为独立location,避免单个content_by_lua_block膨胀为千行怪物;
- 性能优化:内部跳转零网络开销、零tcp握手,比外部重定向快两个数量级。
然而,匿名location的使用陷阱同样隐蔽:变量继承规则、header传递行为、子请求与内部跳转的差异……任何一个误解都可能导致缓存击穿、数据泄露或降级失效。本文将从原理到实战,彻底讲透匿名location在外置缓存中的正确用法。
二、匿名location的核心语义
2.1 与普通location的本质区别
| 特性 | 普通location | 匿名location (@name) |
|---|---|---|
| 外部可访问 | ✅ 是 | ❌ 否(返回404) |
| uri匹配 | 基于请求uri正则/前缀 | 仅通过名称精确引用 |
| 变量继承 | 完整继承 | ⚠️ 部分继承(见下文) |
| header传递 | 完整传递 | ⚠️ 需显式配置 |
| 日志记录 | 默认记录 | 默认不记录(需手动开启) |
| 嵌套定义 | 支持 | ❌ 不支持 |
📌 核心认知:匿名location不是“隐藏的url”,而是进程内的函数调用。它没有独立的请求生命周期,而是依附于父请求存在。理解这一点,才能避免用“url思维”去设计内部路由。
2.2 三种触发方式对比
# 方式1:try_files(最常用) try_files $uri @cache_backend; # 方式2:error_page(降级专用) error_page 590 =200 @redis_fallback; # 方式3:rewrite + last(条件跳转) rewrite ^/api/v1/(.*)$ @legacy_cache last;
| 触发方式 | 适用场景 | 变量传递 | 状态码控制 |
|---|---|---|---|
| try_files | 缓存miss后回源 | 完整继承 | 保留原状态码 |
| error_page | 异常降级 | 完整继承 | 可用=号重写 |
| rewrite last | 条件路由分发 | 完整继承 | 保留原状态码 |
⚠️ 关键区别:lua中的 ngx.location.capture("@name") 是子请求,而上述三种方式是内部跳转。子请求有独立的变量空间和header上下文,内部跳转则共享父请求上下文。混淆两者是踩坑的首要原因。
三、外置缓存中的标准模式
3.1 三层缓存+匿名location架构
http {
lua_shared_dict l1_cache 100m;
server {
listen 80;
# ========== 入口:统一缓存网关 ==========
location /api/ {
content_by_lua_block {
local key = ngx.var.scheme .. ":" .. ngx.var.host .. ngx.var.uri
-- l1: 本地共享字典
local val = ngx.shared.l1_cache:get(key)
if val then
ngx.header["x-cache"] = "l1-hit"
ngx.say(val)
return
end
-- l2/l3: 委托给匿名location处理
local res = ngx.location.capture("@cache_lookup", { share_all_vars = true })
if res.status == 200 then
ngx.header["x-cache"] = res.header["x-cache"] or "l2-hit"
ngx.say(res.body)
else
ngx.status = res.status
ngx.say(res.body)
end
}
}
# ========== l2: redis查询(匿名location)==========
location @cache_lookup {
internal;
content_by_lua_block {
local cache = require "cache_handler"
local key = ngx.var.scheme .. ":" .. ngx.var.host .. ngx.var.uri
local data, err = cache.get(key)
if data then
ngx.header["x-cache"] = "l2-hit"
ngx.say(cjson.encode(data))
return
end
if err == "redis_unavailable" then
-- 触发降级到l3
return ngx.exec("@cache_backend") -- ⭐ 内部跳转,非子请求
end
-- miss:也跳转回源
return ngx.exec("@cache_backend")
}
}
# ========== l3: 回源(匿名location)==========
location @cache_backend {
internal;
proxy_pass http://backend;
proxy_set_header host $host;
proxy_set_header x-real-ip $remote_addr;
# 回源成功后回填缓存的逻辑放在post_action或lua header_filter中
header_filter_by_lua_block {
if ngx.status == 200 then
ngx.ctx.cache_key = ngx.var.scheme .. ":" .. ngx.var.host .. ngx.var.uri
ngx.ctx.should_cache = true
end
}
body_filter_by_lua_block {
if ngx.ctx.should_cache and ngx.arg[2] then -- eof
local cache = require "cache_handler"
cache.set(ngx.ctx.cache_key, ngx.arg[1], 300)
end
}
}
# ========== 降级兜底(匿名location)==========
location @cache_static_fallback {
internal;
default_type application/json;
add_header x-cache-degraded "full-failure" always;
return 503 '{"code":503,"msg":"service temporarily unavailable"}';
}
}
}3.2 为什么回源要用匿名location而非直接proxy_pass?
将 proxy_pass 放入 @cache_backend 而非写在主location中,有三个工程价值:
- 复用性:多个入口location(/api/、/web/、/graphql)可共享同一个回源逻辑,修改一处全局生效;
- 可测试性:可通过 error_page 或 try_files 单独触发回源路径进行压测和验证;
- 关注点分离:主location只负责缓存决策,回源细节(超时、header、重试)封装在独立单元中。
四、变量与header传递的深水区
4.1 变量继承规则
匿名location通过内部跳转触发时,大部分变量自动继承,但以下例外必须注意:
| 变量 | 是否继承 | 说明 |
|---|---|---|
| $uri, $request_uri | ✅ 是 | 保持原始请求值 |
| $args | ✅ 是 | 查询参数完整传递 |
| $http_* | ✅ 是 | 客户端原始header |
| $upstream_* | ❌ 否 | 属于上一级upstream,重置为空 |
| $sent_http_* | ❌ 否 | 属于当前响应,未发送前为空 |
| lua ngx.var.* | ⚠️ 视情况 | share_all_vars=true时共享 |
⚠️ 高频踩坑:在 @cache_backend 中使用 $upstream_cache_status 总是为空,因为它属于proxy_cache模块的变量,而匿名location中没有启用proxy_cache。如需传递缓存状态,应通过自定义header或lua ctx。
4.2 header传递的正确姿势
# ❌ 错误:期望客户端header自动传到匿名location
location @cache_backend {
proxy_set_header x-user-id $http_x_user_id; # 可能为空!
}
# ✅ 正确:在主location中捕获,通过变量传递
location /api/ {
set $saved_user_id $http_x_user_id;
content_by_lua_block { ... ngx.exec("@cache_backend") ... }
}
location @cache_backend {
proxy_set_header x-user-id $saved_user_id; # 可靠
}对于lua子请求(ngx.location.capture),header默认不传递,必须显式指定:
local res = ngx.location.capture("@cache_lookup", {
share_all_vars = true,
ctx = { user_id = ngx.req.get_headers()["x-user-id"] }, -- 通过ctx传递
})4.3 lua ctx vs 变量:何时用哪个?
| 传递内容 | 推荐方式 | 原因 |
|---|---|---|
| 简单字符串(key、flag) | set $var + share_all_vars | 声明式、可读性好 |
| 复杂数据结构(table、userdata) | ngx.ctx | 变量只能存字符串 |
| 跨多个匿名location的状态 | ngx.ctx | 变量作用域限于单次跳转 |
| 需要被proxy_set_header使用的值 | set $var | proxy指令无法读取lua ctx |
五、安全防护:匿名≠安全
5.1 常见安全误区
| 误区 | 现实风险 |
|---|---|
| “@location外部访问不了,不用加鉴权” | 若误写为普通location或rewrite规则错误,可能意外暴露 |
| “internal就够了” | nginx配置热加载期间可能存在短暂窗口期 |
| “匿名location不记录日志,不影响审计” | 恰恰因为不记录,攻击利用时更难追溯 |
5.2 纵深防御清单
location @cache_backend {
internal; # 第1层:禁止外部访问
# 第2层:即使internal失效,也拒绝非预期来源
if ($http_x_internal_token != "your-secret") {
return 403;
}
# 第3层:限制方法
limit_except get head {
deny all;
}
# 第4层:开启审计日志
access_log /var/log/nginx/internal_access.log combined;
proxy_pass http://backend;
}📌 原则:
internal是必要条件,但不是充分条件。所有承载敏感操作的匿名location都应假设“可能被意外触达”,并据此设计额外防护。
六、调试与可观测性
6.1 让匿名location可见
# 为匿名location单独配置日志
log_format internal '$remote_addr [$time_local] "@$location_name" '
'$status $body_bytes_sent $request_time';
server {
location @cache_backend {
internal;
access_log /var/log/nginx/cache_internal.log internal;
# ...
}
}6.2 追踪内部跳转链路
在header中注入跳转路径,便于排查问题:
location @cache_lookup {
internal;
add_header x-internal-path "lookup" always;
# ...
}
location @cache_backend {
internal;
add_header x-internal-path "backend" always;
# ...
}配合响应头 x-cache: l2-hit 和 x-internal-path: lookup,可完整还原请求经过了哪些内部节点。
6.3 常见调试命令
# 验证匿名location确实不可外部访问 curl -v http://localhost/@cache_backend # 期望:404 not found # 通过正常入口触发,检查内部header curl -si http://localhost/api/config | grep x-internal # 查看内部日志 tail -f /var/log/nginx/cache_internal.log
七、常见踩坑速查表
| 现象 | 根因 | 解决方案 |
|---|---|---|
| @location返回404 | 拼写错误或未加@前缀 | 检查try_files/error_page中的名称 |
| 变量在@location中为空 | 未使用share_all_vars或未set保存 | 子请求加share_all_vars,跳转前set |
| header未传递到proxy_pass | 依赖 $ http_*但子请求不传递 | 改用set变量或ctx传递 |
| 降级未触发 | ngx.exec在ngx.say之后调用 | 确保exec在任何输出之前 |
| 循环跳转 | @a exec @b,@b又exec @a | 添加跳转深度计数器或状态标记 |
| 匿名location被外部访问 | 误删internal或配置语法错误 | nginx -t验证 + curl测试 |
| 日志中看不到内部请求 | 未单独配置access_log | 为@location添加独立日志指令 |
| lua ctx在跳转后丢失 | 使用了子请求而非内部跳转 | ngx.exec保留ctx,capture不保留 |
| proxy_set_header读到空值 | 变量在跳转后被重置 | 跳转前用set固化到命名变量 |
| 性能低于预期 | 误用子请求代替内部跳转 | 纯跳转用exec,需捕获响应用capture |
八、结语
到此这篇关于nginx外置缓存匿名location的使用的文章就介绍到这了,更多相关nginx外置缓存匿名location内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论