ホームページ  >  記事  >  バックエンド開発  >  Nginx+Lua+Redis は同時実行性の高い Web アプリケーションを構築します

Nginx+Lua+Redis は同時実行性の高い Web アプリケーションを構築します

WBOY
WBOYオリジナル
2016-06-20 13:03:151181ブラウズ

この記事では、Nginx+Lua+Redis を使用して同時実行性の高い Web アプリケーションを構築する方法を紹介します。Curl は Nginx をリクエストし、Nginx は Lua を通じて Redis にクエリを実行し、json データを返します。

1. インストール

1. lua-redis-parser をインストールします

#git clone https://github.com/agentzh/lua-redis-parser.git
 #export LUA_INCLUDE_DIR=/usr/include/lua5.1
 #make CC=gcc
 #make install CC=gcc


2. 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/share/lua/5.1/


3. redis-lua
をインストールします

#git clone https://github.com/nrk/redis-lua.git
 #cp redis-lua/src/redis.lua /usr/share/lua/5.1/

2. 構成

#vi /etc/nginx/nginx.conf
http {
    include mime.types;
    default_type application/octet-stream;
    access_log logs/access.log;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 60;
    types_hash_max_size 2048;
    server_tokens off;
    lua_code_cache on;

    upstream redis_pool {
        server 192.168.1.105:6379;
        keepalive 1024 single; 
        //定义连接池大小,当连接数达到此数后,后续的连接为短连接
    }

    server {
        listen 80;
        server_name 192.168.1.104;

        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_redis.lua;
        }
    }
}

3. テスト
1. スクリプトを作成します
上記の構成で test_redis.lua スクリプトを記述します

 #vi test_redis.lua
    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


2. データの構築

#redis-cli -h 192.168.1.105 -p 6379
redis 192.168.1.105:6379>HMSET testnlr www www.scutephp.com mail mail.scutephp.com



3. テストを開始します

#curl 'http://192.168.1.104/json?key=testnlr'
["www", "www.scutephp.com", "mail", "mail.
scutephp.com"]


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。