memc_nginx+srcache_nginx+memcached构建透明的动态页面缓存
在上一节《nginx+memcached构建页面缓存应用》http://www.ttlsa.com/html/2418.html中,说道nginx只负责从memcached服务器中读取数据,要往memcached写入数据还得需要后台的应用程序来完成。使用memc-nginx和srcache-nginx模块就可以主动的向memcached添加缓存,对应用程序来说是透明的。大大的提高动态页面访问速度。第一次访问创建缓存,后续访问在缓存过期时间内,直接从memcached返回,不需要再次经过php-fpm处理。
nginx_memc模块与nginx_srcache模块配合使用,来提供缓存服务器后端的操作,在技术上,任何提供REST接口的模块都可以与nginx_srcache配合使用来获取和存储缓存子请求。
使用memcached作为后端缓存,需要注意memcached存储大小的限制,不得超过1M。为了使用更宽松的后端存储服务,建议使用redis等,参见《srcache_nginx+redis构建缓存系统》。
1. memc-nginx-module模块指令说明:
memc_pass
语法:memc_pass address:port or socket;
默认值:none
配置段:http, server, location, if
指定memcached服务器地址。
memc_cmds_allowed
语法:memc_cmds_allowed <cmd>...
默认值: none
配置段:http, server, location, if
列出允许访问的memcached命令。默认情况下,所有的memcached命令都可以访问。
memc_flags_to_last_modified
语法:memc_flags_to_last_modified on|off
默认值:off
配置段:http, server, location, if
读取memcached标识,并将其设置为Last-Modified头部值。对于有条件的get,nginx返回304未修改响应,以便节省带宽。
memc_connect_timeout
语法:memc_connect_timeout <time>
默认值:60s
配置段: http, server, location
与memcached服务器建立连接的超时时间。不得超过597 hours。
memc_send_timeout
语法:memc_send_timeout <time>
默认值:60s
配置段:http, server, location
设置发送请求到memcached服务器的超时时间。不得超过597 hours。
memc_read_timeout
语法:memc_read_timeout <time>
默认值:60s
配置段:http, server, location
定义从memcached服务器读取响应超时时间。不得超过597 hours。
memc_buffer_size
语法:memc_buffer_size <size>
默认值:4k/8k
配置段:http, server, location
读取从memcached服务器接收到响应的缓冲大小。
2. memcached支持的命令
memcached存储命令set、add、replace、prepend、append,以$memc_key作为键。$memc_exptime定义过期时间,默认值为0。$memc_flags作为标识,默认值为0,来建立相应的memcached查询。
如果$memc_value没有定义,那么请求的请求体将作为该值,除了incr和decr命令外。注意:如果$memc_value定义为空的字符串,那么该空字符串仍然被当做该值。
2.1 get $memc_key
使用键来检索值。
1
2
3
4
5
6
|
location
/
foo
{
set
$
memc
_cmd
'get'
;
set
$
memc
_key
'my_key'
;
memc
_pass
127.0.0.1
:
11211
;
add
_header
X
-
Memc
-
Flags
$
memc_flags
;
}
|
如果该键被找到,响应体为该键值,返回200。否则范围404 Not Found。如果发生错误或客户端错误或服务端错误,则返回502。标识码设置到$memc_flags变量。通常使用add_header指令来添加到响应头部。
2.2 set $memc_key $memc_flags $memc_exptime $memc_value
将请求体作为memcached值。如果另外指定值可以通过$memc_value变量来指定。
1
2
3
4
5
6
7
|
location
/
foo
{
set
$
memc
_cmd
'set'
;
set
$
memc
_key
'my_key'
;
set
$
memc
_flags
12345
;
set
$
memc
_exptime
24
;
memc
_pass
127.0.0.1
:
11211
;
}
|
或
1
2
3
4
5
6
7
8
|
location
/
foo
{
set
$
memc
_cmd
'set'
;
set
$
memc
_key
'my_key'
;
set
$
memc
_flags
12345
;
set
$
memc
_exptime
24
;
set
$
memc
_value
'my_value'
;
memc
_pass
127.0.0.1
:
11211
;
}
|
返回201,说明创建memcached缓存存储成功。返回200说明NOT_STORED。返回404说明NOT_FOUND。返回502说明发生错误或客户端错误或服务端错误。
memcached原始响应是响应体,404 NOT FOUND除外。
2.3 add $memc_key $memc_flags $memc_exptime $memc_value
和set命令相似
2.4 prepend $memc_key $memc_flags $memc_exptime $memc_value
和set命令相似
2.5 delete $memc_key
删除该键值
1
2
3
4
5
6
|
location
/
foo
set
$
memc_cmd
delete
;
set
$
memc_key
my_key
;
memc
_pass
127.0.0.1
:
11211
;
}
|
返回200说明删除成功。返回404说明NOT_FOUND。返回502说明发生错误或客户端错误或服务端错误。
2.6 delete $memc_key $memc_exptime
和delete命令相似。
2.7 incr $memc_key $memc_value
给指定的$memc_key对应的$memc_value增量。
1
2
3
4
5
|
location
/
foo
{
set
$
memc_key
my_key
;
set
$
memc
_value
2
;
memc
_pass
127.0.0.1
:
11211
;
}
|
每次访问/foo将导致my_key的值加2。
返回200说明成功。返回404说明键Not Found。返回502说明发生错误或客户端错误或服务端错误。
2.8 decr $memc_key $memc_value
与incr相似。
2.9 flush_all
刷新memcached上所有的键。
1
2
3
4
|
location
/
foo
{
set
$
memc_cmd
flush_all
;
memc
_pass
127.0.0.1
:
11211
;
}
|
2.10 flush_all $memc_exptime
与flush_all相似。
2.11 stats
输出memcached统计信息。
1
2
3
4
|
location
/
foo
{
set
$
memc_cmd
stats
;
memc
_pass
127.0.0.1
:
11211
;
}
|
2.12 version
返回memcached版本信息。
1
2
3
4
|
location
/
foo
{
set
$
memc_cmd
version
;
memc
_pass
127.0.0.1
:
11211
;
}
|
3. srcache-nginx-module模块指令说明:
srcache_fetch
语法:srcache_fetch <method> <uri> <args>?
默认值:no
配置段:http, server, location, location if
查询缓存。返回200说明缓存命中,直接从缓存响应客户端请求。非200需要后端程序处理。
srcache_fetch_skip
语法:srcache_fetch_skip <flag>
默认值:srcache_fetch_skip 0
配置段:http, server, location, location if
<flag>支持nginx变量。当这个参数值不为空和不等于0,则从缓存取数据过程被无条件跳过。
srcache_store
语法:srcache_store <method> <uri> <args>?
默认值:no
配置段:http, server, location, location if
将当前请求的响应存入缓存。可以使用srcache_store_skip和srcache_store_max_size指令禁用缓存。不管是响应状态行,响应头,响应体都会被缓存。默认情况下,下列特殊响应头不会被缓存:
Connection
Keep-Alive
Proxy-Authenticate
Proxy-Authorization
TE
Trailers
Transfer-Encoding
Upgrade
Set-Cookie
可以使用srcache_store_pass_header、srcache_store_hide_header指令来控制哪些头要缓存哪些不要。
注意:即使所有的响应数据被立即发送,当前的nginx请求生命周期未必完成,直到srcache_store子请求完成。这意味着服务器端延迟关闭TCP连接,或下一个请求服务发送同一个TCP连接。
srcache_store_max_size
语法:srcache_store_max_size <size>
默认值:srcache_store_max_size 0
配置段:http, server, location, location if
当响应体超过该值,将不会缓存。
当后端缓存存储有对缓存数据做硬限制,这个指令非常有用。比如memcached服务器,上限是1M。
默认值0,不限制。
srcache_store_skip
语法:srcache_store_skip <flag>
默认值:srcache_store_skip 0
配置段:http, server, location, location if
<flag>支持nginx变量。当这个参数值不为空和不等于0,则从存入缓存过程被无条件跳过。
srcache_store_statuses
语法:srcache_store_statuses <status1> <status2> ..
默认值:srcache_store_statuses 200 301 302
配置段:http, server, location, location if
该指令控制那些状态码响应被缓存。
srcache_header_buffer_size
语法:srcache_header_buffer_size <size>
默认值:srcache_header_buffer_size 4k/8k
配置段:http, server, location, location if
在序列化响应头时控制头缓冲大小。默认大小为页面大小,通常为4k或8k,取决于具体平台。
注意:该大小是以每个头的,因此,需要足够大来容纳最大响应头。
srcache_store_hide_header
语法:srcache_store_hide_header <header>
默认值:no
配置段:http, server, location, location if
默认情况下,除了以下头缓存所有响应头:
Connection
Keep-Alive
Proxy-Authenticate
Proxy-Authorization
TE
Trailers
Transfer-Encoding
Upgrade
Set-Cookie
可以隐藏多个响应头,不区分大小写。如
srcache_store_hide_header X-Foo;
srcache_store_hide_header Last-Modified;
srcache_store_pass_header
语法:srcache_store_pass_header <header>
默认值:no
配置段:http, server, location, location if
默认情况下,除了以下头缓存所有响应头:
Connection
Keep-Alive
Proxy-Authenticate
Proxy-Authorization
TE
Trailers
Transfer-Encoding
Upgrade
Set-Cookie
可以缓存多个响应头,不区分大小写。如
srcache_store_pass_header Set-Cookie;
srcache_store_pass_header Proxy-Autenticate;
srcache_methods
语法:srcache_methods <method>...
默认值:srcache_methods GET HEAD
配置段:http, server, location
srcache_ignore_content_encoding
语法:srcache_ignore_content_encoding on|off
默认值: srcache_ignore_content_encoding off
配置段:http, server, location, location if
内容是否编码。
建议后端服务器禁用gzip/deflate压缩。在nginx.conf配置:
1
|
proxy_set_header
Accept
-
Encoding
""
;
|
srcache_request_cache_control
语法:srcache_request_cache_control on|off
默认值:srcache_request_cache_control off
配置段:http, server, location
当该指令为on时,请求头Cache-Control和Pragma按照下面的方法处理:
1. srcache_fetch查询缓存操作时,当请求头Cache-Control: no-cache 、 Pragma: no-cache 将跳过。
2. srcache_store存入缓存操作时,当请求头Cache-Control: no-store将跳过。
当该指令为off时,将禁用此功能,对于繁忙的站点依赖缓存加速被认为是最安全的。
srcache_response_cache_control
语法:srcache_response_cache_control on|off
默认值:srcache_response_cache_control on
配置段:http, server, location
当该指令为on时,响应头Cache-Control和Expires按照下面的方法处理:
Cache-Control: private skips srcache_store,
Cache-Control: no-store skips srcache_store,
Cache-Control: no-cache skips srcache_store,
Cache-Control: max-age=0 skips srcache_store,
Expires: <date-no-more-recently-than-now> skips srcache_store.
该指令优先级比srcache_store_no_store,srcache_store_no_cache,srcache_store_private高。
srcache_store_no_store
语法:srcache_store_no_store on|off
默认值:srcache_store_no_store off
配置段:http, server, location
开启该指令,将强制响应头Cache-Control: no-store。默认为关闭。
srcache_store_no_cache
语法:srcache_store_no_cache on|off
默认值:srcache_store_no_cache off
配置段:http, server, location
开启该指令,将强制响应头Cache-Control: no-cache。默认为关闭。
srcache_store_private
语法:srcache_store_private on|off
默认值:srcache_store_private off
配置段:http, server, location
开启该指令,将强制响应头Cache-Control: private。默认为关闭。
srcache_default_expire
语法:srcache_default_expire <time>
默认值:srcache_default_expire 60s
配置段:http, server, location, location if
控制默认过期时间。当响应头既没有Cache-Control: max-age=N也没有指定Expires时,允许的$srcache_expire变量值。
该值必须小于597hours。
srcache_max_expire
语法:srcache_max_expire <time>
默认值:srcache_max_expire 0
配置段:http, server, location, location if
控制最大缓存时间,此设置优先级高于其他计算方法。
该值必须小于597hours。
默认为0,不限制。
4. srcache-nginx-module变量
$srcache_expire
当前的响应存入缓存的过期时间。按照下面的方法计算:
1. 当响应头Cache-Control: max-age=N被指定,那么N将作为过期时间。
2. 如果响应头Expires被指定,那么该值与当前时间差作为过期时间。
3. 当既没有指定Cache-Control: max-age=N也没有指定Expires,那么使用 srcache_default_expire 指定的值。
如果超过srcache_max_expire指令值,那么此变量最终值为srcache_max_expire。
$srcache_fetch_status
获取缓存的三种状态值:HIT, MISS, BYPASS。
$srcache_store_status
存入缓存的两种状态值:STORE ,BYPASS。
5. 安装nginx_memc和nginx_srcache模块
1
2
3
4
5
6
7
|
# wget https://github.com/agentzh/memc-nginx-module/archive/master.zip
# wget https://github.com/agentzh/srcache-nginx-module/archive/master.zip
# ./configure --prefix=/usr/local/nginx-1.2.5 \
--
add
-
module
=
.
.
/
srcache
-
nginx
-
module
--
add
-
module
=
.
.
/
memc
-
nginx
-
module
# make
# make install
|
6. 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
upstream
memcacheds
{
server
10.1.240.166
:
22222
;
}
server
{
listen
8090
;
server_name
test
.
ttlsa
.
com
;
index
index
.
html
index
.
htm
index
.
php
;
root
/
data
/
wwwroot
/
www
.
ttlsa
.
com
/
webroot
;
location
/
memc
{
internal
;
memc_connect
_timeout
100ms
;
memc_send
_timeout
100ms
;
memc_read
_timeout
100ms
;
set
$
memc
_key
$
query_string
;
set
$
memc
_exptime
120
;
memc_pass
memcacheds
;
}
location
~
.
*
\
.
php
?
$
{
if
(
$
uri
~
/
ttlsa
/
)
{
set
$
ttlsa
_key
$
request_uri
;
srcache_fetch
GET
/
memc
$
ttlsa_key
;
srcache_store
PUT
/
memc
$
ttlsa_key
;
add
_header
X
-
Cached
-
From
$
srcache_fetch_status
;
add
_header
X
-
Cached
-
Store
$
srcache_store_status
;
}
include
fastcgi_params
;
fastcgi
_pass
127.0.0.1
:
10081
;
fastcgi_index
index
.
php
;
fastcgi_connect
_timeout
60
;
fastcgi_send
_timeout
180
;
fastcgi_read
_timeout
180
;
fastcgi_buffer
_size
128k
;
fastcgi
_buffers
4
256k
;
fastcgi_busy_buffers
_size
256k
;
fastcgi_temp_file_write
_size
256k
;
fastcgi_intercept_errors
on
;
fastcgi_param
SCRIPT
_FILENAME
$
document_root
$
fastcgi_script_name
;
}
}
|
7. 测试
7.1 第一次访问(404 Not found 创建缓存)
7.2 后续访问(直接从缓存中响应)
第一次访问创建缓存,后续访问在缓存过期时间内,直接从memcached返回,不需要再次经过php-fpm处理。大大提升动态页面访问速度。
《memc_nginx+srcache_nginx+memcached遇到的问题》
如需转载请注明出处:http://www.ttlsa.com/html/2460.html
成长的对话版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!