error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
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;
}
}
}
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:14:57 GMT
Content-Type: application/x-javascript
Content-Length: 23
Last-Modified: Wed, 20 Feb 2013 19:08:50 GMT
Connection: keep-alive
Accept-Ranges: bytes
// this a test js file
redis 127.0.0.1:6379> set '/common.js' '// fetch from redis'
OK
redis 127.0.0.1:6379> keys *
1) "/common.js"
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:23:13 GMT
Content-Type: application/x-javascript
Content-Length: 19
Connection: keep-alive
// fetch from redis
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
location /get {
set_unescape_uri $key $arg_key;
redis2_query get $key;
redis2_pass 127.0.0.1:6379;
}
location /set {
set_unescape_uri $key $arg_key;
set_unescape_uri $val $arg_val;
redis2_query set $key $val;
redis2_pass 127.0.0.1:6379;
}
}
}
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:49:57 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
$19
// fetch from redis
[root@vm5 conf]# curl -i 'localhost/set?key=/common.js&val=set by nginx'
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:50:41 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
+OK
[root@vm5 conf]# curl -i localhost/get?key=/common.js
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 19:50:45 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
$12
set by nginx
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
location / {
content_by_lua_file conf/lua/redis.lua;
}
}
}
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
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 20:20:07 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
OK
[root@vm5 conf]# curl -i 'localhost/?cmd=get&key=/common.js'
HTTP/1.1 200 OK
Server: ngx_openresty/1.2.4.14
Date: Wed, 20 Feb 2013 20:20:15 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
set by lua_resty_redis