nginx本地缓存模块ngx_slowfs_cache
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. 测试
5. 清缓存
对于文件可以这么来做缓存。那么对于要经过php处理过的该怎么缓存呢? 请看下篇。
如需转载请注明出处:http://www.ttlsa.com/html/2390.html
成长的对话版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!