当前位置:首页 > PHP教程 > PHP总结归纳

利用ProxyCache使Nginx对静态资源进行缓存

前言 nginx是高性能的http服务器,通过proxy cache可以使其对静态资源进行缓存。其原理就是把静态资源按照一定的规则存在本地硬盘,并且会在内存中缓存常用的资源,从而加快静态资源的响应。 配置proxy cache 以下为nginx配置片段: proxy_temp_path /usr/lo

前言

nginx是高性能的http服务器,通过proxy cache可以使其对静态资源进行缓存。其原理就是把静态资源按照一定的规则存在本地硬盘,并且会在内存中缓存常用的资源,从而加快静态资源的响应。

配置proxy cache

以下为nginx配置片段:

proxy_temp_path /usr/local/nginx/proxy_temp_dir 1 2; #keys_zone=cache1:100m 表示这个zone名称为cache1,分配的内存大小为100mb #/usr/local/nginx/proxy_cache_dir/cache1 表示cache1这个zone的文件要存放的目录 #levels=1:2 表示缓存目录的第一级目录是1个字符,第二级目录是2个字符,即/usr/local/nginx/proxy_cache_dir/cache1/a/1b这种形式 #inactive=1d 表示这个zone中的缓存文件如果在1天内都没有被访问,那么文件会被cache manager进程删除掉 #max_size=10g 表示这个zone的硬盘容量为10gb proxy_cache_path /usr/local/nginx/proxy_cache_dir/cache1 levels=1:2 keys_zone=cache1:100m inactive=1d max_size=10g; server { listen 80; server_name *.example.com; #在日志格式中加入$upstream_cache_status log_format format1 '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" $upstream_cache_status'; access_log log/access.log fomat1; #$upstream_cache_status表示资源缓存的状态,有hit miss expired三种状态 add_header x-cache $upstream_cache_status; location ~ .(jpg|png|gif|css|js)$ { proxy_pass http://127.0.0.1:81; #设置资源缓存的zone proxy_cache cache1; #设置缓存的key proxy_cache_key $host$uri$is_args$args; #设置状态码为200和304的响应可以进行缓存,并且缓存时间为10分钟 proxy_cache_valid 200 304 10m; expires 30d; } } 安装purge模块

purge模块被用来清除缓存

$ wget http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz $ tar -zxvf ngx_cache_purge-1.2.tar.gz

查看编译参数

$ /usr/local/nginx/sbin/nginx -v

在原有的编译参数后面加上--add-module=/usr/local/ngx_cache_purge-1.2

$ ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --add-module=/usr/local/ngx_cache_purge-1.2 $ make && make install

退出nginx,并重新启动

$ /usr/local/nginx/sbin/nginx -s quit $ /usr/local/nginx/sbin/nginx 配置purge

以下是nginx中的purge配置片段

location ~ /purge(/.*) { #允许的ip allow 127.0.0.1; deny all; proxy_cache_purge cache1 $host$1$is_args$args; } 清除缓存

使用方式:

$ wget http://example.com/purge/uri

其中uri为静态资源的uri,如果缓存的资源的url为 http://example.com/js/jquery.js,那么访问 http://example.com/purge/js/jquery.js则会清除缓存。

命中率

保存如下代码为hit_rate.sh:

#!/bin/bash # author: jeremy wei # 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` 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/log/access.log 参考:

http://wiki.nginx.org/httpproxymodule



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!