查看nginx cache命中率

一、在http header上增加命中显示

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

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;

}

而通过curl或浏览器查看到的header如下:

1

2

3

4

5

6

7

8

9

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

$upstream_cache_status包含以下几种状态:

·MISS 未命中,请求被传送到后端
·HIT 缓存命中
·EXPIRED 缓存已经过期请求被传送到后端
·UPDATING 正在更新缓存,将使用旧的应答
·STALE 后端将得到过期的应答

二、nginx cache命中率统计

即然nginx为我们提供了$upstream_cache_status函数,自然可以将命中状态写入到日志中。具体可以如下定义日志格式:

1

2

3

4

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的数量除以日志总量得出缓存命中率:

1

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

了解了原理以后,也可以通过crontab脚本将每天的命中率统计到一个日志中,以备查看。

1

2

# crontab -l

1 0 * * * /opt/shell/nginx_cache_hit >> /usr/local/nginx/logs/hit

访脚本的内容为:

1

2

3

4

#!/bin/bash

LOG_FILE=‘/usr/local/nginx/logs/access.log.1‘

LAST_DAY=$(date +%F -d "-1 day")

awk ‘{if($NF==""HIT"") hit++} END {printf "‘$LAST_DAY‘: %d %d %.2f%n", hit,NR,hit/NR}‘ $LOG_FILE

转自:http://www.361way.com/nginx-cache/2665.html

时间: 2024-08-03 08:55:01

查看nginx cache命中率的相关文章

查看buffer cache命中率

SQL> select name,value from v$sysstat where name in('db block gets','consistent gets','physical reads'); NAME                                                                  VALUE---------------------------------------------------------------- -----

查看nginx.apache.mysql.php squid 编译参数

查看nginx服务的编译参数 /application/nginx/sbin/nginx -V [[email protected] ~]# /application/nginx/sbin/nginx -V nginx version: nginx/1.2.1 built by gcc 4.1.2 20080704 (Red Hat 4.1.2-52) configure arguments: --user=nginx --group=nginx --prefix=/application/ng

如何查看nginx状态?如何统计访问量?

nginx是个什么玩意儿本文不再多说,直奔主题! 1. 如何查看nginx的状态? stub_status 模块介绍 stub_status 模块主要用于查看Nginx的一些状态信息. 本模块默认是不会编译进Nginx的,如果你要使用该模块,则要在编译安装Nginx时指定: ./configure –with-http_stub_status_module 一.查看已安装的 Nginx 是否包含 stub_status 模块 #/usr/local/nginx/sbin/nginx -V 如果包

nginx cache 使用 【转】

1.编译安装安装cache_purge模块,可以参考我写的:http://bbs.linuxtone.org/thread-6875-1-1.htmlnginx的server配置: server { listen       80; server_name 54yancheng.com www.54yancheng.com; #purge cache files location ~ /purge(/.*) { #设置只允许指定的IP或IP段才可以清除URL缓存,加入你的squid服务器的ip地

查看nginx,apache,mysql,php的编译参数

查看nginx编译参数:/usr/local/nginx/sbin/nginx -V查看apache编译参数:cat /usr/local/apache2/build/config.nice查看mysql编译参数:cat /usr/local/mysql/bin/mysqlbug | grep CONFIGURE_LINE查看php编译参数:/usr/local/php/bin/php -i | grep configure

如何用linux命令查看nginx是否在正常运行

有时想知道nigix是否在正常运行,需要用linux命令查看nginx运行情况. 执行命令: ps -A | grep nginx 如果返回结果的话,说明有nginx在运行,服务已经启动. 如果不怕nginx关闭的话.也可以执行: service nginx restart 把nginx服务重启,在重启时,也可以看到具体有没有什么地方出错.

查看Nginx、apache、MySQL和PHP的编译参数

Linux下查看Nginx.Napache.MySQL.PHP的编译参数的命令如下: 1.nginx编译参数:#/usr/local/nginx/sbin/nginx -V2.apache编译参数:# cat /usr/local/apache/build/config.nice3.php编译参数:# /usr/local/php/bin/php -i |grep configure4.mysql编译参数:# cat /usr/local/mysql/bin/mysqlbug|grep conf

linux中查看nginx、apache、php、mysql配置文件路径的方法

转自:http://www.phper163.com/archives/368 如何在linux中查看nginx.apache.php.mysql配置文件路径了,如果你接收一个别人配置过的环境,但没留下相关文档.这时该怎么判断找到正确的加载文件路径了.可以通过以下来判断1.判断apache首先执行命令找到httpd路径ps aux | grep httpd如httpd路径为 /usr/local/apache/bin/httpd然后执行以下命令/usr/local/apache/bin/http

查看nginx apache mysql php 编译参数

查看nginx编译参数:/usr/local/nginx/sbin/nginx -V查看apache编译参数:cat /usr/local/apache2/build/config.nice查看mysql编译参数:cat /usr/local/mysql/bin/mysqlbug | grep CONFIGURE_LINE查看php编译参数:/usr/local/php/bin/php -i | grep configure