nginx+lua+redis构建高并发应用(转)
ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求。
url请求nginx服务器,然后lua查询redis,返回json数据。
备注:centos或者redhat系统请跳转到nginx + ngx_lua安装测试
一.安装lua
|
1
2
3
|
# apt-get install lua5.1
# apt-get install liblua5.1-dev
# apt-get install liblua5.1-socket2
|
二.安装nginx
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# apt-get install git-core
# git clone https://github.com/simpl/ngx_devel_kit.git
# git clone https://github.com/chaoslawful/lua-nginx-module.git
# git clone https://github.com/agentzh/redis2-nginx-module.git
# git clone https://github.com/agentzh/set-misc-nginx-module.git
# git clone https://github.com/agentzh/echo-nginx-module.git
# git clone https://github.com/catap/ngx_http_upstream_keepalive.git
# apt-get install libpcre3 libpcre3-dev libltdl-dev libssl-dev libjpeg62 libjpeg62-dev libpng12-0 libpng12-dev libxml2-dev libcurl4-openssl-dev libmcrypt-dev autoconf libxslt1-dev libgd2-noxpm-dev libgeoip-dev libperl-dev -y
# wget http://nginx.org/download/nginx-1.0.8.tar.gz
# tar zxvf nginx-1.0.8.tar.gz
# cd nginx-1.0.8
# ./configure --prefix=/usr/local/nginx --with-debug --with-http_addition_module \
--with-http_dav_module --with-http_flv_module --with-http_geoip_module \
--with-http_gzip_static_module --with-http_image_filter_module --with-http_perl_module \
--with-http_random_index_module --with-http_realip_module --with-http_secure_link_module \
--with-http_stub_status_module --with-http_ssl_module --with-http_sub_module \
--with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl \
--with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module \
--add-module=../ngx_devel_kit \
--add-module=../echo-nginx-module \
--add-module=../lua-nginx-module \
--add-module=../redis2-nginx-module \
--add-module=../ngx_http_upstream_keepalive \
--add-module=../set-misc-nginx-module
# make
# make install
|
三.安装lua-redis-parser
|
1
2
3
4
|
# 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
|
四.安装json
|
1
2
3
|
# 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/
|
五.安装redis-lua
|
1
2
|
# git clone https://github.com/nrk/redis-lua.git
# cp redis-lua/src/redis.lua /usr/share/lua/5.1/
|
六.配置
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
user www-data;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
error_log logs/error.log notice;
pid logs/nginx.pid;
worker_rlimit_nofile 60000;
events {
worker_connections 1024;
use epoll;
}
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.39:6379;
keepalive 1024 single; //定义连接池大小,当连接数达到此数后,后续的连接为短连接
}
server {
listen 80;
server_name 192.168.1.211;
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/fuck.lua;
}
}
}
|
# vim fuck.lua
|
1
2
3
4
5
6
7
8
9
10
11
12
|
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 -h 192.168.1.39
|
1
2
3
|
redis 192.168.1.39:6379> HMSET ttlsa www www.ttlsa.com mail mail.ttlsa.com
OK
|
# curl 'http://192.168.1.211/json?key=ttlsa'
|
1
|
["www","www.ttlsa.com","mail","mail.ttlsa.com"]
|
www.ttlsa.com
http://www.ttlsa.com/nginx/nginx-lua-redis/
nginx+lua+redis构建高并发应用(转)的更多相关文章
- Nginx+Lua+Redis构建高并发应用
一. 源文来自:http://www.ttlsa.com/nginx/nginx-lua-redis/ 二. 预览如下:
- Nginx与Redis解决高并发问题
原文链接:http://bbs.phpchina.com/forum.php?mod=viewthread&tid=229629 第一版产品采用的是Jquery,Nginx,PHP(CI框架) ...
- Lua + Redis 解决高并发
一.业务背景 优惠券业务主要提供用户领券和消券的功能:领取优惠券的动作由用户直接发起,由于资源有限,我们必须对用户的领取动作进行一些常规约束. 约束1(优惠券维度): 券的最大数量 max: 约束2( ...
- nginx +lua +redis 构建自动缓存系统
一. nginx环境搭建 第一步下载 LuaJIT-2.0.4.tar.gz http://luajit.org/download/LuaJIT-2.0.4.tar.gz安装 make &&a ...
- nginx+lua+redis高并发应用建设
ngx_lua将lua嵌nginx,让nginx运行lua脚本.高并发,非堵塞过程中的各种请求. url要求nginxserver,然后lua查询redis,返回json数据. 一.安装lua-ngi ...
- 【重要】Nginx模块Lua-Nginx-Module学习笔记(三)Nginx + Lua + Redis 已安装成功(非openresty 方式安装)
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis 一. 目标 使用Redis做分布式缓存:使用lua API来访问redis缓存:使用nginx向客户端提 ...
- nginx lua redis 访问频率限制(转)
1. 需求分析 Nginx来处理访问控制的方法有多种,实现的效果也有多种,访问IP段,访问内容限制,访问频率限制等. 用Nginx+Lua+Redis来做访问限制主要是考虑到高并发环境下快速访问控制的 ...
- Redis实现高并发分布式序列号
使用Redis实现高并发分布式序列号生成服务 序列号的构成 为建立良好的数据治理方案,作数据掌握.分析.统计.商业智能等用途,业务数据的编码制定通常都会遵循一定的规则,一般来讲,都会有自己的编码规则和 ...
- 基于nginx+lua+redis高性能api应用实践
基于nginx+lua+redis高性能api应用实践 前言 比较传统的服务端程序(PHP.FAST CGI等),大多都是通过每产生一个请求,都会有一个进程与之相对应,请求处理完毕后相关进程自动释放. ...
随机推荐
- STL的一些泛型算法
源地址:http://blog.csdn.net/byijie/article/details/8142859 从福州大学资料里摘下来的我现在能理解的泛型算法 algorithm min(a,b) 返 ...
- Linux shell中的I/O重定向相关(转)
1. 基本概念(这是理解后面的知识的前提,请务必理解) a. I/O重定向通常与 FD有关,shell的FD通常为10个,即 0-9: b. 常用FD有3个,为0(stdin,标准输入).1(std ...
- 2013 吉林通化邀请赛 Tutor 有点坑的水题
计算12个数的和的平均数.四舍五入,不能有后导0. 我的做法是,将答案算出后,乘以1000,然后看个位是否大于等于5,判断是否要进位…… #include<iostream> #inclu ...
- Swift供选链接
函数是运行特定任务的代码自包括块. 给定一个函数名称标识, 当运行其任务时就能够用这个标识来进行"调用". Swift的统一的功能语法足够灵活来表达不论什么东西,不管是甚至没有參数 ...
- cmake编译时遇到的问题解决
编译cmake首先须要gcc环境,能够运行 gcc --version命令看看. 假设没有,能够使用yum或从cd中进行安装,此处是在虚拟机中从cd中进行安装.将cd链接到虚拟机都会吧,此处略去,.. ...
- poj 2038 Team Rankings 枚举排列
//poj 2038 //sep9 #include <iostream> #include <algorithm> using namespace std; char s[1 ...
- hdu4223(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4223 由于n范围较小,完全可暴力... #include <cstdio> #includ ...
- 【Java&Android开源库代码剖析】のAndroid-Universal-Image-Loader-part1
做Android app开发的同学应该都听说过或者用过nostra13的Android-Universal-Image-Loader开源库,它在图片异步加载.缓存和显示等方面提供了强大灵活的框架.之前 ...
- Nginx 进程间通信
Linux下的IPC非常多,nginx的进程都是有亲缘关系的进程,对于他们的通信我们选择TCP socket进行通信. TCP socket 用来做进程通信的优点有, 1.socket是文件描 ...
- asp.net中用FileStream类实现下载文件功能,自定义下载路径,像IE下载一样
方法一: //这个值可以从配置文件读取,或者放个textbox让用户填 string path = "你的路径";FileStream outputStream = new Fil ...