需求背景:将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,全小写,中横线变为下划线。
实际效果图: