nginx+lua+redis高同時応用建設
2832 ワード
ngx_luaはluaをnginxに埋め込み、nginxにluaスクリプトを実行させます.高同時、非渋滞中の各種リクエスト.
urlはnginxserverを要求し、luaはredisをクエリーし、jsonデータを返します.
一.lua-nginx-moduleのインストール
「LNMLGC」アーキテクチャを参照
二.redis 2-nginx-moduleモジュールのインストール
get https://github.com/openresty/echo-nginx-module get https://github.com/openresty/redis2-nginx-module get https://github.com/agentzh/set-misc-nginx-module.git #
./configure --prefix=/usr/local/nginx \--with-debug \--with-http_stub_status_module \--with-http_ssl_module \--with-pcre=../pcre-8.21 \--add-module=../ngx_devel_kit-0.2.19 \--add-module=../lua-nginx-module-0.9.8 \--add-module=../echo-nginx-module \--add-module=../redis2-nginx-module \--add-module=../set-misc-nginx-module
♪make♪make install 3.lua-redis-parser#git clone https://githubをインストールします.com/agentzh/lua-redis-parser.git # export LUA_INCLUDE_DIR=/usr/local/include/luajit-2.0#make CC=gcc#make install CC=gcc 4.json#wget http://filesをインストールします.luaforge.net/releases/json/json/0.9.50/json4lua-0.9.50.zip # unzip json4lua-0.9.50.zip # cp json4lua-0.9.50/json/json.lua/usr/local/lua/lib/5.redis-lua#git clone https://githubをインストールします.com/nrk/redis-lua.git # cp redis-lua/src/redis.lua/usr/local/lua/lib/六.コンフィギュレーション
# vi test.lua、nginxに置く.conf同フォルダ下
七.テスト
# redis-cli
127.0.0.1:6379> HMSET ttlsa www www.ttlsa.com mail mail.ttlsa.com
OK
127.0.0.1:6379> hgetall ttlsa
1) "www"
2) "www.ttlsa.com"
3) "mail"
4) "mail.ttlsa.com"
# curl http://localhost/json?
key=ttlsa
["www","www.ttlsa.com","mail","mail.ttlsa.com"]
www.ttlsa.com
urlはnginxserverを要求し、luaはredisをクエリーし、jsonデータを返します.
一.lua-nginx-moduleのインストール
「LNMLGC」アーキテクチャを参照
二.redis 2-nginx-moduleモジュールのインストール
get https://github.com/openresty/echo-nginx-module get https://github.com/openresty/redis2-nginx-module get https://github.com/agentzh/set-misc-nginx-module.git #
./configure --prefix=/usr/local/nginx \--with-debug \--with-http_stub_status_module \--with-http_ssl_module \--with-pcre=../pcre-8.21 \--add-module=../ngx_devel_kit-0.2.19 \--add-module=../lua-nginx-module-0.9.8 \--add-module=../echo-nginx-module \--add-module=../redis2-nginx-module \--add-module=../set-misc-nginx-module
♪make♪make install 3.lua-redis-parser#git clone https://githubをインストールします.com/agentzh/lua-redis-parser.git # export LUA_INCLUDE_DIR=/usr/local/include/luajit-2.0#make CC=gcc#make install CC=gcc 4.json#wget http://filesをインストールします.luaforge.net/releases/json/json/0.9.50/json4lua-0.9.50.zip # unzip json4lua-0.9.50.zip # cp json4lua-0.9.50/json/json.lua/usr/local/lua/lib/5.redis-lua#git clone https://githubをインストールします.com/nrk/redis-lua.git # cp redis-lua/src/redis.lua/usr/local/lua/lib/六.コンフィギュレーション
...
http {
...
upstream redis_pool {
server localhost:6379;
keepalive 1024 single;
// , 。
}
server {
...
location /get_redis{
#internal;
set_unescape_uri $key $arg_key;
redis2_query hgetall $key;
redis2_pass redis_pool;
}
location /json {
content_by_lua_file conf/test.lua;
}
}
}
# vi test.lua、nginxに置く.conf同フォルダ下
local json = require("json")
local parser = require("redis.parser")
local res = ngx.location.capture("/get_redis",{
args = { key = ngx.var.arg_key }
})
if res.status == 200 then
reply = parser.parse_reply(res.body)
value = json.encode(reply)
ngx.say(value)
a = json.decode(value)
ngx.say(a[2])
end
七.テスト
# redis-cli
127.0.0.1:6379> HMSET ttlsa www www.ttlsa.com mail mail.ttlsa.com
OK
127.0.0.1:6379> hgetall ttlsa
1) "www"
2) "www.ttlsa.com"
3) "mail"
4) "mail.ttlsa.com"
# curl http://localhost/json?
key=ttlsa
["www","www.ttlsa.com","mail","mail.ttlsa.com"]
www.ttlsa.com