Build highly concurrent applications with nginx + Lua + redis

Source: Internet
Author: User
Tags nginx server install redis

Ngx_lua embeds Lua into nginx and enables nginx to execute Lua scripts to process various requests with high concurrency and non-blocking.

The URL requests the nginx server, Lua queries redis, and returns JSON data.


1. Install LUA-nginx-Module

See lnmlgc Architecture


2. Install redis2-nginx-module 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-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. Install 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. Install 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. Lua/usr/local/Lua/lib/

5. Install redis-Lua
# Git clone https:
// Github.com/nrk/redis-lua.git
# Cp redis-Lua/src/redis. Lua/usr/local/Lua/lib/

Vi. Configuration
... HTTP {... upstream redis_pool {server localhost: 6379; keepalive 1024 single; // defines the connection pool size. When the number of connections reaches this value, the subsequent connections are short connections} 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, which is placed in the same directory as 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

7. Test
# 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

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.