- A+
nginx proxy反向代理本身就支持缓存的,但是如果没有使用到nginx反向代理的话,就需要使用ngx_slowfs_cache模块来实现本地站点静态文件缓存,同时还为低速的存储设备创建快速缓存。
1. 安装ngx_slowfs_cache和ngx_cache_purge模块
1 2 3 4 5 6 7 8 9 10 11 |
# 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. 使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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. 测试
对于文件可以这么来做缓存。那么对于要经过php处理过的该怎么缓存呢? 请看下篇。
如需转载请注明出处:http://www.ttlsa.com/html/2390.html

08/06/2017 下午 5:51 沙发
您好 楼主在吗 打扰您一下 我按照您的文档 添加安装了ngx_slowfs_cache(我以前已经装好nginx) ,nginx.conf里配置上slowfs_cache_path以后,检测nginx 就会报错 [root]# ./nginx -t
Segmentation fault
实在找不到原因,指望得到您的指导,非常感谢
28/07/2017 下午 2:52 1层
@liwj_netcom 同样的错误。求指点!
28/07/2017 下午 2:53 1层
@liwj_netcom 同样的错误,求指点!
28/07/2017 下午 4:01 2层
@二蛋 没解决,据查 ngx_slowfs_cache好像就支持到nginx 1.3 ,后面在没更新,不知道是不是新版nginx已经不支持了… :evil:
07/02/2017 下午 2:18 板凳
:mrgreen: :mrgreen: :mrgreen: 去
25/02/2016 下午 9:45 地板
[…] 27. srcache_nginx+redis构建缓存系统 28. nginx模块nginx-http-footer-filter研究使用 29. nginx本地缓存模块ngx_slowfs_cache 30. nginx+fancy实现漂亮的索引目录 31. nginx secure_link下载防盗链 32. […]
15/04/2015 下午 4:25 4楼
楼主你好,向您请教一个问题。写好一个新的缓存算法,如何将它用nginx实现呢?是改写proxy_cache和slowfs_cache呢?还是加载第三方模块呢?
15/04/2015 下午 5:39 1层
@王冬青 这个得你自己考虑了。只要性能能提升,改写或者加载第三方模块都OK。
17/04/2015 上午 10:47 2层
@TTLSA 谢谢啊
06/04/2015 上午 3:04 5楼
Windows下面有没有这个模块呢?
07/04/2015 上午 10:30 1层
@谷歌搜索 去找找咯
09/04/2015 下午 6:02 2层
@默北 没有找到,估计要重新编译nginx,还是Linux环境比较方便。
17/08/2013 下午 1:36 6楼
自带的和slowfs相比之下有什么差别?
17/08/2013 下午 1:52 1层
@youy 自带的是反代。
来自外部的引用: 2