$sent_http_*
以 $sent_http_ 前缀开头的变量返回发送给客户端的 HTTP 响应头。 — NGINX Core (HTTP)
$sent_http_*
NGINX Core (HTTP)
说明
在 NGINX 中,当在 server 或 location 块中设置了某些响应头时,$sent_http_ 前缀的变量会被自动生成。对于发送给客户端的每个 HTTP 响应头,都有一个对应的以 $sent_http_ 为前缀的变量。例如,如果你在 server 配置中发送了 HTTP 头 'X-My-Header',对应的变量将是 $sent_http_x_my_header。通过这些变量,你可以在 NGINX 配置或日志格式中直接访问响应中已发送的 HTTP 头的值。 该变量在需要进行条件日志记录或基于特定头的值自定义输出的场景中特别有用。只有在响应中实际设置了相应头时,$sent_http_ 变量才会被填充;如果某个头未被设置,对应的变量将为空。该机制有助于跟踪和管理各种头信息,这在调试或验证服务器行为(尤其是在测试期间)等场景中至关重要。
配置示例
location /example {
add_header X-My-Header "MyValue";
add_header X-Another-Header "AnotherValue";
# Log sent headers
access_log /var/log/nginx/example.log custom_format;
}
log_format custom_format 'Sent HTTP Headers: $sent_http_x_my_header, $sent_http_x_another_header';子系统
http可缓存
是类型
前缀变量上下文
http, server, location, if⚠
确保相应的 HTTP headers 已实际设置;否则,该变量将为空。
⚠
变量名必须为小写,并使用下划线替代连字符,因为 NGINX 会相应地转换头名称(例如 X-My-Header 会变为 x_my_header)。