Nginx添加proxy_cache模块


为Nginx添加proxy_cache模块,对视频进行缓存
Nginx是高性能的HTTP服务器,通过Proxy Cache可以使其对视频进行缓存。
其原理就是把视频按照一定的规则存在本地硬盘,并且会在内存中缓存常用的资源,从而加快视频的响应。

假设nginx1.3.8已经安装成功。

1、安装Purge模块,Purge模块被用来清除缓存的。
安装之前先停止nginx ,$ /usr/local/nginx/sbin/nginx -s quit
$ wget http://labs.frickle.com/files/ngx_cache_purge-2.0.tar.gz
$ tar zxf ngx_cache_purge-2.0.tar.gz
nginx和ngx_cache_purge必须在同一级目录
$ cd nginx-1.3.8
查看编译参数
$ /usr/local/nginx/sbin/nginx -V
在原有的编译参数后面加上--add-module=../ngx_cache_purge-2.0
$ ./configure --user=www --group=www --prefix=/usr/local/nginx --add-module=../nginx_mod_h264_streaming-2.2.7 \
--with-http_flv_module --with-http_stub_status_module --with-http_ssl_module --with-http_mp4_module \
--with-http_gzip_static_module --with-pcre=../pcre-8.32 --with-pcre-jit --add-module=../ngx_cache_purge-2.0
$ make && make install

2、配置proxy_cahe

$ vim /usr/local/nginx/conf/nginx.conf
修改配置文件。
proxy_temp_path   /data/ngx_cache 1 2;

#keys_zone=cache1:100m 表示这个zone名称为cache1,分配的内存大小为100MB
#/data/ngx_cache/cache1 表示cache1这个zone的文件要存放的目录
#levels=1:2 表示缓存目录的第一级目录是1个字符,第二级目录是2个字符,即/data/ngx_cache/cache1/a/1b这种形式
#inactive=1d 表示这个zone中的缓存文件如果在1天内都没有被访问,那么文件会被cache manager进程删除掉
#max_size=10g 表示这个zone的硬盘容量为10GB

proxy_cache_path  /data/ngx_cache/cache1  levels=1:2 keys_zone=cache1:100m inactive=1d max_size=10g;

server {
    listen 80;
    server_name localhost;

    #在日志格式中加入$upstream_cache_status
     log_format format1 '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $http_x_forwarded_for $upstream_cache_status $request_time';

    access_log log/access.log format1;
    
     location /group1 {
          proxy_next_upstream http_502 http_504 error timeout invalid_header;
          proxy_set_header Host $host;
          proxy_set_header X-Forwarded-For $remote_addr;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_pass http://group1;
          #access_log off;
          #设置资源缓存的zone
          proxy_cache cache1;
          #设置缓存的key
          proxy_cache_key $host$uri$is_args$args;
          #设置状态码为200和304的响应可以进行缓存,并且缓存时间为30分钟
          proxy_cache_valid 200 304 30m;
          expires 1d;
          #$upstream_cache_status表示资源缓存的状态,有HIT MISS EXPIRED三种状态
          add_header X-Cache $upstream_cache_status;
     }
    
     #配置Purge
     #以下是nginx中的Purge配置片段
     location ~ /purge(/.*) {
          #允许的IP
          allow 127.0.0.1;
          #10网段的都可以访问
          allow 192.168.10.0/24;
          deny all;  #其他的禁止访问
          proxy_cache_purge cache1 $host$1$is_args$args;
     }
}
配置完毕,:wq! 保存退出
退出nginx,并重新启动

$ /usr/local/nginx/sbin/nginx -s quit
$ /usr/local/nginx/sbin/nginx

***********************************************
启动nginx若出现错误“nginx: [emerg] unknown directive "proxy_cache_purge" in /usr/local/nginx/conf/nginx.conf:96”
表示模块没安装成功,需要安装pcre
$ wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gz
$ tar zxf pcre-8.32.tar.gz
$ cd pcre-8.32
$ ./configure
$ make && make install
安装完pcre后重新执行安装nginx即可。
***********************************************

$upstream_cache_status包含以下几种状态:

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


清除缓存
使用方式:

$ wget http://192.168.10.16/purge/uri
其中uri为资源的URI,如果缓存的资源的URL为 http://192.168.10.16/group2/M00/00/46/ooYBAFIODMyAUSk3ANFjhglUy6s933.flv,
那么访问 http://192.168.10.16/purge/group2/M00/00/46/ooYBAFIODMyAUSk3ANFjhglUy6s933.flv则会清除缓存。

命中率
保存如下代码为hit_rate.sh:

#!/bin/bash
#
# proxy_cache hit rate

if [ $1x != x ] then
    if [ -e $1 ] then
        HIT=`cat $1 | grep HIT | wc -l`
        ALL=`cat $1 | wc -l`
         #Hit_rate=`echo "scale=2;($HIT/$ALL)*100" | bc`
        
Hit_rate=`awk 'BEGIN{printf "%.2f%\n",('$HIT'/'$ALL')*100}'`
         echo "Hit rate=$Hit_rate%"
    else
        echo "$1 not exsist!"
    fi
else
    echo "usage: ./hit_rate.sh file_path"
fi
使用方式

$ ./hit_rate.sh /usr/local/nginx/logs/access.log
 

猜你喜欢

转载自javapyer.iteye.com/blog/1986092