Nginx+Lua+Redis構(gòu)建高并發(fā)Web應(yīng)用
本文介紹如何用Nginx+Lua+Redis來構(gòu)建高并發(fā)Web應(yīng)用,Curl請(qǐng)求Nginx,Nginx通過Lua查詢Redis,返回json數(shù)據(jù)。
一、安裝
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/
二、配置
#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;
//定義連接池大小,當(dāng)連接數(shù)達(dá)到此數(shù)后,后續(xù)的連接為短連接
}
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;
}
}
}
三、測(cè)試
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、構(gòu)造數(shù)據(jù)
#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、開始測(cè)試
#curl 'http://192.168.1.104/json?key=testnlr'
["www", "www.joyvc.cn", "mail", "mail.joyvc.cn"]
相關(guān)文章
在Nginx服務(wù)器中配置針對(duì)TCP的負(fù)載均衡的方法
這篇文章主要介紹了在Nginx服務(wù)器中配置針對(duì)TCP的負(fù)載均衡的方法,另外還介紹了TCP負(fù)載均衡的執(zhí)行原理,需要的朋友可以參考下2015-12-12
nginx實(shí)現(xiàn)一個(gè)域名配置多個(gè)laravel項(xiàng)目的方法示例
這篇文章主要介紹了nginx實(shí)現(xiàn)一個(gè)域名配置多個(gè)laravel項(xiàng)目的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
nginx 部署啟動(dòng)jar包用到的一些命令和流程操作
這篇文章主要介紹了nginx 部署啟動(dòng)jar包用到的一些命令和流程操作,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11

