1.nginx的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."
ngx_http_memcached是核心模块,不需要另外编译。他只能get,不能set,对于set情况,需要通过后端处理。
location ^~ /memcache {
set $memcached_key $arg_key;
memcached_pass localhost:11211;
add_header X-mem $memcached_key;
error_page 403 404 502 504 = @fallback;
default_type text/html;
}
location @fallback{
rewrite ^/.*$ /setmemcache.php?key=$arg_key;
}
// setmemcache.php
header("x-mem:mis");
$key = isset($_GET['key']) ? $_GET['key'] : null ;
if($key !== null){
$value = "you make it by your work";
$m = new Memcache;
$m->connect('localhost',11211);
$m->set($key,$value);
}
测试如下:
2.nginx与HttpMemcModule
编译添加模块
--prefix=/usr/local/nginx81 --add-module=/usr/local/src/memc-nginx-module-0.14/ --add-module=/usr/local/src/echo-nginx-module-0.53/
make
cp objs/nginx /pathto/nginx
配置
location /amem {
set $memc_key $arg_key;
set $memc_cmd $arg_cmd;
set $memc_value $arg_val;
set $memc_exptime $arg_exptime;
memc_pass 127.0.0.1:11211;
add_header X-Memc-key $memc_key;
}
测试
set :http://localhost:81/amem?key=ab&cmd=set&val=1
get :http://localhost:81/amem?key=ab&cmd=get&val=1
delete :http://localhost:81/amem?key=ab&cmd=delete&val=1
flush_all :http://localhost:81/amem?key=ab&cmd=flush_all&val=1
version :http://localhost:81/amem?key=ab&cmd=version&val=1
incr:http://localhost:81/amem?key=ab&cmd=incr&val=1
decr:http://localhost:81/amem?key=ab&cmd=decr&val=1
stats:http://localhost:81/amem?key=ab&cmd=stats&val=1
可见,常见的方法,他都可以支持。