nginx 输出自定义response header 到access.log

本文介绍如何通过Nginx记录Squid代理服务器的缓存命中率,具体展示了如何配置Nginx来获取HTTP响应头中的X-Cache字段,并将其输出到access.log文件中。

需求背景:将squid 的命中率字段输出到nginx的access.log当中,以便统计缓存命中率。

关键:怎么取http的response 的header 值?
参考官方文档 ngx_http_upstream_module
连接:http://nginx.org/en/docs/http/ngx_http_upstream_module.html
发现如下这段话:

$upstream_http_name keep server response header fields. For example, the “Server” response header field is available through the $upstream_http_server variable. The rules of converting header field names to variable names are the same as for the variables that start with the “$http_” prefix. Only the header fields from the response of the last server are saved.
意思就是如果你想取response 头部的 connection(假定的自定义字段key) 字段则取值规则为:$upstream_http_connection。

抓取squid数据包:
这里写图片描述

命中率header字段为X-Cache:MISS from xxx
所以 修改日志格式化配置:

log_format  main  '$remote_addr -test-$remote_user[$time_local] "$request" ''$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$upstream_http_x_cache"';

注意X-Cache:对应nginx内置变量名为$upstream_http_x_cache,全小写,中横线变为下划线。
实际效果图:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值