Nginx的Web缓存服务主要由proxy_cache相关指令集和fastcgi_cache相关指令集构成,前者用于反向代理时,对后端内容源服务器进行缓存,后者主要用于对FastCGI的动态程序进行缓存。两者的功能基本上一样。
这里要介绍的第三方模块 ngx_slowfs_cache 扩充了Nginx的缓存功能,通过 ngx_slowfs_cache 可以实现本地站点静态文件缓存(配合root指令使用)。此功能为低速的存储设备创建快速缓存提供了可能。举个例子:
网站文件存放在一个网络存储上(network disks),缓存文件则存储在本地磁盘上。
网络存储使用的是 7200转的 SATA硬盘组,而本地磁盘使用的是 15000转的 SAS硬盘。
通过 ngx_slowfs_cache 将静态文件缓存到要地磁盘后,访问速度将明显改善。
ngx_slowfs_cache 模块同时也提供了“cache_purge”功能,用于清除指定URL的缓存。
ngx_slowfs_cache 当前的版本为:ngx_slowfs_cache-1.5
ngx_slowfs_cache 的下载地址是:http://labs.frickle.com/nginx_ngx_slowfs_cache/
ngx_slowfs_cache 配置参数(英文)
slowfs_cache zone_name (context: http, server, location)
——————————————————–
Sets area used for caching (previously definied using slowfs_cache_path).
slowfs_cache_key key (context: http, server, location)
——————————————————
Sets key for caching.
slowfs_cache_purge zone_name key (context: location)
—————————————————-
Sets area and key used for purging selected pages from cache.
slowfs_cache_path path [levels] keys_zone=zone_name:zone_size [inactive] [max_size] (context: http)
—————————————————————————————————
Sets cache area and its structure.
slowfs_temp_path path [level1] [level2] [level3] (context: http)
—————————————————————-
Sets temporary area where files are stored before they are moved to cache area.
Default: "/tmp 1 2"
slowfs_cache_min_uses number (context: http, server, location)
————————————————————–
Sets number of uses after which file is copied to cache.
Default: "1"
slowfs_cache_valid [reply_code] time (context: http, server, location)
———————————————————————-
Sets time for which file will be served from cache.
slowfs_big_file_size size (context: http, server, location)
———————————————————–
Sets minimum file size for "big" files. Worker processes fork() before
they start copying "big" files to avoid any service disruption.
Default: "128k"
ngx_slowfs_cache 配置变量
$slowfs_cache_status
——————–
Represents availability of cached file.
Possible values are: MISS, HIT and EXPIRED.
ngx_slowfs_cache 的安装
1、下载ngx_slowfs_cache,我们将得到一个文件 ngx_slowfs_cache-1.5.tar.gz
2、解压包 tar zxf ngx_slowfs_cache-1.5.tar.gz 得到目录 ngx_slowfs_cache-1.5
3、执行nginx编译,添加一条编译指令 –add-module=../ngx_slowfs_cache-1.5 即可将ngx_slowfs_cache模块编入nginx,完成的编译参数如:
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --add-module=../ngx_slowfs_cache-1.5
4、执行编译安装 make && make install
如果没有意外错误,至此您已经完成了 ngx_slowfs_cache 模块的安装。
ngx_slowfs_cache 配置举例
http {
#注:slowfs_cache_path和slowfs_temp_path指定的路径必须在同一分区
slowfs_temp_path /data0/slowfs_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
slowfs_cache_path /data0/slowfs_cache_dir levels=1:2 keys_zone=fastcache:200m inactive=1d max_size=30g;
server
{
listen 80;
server_name www.yourdomain.com 192.168.8.42;
index index.html index.htm;
root /data0/htdocs/www;
location /
{
slowfs_cache fastcache;
slowfs_cache_key $uri;
slowfs_cache_valid 1d;
}
#用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。
location ~ /purge(/.*)
{
#设置只允许指定的IP或IP段才可以清除URL缓存。
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
slowfs_cache_purge fastcache $1;
}
access_log off;
}
}
ngx_slowfs_cache 的使用
按如上配置完成以后,要确认一个文件是否被cache或要清除一个指定URL的缓存,只需要访问:
http://192.168.8.42/purge/application/my/view/images/user.png
purge 是要调用PURGE指令
/application/my/view/images/user.png 是你要确认或清除缓存的URL路径
转载自 <a href="http://www.yanghengfei.com/archives/363/" title="nginx ngx_slowfs_cache模块介绍" rel="bookmark">nginx ngx_slowfs_cache模块介绍 | 星外飞客 </a>
我简单说几句