最新消息:

Nginx Proxy_Cache 命中率

cache admin 3590浏览 0评论

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

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;
}
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

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

log_format www.aikaiyuan.com '$remote_addr – $remote_user [$time_local] "$request" '
          '$status $body_bytes_sent "$http_referer" '
          '"$http_user_agent" "$http_x_forwarded_for"'
         '"$upstream_cache_status"';

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

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

转载请注明:爱开源 » Nginx Proxy_Cache 命中率

您必须 登录 才能发表评论!