一、在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

 
 

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

  1. 查看buffer cache命中率

    SQL> select name,value from v$sysstat where name in('db block gets','consistent gets','physical r ...

  2. nginx cache的玩法

      一.简介 Nginx版本从0.7.48开始,支持了类似Squid的缓存功能.这个缓存是把URL及相关组合当做Key,用Md5算法对Key进行哈希,得到硬盘上对应的哈希目录路径,从而将缓存内容保存在 ...

  3. 查看nginx配置文件路径

    进入nginx安装目录(我的是/usr/local/nginx-1.7.8/) 进入sbin目录,输入 ./nginx -t查看nginx配置文件路径以及该文件语法是否正确 ./nginx -v查看n ...

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

      有时想知道nigix是否在正常运行,需要用linux命令查看nginx运行情况. 执行命令: ps -A | grep nginx  如果返回结果的话,说明有nginx在运行,服务已经启动. 如果 ...

  5. 查看nginx编译安装

    大家是否遇到过去了新公司,公司内的LAMP,LNMP等所有的环境都是配置好的(已经在提供服务了),公司又没有留下部署文档,甚至安装LAMP,LAMP等环境的人已经和你交接完离职了,那么线上服务器(la ...

  6. CentOS下查看nginx和php的编译参数

    在已经编译安装好的nginx和php的server上是可以查看之前编译时候的参数的,方法如下. 1.查看nginx的编译参数 # nginx -V nginx version: nginx/1.9.4 ...

  7. 【学习笔记】启动Nginx、查看nginx进程、查看nginx服务主进程的方式、Nginx服务可接受的信号、nginx帮助命令、Nginx平滑重启、Nginx服务器的升级

     1.启动nginx的方式: cd /usr/local/nginx ls ./nginx -c nginx.conf 2.查看nginx的进程方式: [root@localhost nginx] ...

  8. 查看Nginx、PHP、Apache和MySQL的编译参数

    1.查看Nginx编译参数 [root@portal finance]# your_nginx_dir/sbin/nginx -V nginx version: nginx/ built by (Re ...

  9. 查看nginx | apache | php | tengine | tomcat版本的信息以及如何隐藏版本信息【转】

    转自: 查看nginx | apache | php | tengine | tomcat版本的信息以及如何隐藏版本信息 - 追马 - 51CTO技术博客http://lovelace.blog.51 ...

随机推荐

  1. 常用jdbc操作

    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");   Connection con = DriverMa ...

  2. 配置SELINUX

    selinux的配置文件:# more /etc/selinux/config # This file controls the state of SELinux on the system. # S ...

  3. LevelDB场景分析4--BackgroundCompaction

    1.DBImpl::Open      uint64_t new_log_number = impl->versions_->NewFileNumber();      WritableF ...

  4. 关于JavaScript中name的意义冲突

    在昨天的<Javascript权威指南>学习笔记之十:ECMAScript 5 增强的对象模型一文中,对于一段代码的调试出现了一个奇怪现象,现将源码贴在以下: <script typ ...

  5. STM32 可编程电压监测器(PVD)实现数据掉电保存

    STM32内部有一个完整的上电复位和掉电复位电路,当供电电压达到2v时系统即能正常工作. STM32内部自带PVD功能,用于对MCU供电电压VDD进行监控.通过电源控制寄存器中的PLS[2:0]位可以 ...

  6. gulp#4.0 Did you forget to signal async completion?

    异常截图 解决方案: https://stackoverflow.com/questions/36897877/gulp-error-the-following-tasks-did-not-compl ...

  7. unity, 延迟执行代码

    使用协程实现比较方便,可以带参数. void Start(){ StartCoroutine(delayLaunchRocket(rocket,2.0f)); } IEnumerator delayL ...

  8. angularJS核心原理

    一.angularJS优点和缺点 优点 1.1免去重复劳动-获取元素.给元素加事件.创建元素 1.2接管UI:angularJS根据数据创建UI元素,免去手工创建UI元素. 1.3自动同步:根据数据个 ...

  9. controller与requestmapping

    使用@controller定义controllersSpring mvc将特定url的请求分发到controller类来进行处理在spring 3.0中,通过@controller标注即可将class ...

  10. MarkDown的vim插件安装

    作用:可以使markdown语法高亮.1.安装.使用pathogen插件管理.    cd ~/.vim/bundle    git clone https://github.com/plasticb ...