nginx redis模块使用

nginx redis模块使用

1. httpredis测试
# nginx.conf
user  root root;
worker_processes  2;
worker_rlimit_nofile 51200;
error_log  logs/error.log;
pid        logs/nginx.pid;

events {
        use epoll;
        worker_connections  50000;
}


http {
        access_log logs/access.log;
        server {
                listen 80 default;
                server_name 127.0.0.1;
                root html;
                index index.html index.htm;
                location / {
                        default_type text/html;
                        set $redis_key $uri;
                        redis_pass 127.0.0.1:6379;
                        error_page 404 = @fetch;
                        }
                location @fetch {
                        root html;
                        }
                }
}

# curl 127.0.0.1/a.js
// this is a js file
# curl 127.0.0.1
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>ngx_openresty/1.4.3.3</center>
</body>
</html>

# redis-cli
redis 127.0.0.1:6379> set '/a.js' '// fetch from redis'
# curl 127.0.0.1/a.js 
// fetch from redis

2. HttpRedis2Module测试
# nginx.conf
user  root root;
worker_processes  2;
worker_rlimit_nofile 51200;
error_log  logs/error.log;
pid        logs/nginx.pid;

events {
        use epoll;
        worker_connections  50000;
}


http {
        access_log logs/access.log;
        server {
                listen 80 default;
                server_name 127.0.0.1;
                root html;
                index index.html index.htm;
                location /get {
                        set_unescape_uri $key $arg_key;
                        redis2_query get $key;
                        redis2_pass 127.0.01:6379;
                        }
                location /set {
                        set_unescape_uri $key $arg_key;
                        set_unescape_uri $val $arg_val;
                        redis2_query set $key $val;
                        redis2_pass 127.0.01:6379;
                        }
                }
}

[root@VM_44_150_centos test]# curl -i 'localhost/set?key=/a.js&val=caoqing'
HTTP/1.1 200 OK
Server: ngx_openresty/1.4.3.3
Date: Thu, 14 Nov 2013 06:37:44 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive

+OK
[root@VM_44_150_centos test]# curl -i localhost/get?key=/a.js
HTTP/1.1 200 OK
Server: ngx_openresty/1.4.3.3
Date: Thu, 14 Nov 2013 06:37:45 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive

$7
caoqing

3. HttpLuaModule模块的lua-resty-redis测试
# nginx.conf
user  root root;
worker_processes  2;
worker_rlimit_nofile 51200;
error_log  logs/error.log;
pid        logs/nginx.pid;

events {
        use epoll;
        worker_connections  50000;
}


http {
        access_log logs/access.log;
        server {
                listen 80 default;
                server_name 127.0.0.1;
                root html;
                index index.html index.htm;
                location / {
                        content_by_lua_file '/root/conf/redis.lua';
                        }
                }
}
-- redis.lua
local cmd = tostring(ngx.var.arg_cmd)
local key = tostring(ngx.var.arg_key)
local val = tostring(ngx.var.arg_val)
local commands = {
        get="get",
        set="set"
}
cmd = commands[cmd]
if not cmd then
        ngx.say("command not found!")
        ngx.exit(400)
end

local redis = require("resty.redis")
local red = redis:new()

red:set_timeout(1000) -- 1 second

local ok,err = red:connect("127.0.0.1",6379)
if not ok then
        ngx.say("failed to connect: ",err)
        return
end

if cmd == "get" then
        if not key then ngx.exit(400) end
        local res,err = red:get(key)
        if not res then
                ngx.say("failed to get ",key,": ",err)
                return
        end
        ngx.say(res)
end

if cmd == "set" then
        if not (key and val) then ngx.exit(400) end
        local ok,err = red:set(key,val)
        if not ok then
                ngx.say("failed to set ",key,": ",err)
                return
        end
        ngx.say(ok)
end

[root@VM_44_150_centos test]# curl -i 'localhost/?cmd=set&key=my&val=caoqing'
HTTP/1.1 200 OK
Server: ngx_openresty/1.4.3.3
Date: Thu, 14 Nov 2013 06:43:54 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive

OK
[root@VM_44_150_centos test]# curl -i 'localhost/?cmd=get&key=my'
HTTP/1.1 200 OK
Server: ngx_openresty/1.4.3.3
Date: Thu, 14 Nov 2013 06:44:07 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive

caoqing


你可能感兴趣的:(nginx redis模块使用)