使用Nginx第三方模块ngx_slowfs_cache缓存静态文件

发布时间:2019-08-10编辑:脚本学堂
ngx_slowfs_cache:Module adding ability to cache static files.ngx_slowfs_cache扩充了Nginx的缓存功能,通过 ngx_slowfs_cache 可以实现本地站点静态文件缓存。此功能为低速的存储设备创建快速缓存提供了可能。

ngx_slowfs_cache:Module adding ability to cache static files。
ngx_slowfs_cache扩充了nginx的缓存功能,通过 ngx_slowfs_cache 可以实现本地站点静态文件缓存。此功能为低速的存储设备创建快速缓存提供了可能。

通过 ngx_slowfs_cache 将静态文件缓存到要地磁盘后,访问速度将明显改善。而且通常缓存是先放到内存中,从内存中命中自然要比硬盘上命中快很多了。

ngx_slowfs_cache 模块同时也提供了“cache_purge”功能,用于清除指定URL的缓存。
ngx_slowfs_cache 的下载地址:http://labs.frickle.com/nginx_ngx_slowfs_cache/

设置缓存区域
path:存放缓存的路径
levels:缓存文件的目录级数
zone_name:缓存区域的名字
zone_size:内存缓存使用的大小
inactive:如果缓存数据在inactive定义的时间内未被访问,就被移除缓存
max_size:硬盘缓存大小

slowfs_temp_path path [level1] [level2] [level3] (context: http)

ngx_slowfs_cache 的安装
1、下载ngx_slowfs_cache,我们将得到一个文件 ngx_slowfs_cache-1.9.tar.gz
2、解压包 tar zxf ngx_slowfs_cache-1.9.tar.gz 得到目录 ngx_slowfs_cache-1.9
3、执行nginx编译,添加一条编译指令 --add-module=../ngx_slowfs_cache-1.9 即可将ngx_slowfs_cache模块编入nginx,完成的编译参数如:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=../ngx_slowfs_cache-1.9

如果是已经安装好的nginx,可以通过平滑升级来添加该模块。

4、执行编译安装 make && make install
如果没有意外错误,至此您已经完成了 ngx_slowfs_cache 模块的安装。

ngx_slowfs_cache 配置举例:
 

复制代码 代码如下:

slowfs_temp_path /usr/local/nginx/temp;(temp与cache路径一直)
slowfs_cache_path /usr/local/nginx/img levels=1:2 keys_zone=cache_one:50m inactive=1d max_size=1g;

server
{
    listen       80;
    server_name  www.test.com;
    index index.html index.htm;
    root  /usr/local/nginx/html;
 
#    location / {
#        slowfs_cache        cache_one;
#        slowfs_cache_key    $uri;
#        slowfs_cache_valid  1d;
#   }

location ~ .*.(gif|jpg|jpeg|png|bmp|swf|js|css|htm|html)$
{
        slowfs_cache        cache_one;
        slowfs_cache_key    $uri;
        slowfs_cache_valid  1d;
}

#     location ~ /purge(/.*) {
#            allow               127.0.0.1;
#            allow               192.168.0.215;
#            deny                all;
#            slowfs_cache_purge  cache_one $1;
#        }

location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
   expires      1d;
}

location ~ .*.(js|css)?$
{
   expires      2h;
}
}

slowfs_cache 可以用loation路径或者文件的类型来定义缓存
两种写法:
A:
     

复制代码 代码如下:
   location / {
            slowfs_cache        cache_one;
            slowfs_cache_key    $uri;
            slowfs_cache_valid  1d;
       }

B:
     

复制代码 代码如下:
  location ~ .*.(gif|jpg|jpeg|png|bmp|swf|js|css|htm|html)$
        {
            slowfs_cache        cache_one;
            slowfs_cache_key    $uri;
            slowfs_cache_valid  1d;
        }

    经测试,写法A只能缓存html等静态文件,无法缓存图片,可以用purge模块删除缓存。
    写法B则图片跟html都能缓存,但无法使用purge模块。(很纠结有木有。。。)

    因为是做图片服务器,所以这里采用写法B,没用purge模块,可以通过脚本来删除指定缓存。
    如下:
   

复制代码 代码如下:
#vi flush_cache
    #!/bin/bash
    if [ -n "$1" ]
    then
    grep -ra "^KEY" /usr/local/nginx/img/ | grep "$1$" | linuxjishu/13830.html target=_blank class=infotextkey>awk -F[:] '{print $1}' | xargs rm -rf
    else
    echo Operating is wrong
    fi

    要删除http://test.com/img/432.jpg,则执行
    #./flush_cache /img/432.jpg

    缺点是如果缓存很大,删除会比较慢。