Nginx+lua+redis Constructing high Concurrent Web application _nginx

Source: Internet
Author: User
Tags curl json lua redis git clone

This article describes how to build high concurrent Web applications with Nginx+lua+redis, curl requests Nginx,nginx to return JSON data through Lua query Redis.

First, installation
1. Installation Lua-redis-parser

Copy Code code as follows:

#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. Install JSON
Copy Code code as follows:

#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. Installation Redis-lua
Copy Code code as follows:

#git Clone Https://github.com/nrk/redis-lua.git
#cp REDIS-LUA/SRC/REDIS.LUA/USR/SHARE/LUA/5.1/

Second, the configuration

Copy Code code as follows:

#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;
Defines the connection pool size, and when the number of connections reaches this number, subsequent connections are short connections
}

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;
}
}
}

Third, testing
1, write the script
Write the Test_redis.lua script in the configuration above

Copy Code code as follows:

#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 = = Then
Reply = parser.parse_reply (res.body)
Value = Json.encode (Reply)
Ngx.say (value)
A = Json.decode (value)
Ngx.say (A[2])
End

2, the construction of data
Copy Code code as follows:

#redis-cli-h 192.168.1.105-p 6379
Redis 192.168.1.105:6379>hmset testnlr www www.joyvc.cn mail mail.joyvc.cn

3. Start testing
Copy Code code as follows:

#curl ' HTTP://192.168.1.104/JSON?KEY=TESTNLR '
["www", "www.joyvc.cn", "Mail", "mail.joyvc.cn"]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.