Linux 缓存命中率,nginx缓存命中率统计(转)

转自:http://www.libertyvps.com/thread-275-1-1.html

nginx提供了$upstream_cache_status这个变量来显示缓存的状态,我们可以在配置中添加一个http头来显示这一状态,达到类似squid的效果。

Java代码 a4c26d1e5885305701be709a3d33442f.png a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

location / {

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 180;

proxy_send_timeout 180;

proxy_read_timeout 180;

proxy_buffer_size 128k;

proxy_buffers 4128k;

proxy_busy_buffers_size 128k;

proxy_temp_file_write_size 128k;

proxy_cache cache;

proxy_cache_valid 2003041h;

proxy_cache_valid 4041m;

proxy_cache_key $uri$is_args$args;

add_header Nginx-Cache "$upstream_cache_status";

proxy_pass http://backend;

}

location / {

proxy_redirect off;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_connect_timeout 180;

proxy_send_timeout 180;

proxy_read_timeout 180;

proxy_buffer_size 128k;

proxy_buffers 4 128k;

proxy_busy_buffers_size 128k;

proxy_temp_file_write_size 128k;

proxy_cache cache;

proxy_cache_valid 200 304 1h;

proxy_cache_valid 404 1m;

proxy_cache_key $uri$is_args$args;

add_header Nginx-Cache "$upstream_cache_status";

proxy_pass http://backend;

}

Java代码 a4c26d1e5885305701be709a3d33442f.png a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

HTTP/1.1200OK

Date: Mon, 22Apr201302:10:02GMT

Server: nginx

Content-Type: image/jpeg

Content-Length: 23560

Last-Modified: Thu, 18Apr201311:05:43GMT

Nginx-Cache: HIT

Accept-Ranges: bytes

Vary: User-Agent

HTTP/1.1 200 OK

Date: Mon, 22 Apr 2013 02:10:02 GMT

Server: nginx

Content-Type: image/jpeg

Content-Length: 23560

Last-Modified: Thu, 18 Apr 2013 11:05:43 GMT

Nginx-Cache: HIT

Accept-Ranges: bytes

Vary: User-Agent

为了能够统计缓存的命中率,我们需要在日志中记录这一状态

Java代码 a4c26d1e5885305701be709a3d33442f.png a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

log_format main'$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"'

'"$upstream_cache_status"';

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"'

'"$upstream_cache_status"';

统计方法:用HIT的数量除以日志总量得出缓存命中率:

Java代码 a4c26d1e5885305701be709a3d33442f.png a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

awk'{if($NF=="\"HIT\"") hit++} END {printf "%.2f%",hit/NR}'access.log

30.15%

awk '{if($NF=="\"HIT\"") hit++} END {printf "%.2f%",hit/NR}' access.log

30.15%

$upstream_cache_status包含以下几种状态:

·MISS 未命中,请求被传送到后端

·HIT 缓存命中

·EXPIRED 缓存已经过期请求被传送到后端

·UPDATING 正在更新缓存,将使用旧的应答

·STALE 后端将得到过期的应答

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值