1.模块说明
整合Nginx和Memcache需要用到Nginx的ngx_http_memcached_module模块,该模块说明:
The ngx_http_memcached_module module is used to obtain responses from a memcached server. The key is set in the $memcached_key variable. A response should be put in memcached in advance by means external to nginx.
2.工作流程:
后台的应用程序向memcached服务器中缓存需要缓存的页面(可以通过404重定向到后端去处理的),而nginx的memcached_module模块可以直接从memcached服务器中读取内容后输出,后续的请求不再经过应用程序处理,如php-fpm、django,大大的提升动态页面的速度,而且ngx_http_memcached_module可以操作任何兼用memcached协议的软件,如ttserver、membase等。
结构图如下:
memcached的key可以通过memcached_key变量来设置如$uri。如果命中,那么直接输出内容,没有命中就意味着nginx需要从应用程序请求页面。同时,我们还希望该应用程序将键值对写入到memcached,以便下一个请求可以直接从memcached获取。
如果键值不存在,nginx将报告notfound错误。最好的方法是使用error_page指定和location请求处理。同时包含"Bad Gateway"错误和"Gateway Timeout"错误,如:error_page 404 502 504 = @fallback;
注意:需要设置default_type,否则可能会显示不正常。
3.memcached_moudle模块各指令说明
官方文档:
3.1 memcached_bind
语法: memcached_bind address | off;
默认值: none
配置段: http, server, location
指定从哪个IP来连接memcached服务器
3.2 memcached_buffer_size
语法: memcached_buffer_size size;
默认值: 4k|8k;
配置段: http, server, location
读取从memcached服务器接收到响应的缓冲大小。尽快的将响应同步传给客户端。
3.3 memcached_connect_timeout
语法:memcached_connect_timeout time;
默认值:60s;
配置段:http, server, location
与memcached服务器建立连接的超时时间。通常不超过75s。
3.4 memcached_force_ranges
语法: memcached_force_ranges on | off;
默认值: memcached_force_ranges off;
配置段: http, server, location
This directive appeared in version 1.7.7. Enables byte-range support for both cached and uncached responses from the memcached server regardless of the “Accept-Ranges” field in these responses.
3.5 memcached_gzip_flag
语法:memcached_gzip_flag flag;
默认值:none
配置段:http, server, location
测试memcached服务器响应标志。如果设置了,将在响应头部添加了Content-Encoding:gzip。
3.6 memcached_next_upstream
语法: memcached_next_upstream error | timeout | invalid_response | not_found | off ...;
默认值: error timeout;
配置段: http, server, location
指定在哪些状态下请求将转发到另外的负载均衡服务器上,仅当memcached_pass有两个或两个以上时使用。
3.7 memcached_next_upstream_timeout
语法: memcached_next_upstream_timeout time;
默认值: memcached_next_upstream_timeout 0;
配置段: http, server, location
This directive appeared in version 1.7.5.Limits the time allowed to pass a request to the next server. The 0 value turns off this limitation.
3.8 memcached_next_upstream_tries
语法: memcached_next_upstream_tries number;
默认值: memcached_next_upstream_tries 0;
配置段: http, server, location
This directive appeared in version 1.7.5. Limits the number of possible tries for passing a request to the next server. The 0 value turns off this limitation.
3.9 memcached_pass
语法:memcached_pass address:port or socket;
默认值:none
配置段:location, if in location
指定memcached服务器地址。使用变量$memcached_key为key查询值,如果没有相应的值则返回error_page 404。
3.10 memcached_read_timeout
语法:memcached_read_timeout time;
默认值:60s;
配置段:http, server, location
定义从memcached服务器读取响应超时时间。
3.11 memcached_send_timeout
语法:memcached_send_timeout
默认值:60s
配置段:http, server, location
设置发送请求到memcached服务器的超时时间。
$memcached_key变量:memcached key的值
示例配置:
server {
location / {
set $memcached_key "$uri?$args";
memcached_pass host:11211;
error_page 404 502 504 = @fallback;
}
location @fallback {
proxy_pass
}
}
4.整合Nginx和memcached
配置环境:
前端主机:192.168.85.128
memcached服务器:192.168.85.156:11211
后端服务器:192.168.85.157
nginx.conf配置文件:
location / {
set $memcached_key $uri;
memcached_pass 192.168.85.156:11211;
default_type text/html;
error_page 404 502 504 = @fallback;
}
location @fallback {
proxy_pass 192.168.85.157;
}
流程:访问192.168.85.128/index.html时先set然后到192.168.85.156主机上找该页面,如果有缓存就立即返回,如果没有就会产生404错误,而产生404时就会定向到后端主机192.168.85.157上。第一次是没有缓存的,访问后memcached会缓存该条目,这时就可以连接memcached查看相关缓存信息来验证了;
测试:
后端主机192.168.85.157上创建几个页面
[root@localhost html]# cat index.html
Welcome to my nginx!
My nginx!
nginx.org.
Commercial support is available at
nginx.com.
[root@localhost html]# cat test.html
192.168.85.157
访问:
此时的memcached缓存信息:
stats
STAT pid 3480
STAT uptime 163994
STAT time 1458900834
STAT version 1.4.4
STAT pointer_size 32
STAT rusage_user 0.471928
STAT rusage_system 7.722825
STAT curr_connections 10
STAT total_connections 84
STAT connection_structures 12
STAT cmd_get 40
STAT cmd_set 4
STAT cmd_flush 0
STAT get_hits 3
STAT get_misses 37
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 1098
STAT bytes_written 34983
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 69
STAT curr_items 1
STAT total_items 4
STAT evictions 0
END
访问:
此时的memcached缓存信息:
stats
STAT pid 3480
STAT uptime 164388
STAT time 1458901228
STAT version 1.4.4
STAT pointer_size 32
STAT rusage_user 0.473927
STAT rusage_system 7.830809
STAT curr_connections 10
STAT total_connections 112
STAT connection_structures 12
STAT cmd_get 68
STAT cmd_set 4
STAT cmd_flush 0
STAT get_hits 3
STAT get_misses 65
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 1578
STAT bytes_written 39030
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 69
STAT curr_items 1
STAT total_items 4
STAT evictions 0
END
好吧,虽然cmd_get一直在增加,但是却一直无法缓存,难道是由于某些原因没有缓冲上?
附:如果觉得telnet的方式不方便查看缓存信息,可以安装memcached-master,memadmin或memcached-tool等方式来查看,使用方法在这里:
http://blog.chinaunix.net/uid-30212356-id-5682311.html
参考文章: