欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

29)nginx本地缓存模块ngx_slowfs_cache

2013年8月17日
 

nginx proxy反向代理本身就支持缓存的,但是如果没有使用到nginx反向代理的话,就需要使用ngx_slowfs_cache模块来实现本地站点静态文件缓存,同时还为低速的存储设备创建快速缓存。

1. 安装ngx_slowfs_cache和ngx_cache_purge模块

# wget http://labs.frickle.com/files/ngx_slowfs_cache-1.10.tar.gz
# wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz
# tar zxvf ngx_slowfs_cache-1.10.tar.gz
# tar zxvf ngx_cache_purge-2.1.tar.gz
# cd nginx-1.2.5
# ./configure --prefix=/usr/local/nginx-1.2.5 \
--with-http_stub_status_module --with-http_realip_module \
--add-module=../ngx_slowfs_cache-1.10 \
--add-module=../ngx_cache_purge-2.1
# make
# make install

2. 使用

http {
    slowfs_cache_path /data/cache/proxy_cache_dir  levels=1:2   keys_zone=fastcache:4096m inactive=1d max_size=20g;
    slowfs_temp_path  /data/cache/proxy_temp_dir 1 2;

    server {
	    location ~ /purge(/.*) {
            allow               127.0.0.1;
            allow               10.0.0.0/8;
            deny                all;
            slowfs_cache_purge  fastcache $1;
        }

        location ~* \.(gif|jpg|jpeg|css|js|bmp|png)$ {
            slowfs_cache        fastcache;
            slowfs_cache_key    $uri;
            slowfs_cache_valid  1d;
            add_header X-Cache '$slowfs_cache_status from $host';
            expires  max;
        }

    }
}

说明:slowfs_cache_path和slowfs_temp_path需要在同一分区。 slowfs_cache_path指定缓存文件的目录级数,缓存区名称为fastcache,内存缓存空间为4096m,1天没有被访问的内容自动清除,硬盘缓存空间为20g。slowfs_cache_purge为清除缓存。
要注意location执行顺序。nginx purge更新缓存404错误 一例参见http://www.ttlsa.com/html/1084.html

3. 模块指令说明
slowfs_cache
语法:slowfs_cache zone_name
默认值:none
配置段:http, server, location
定义使用的缓存区。要与slowfs_cache_path指令定义的一致。

slowfs_cache_key
语法: slowfs_cache_key key
默认值: none
配置段: http, server, location
设置缓存的键

slowfs_cache_purge
语法: slowfs_cache_purge zone_name key
默认值: none
配置段: location
根据指定的key从缓存中清除也存在的缓存

slowfs_cache_path
语法: slowfs_cache_path path [levels] keys_zone=zone_name:zone_size [inactive] [max_size]
默认值: none
配置段: http
设置缓存区和缓存结构

slowfs_temp_path
语法: slowfs_temp_path path [level1] [level2] [level3]
默认值: /tmp 1 2
配置段: http
设置临时区。文件在移到缓存区时的临时存储地。

slowfs_cache_min_uses
语法: slowfs_cache_min_uses number
默认值: 1
配置段: http, server, location
设置文件被访问多少次后复制到缓存

slowfs_cache_valid
语法: slowfs_cache_valid [reply_code] time
默认值: none
配置段: http, server, location
设置缓存时间

slowfs_big_file_size
语法: slowfs_big_file_size size
默认值: 128k
配置段: http, server, location
设置大文件阀值,避免服务中断

$slowfs_cache_status变量:
表示缓存文件的可用性,值有MISS, HIT, EXPIRED

4. 测试

nginx
5. 清缓存

nginx
对于文件可以这么来做缓存。那么对于要经过php处理过的该怎么缓存呢? 请看下篇。
如需转载请注明出处:http://www.ttlsa.com/html/2390.html

版权说明

文章标题: nginx本地缓存模块ngx_slowfs_cache
本文地址: http://www.ttlsa.com/nginx/local-nginx-cache-module-ngx_slowfs_cache/
除非注明,博客文章均为"运维生存时间"原创,转载请标明本文地址
交流群:①群39514058(满)、②群6690706(满)、③群168085569(新)

关注微博

 
 
  1. 楼主你好,向您请教一个问题。写好一个新的缓存算法,如何将它用nginx实现呢?是改写proxy_cache和slowfs_cache呢?还是加载第三方模块呢?

普通分类: