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_processes8;
worker_cpu_affinity0000000100000010000001000000100000010000001000000100000010000000;
error_log logs/error.log notice;
pid logs/nginx.pid;
worker_rlimit_nofile60000;
events{
worker_connections1024;
useepoll;
}
http{
include mime.types;
default_type application/octet-stream;
access_log logs/access.log;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout60;
types_hash_max_size2048;
server_tokens off;
lua_code_cache on;
upstreamredis_pool{
server192.168.1.39:6379;
keepalive1024single;//定义连接池大小,当连接数达到此数后,后续的连接为短连接
}
server{
listen80;
server_name192.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}
})
ifres.status==200then
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
|
redis192.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
相关推荐
nginx+lua+redis 集群 连接插件和脚本,原来插件是没有密码功能 故 修改了一下 k可以自己下载使用
nginx+lua+redis实现token验证。实现基本的token验证、反向代理转发内部服务,lua连接redis封装、lua域名解析封装、lua域名脚本等
nginx+lua+redis通过匹配客户端ip进行灰度发布
本文介绍如何用Nginx+Lua+Redis来构建高并发Web应用,Curl请求Nginx,Nginx通过Lua查询Redis,返回json数据。 一、安装1、安装lua-redis-parser 代码如下:#git clone ...
nginx+lua+redis黑名单加载,nginx+lua+redis黑名单加载。
最近有个需求是需要用nginx播放服务器的视频,考虑安全问题,需要在nginx加个lua去取redis的token进行验证,刚开始访问测试环境单机还挺好用,可是要访问生产的是集群环境,找了多了资料趟了数不尽的坑,最终形成这...
为处理高并发的请求,采用Nginx+Redis的非阻塞请求加内存数据库方式,采用Lua脚本处理简单业务,返回值为常用的Json
nginx+lua+redis.rar
提到高并发或者抗压力,有这种高qps经验的同学第一反应大都是Nginx+lua+Redis,网上也满天非那种高并发架构方案大都是这种,但是Nginx+lua来做接入层到底是怎么抗住压力的呢?本篇顺序:1、Nginx如何抗住的高并发,...
借助于Nginx的事件驱动模型和非阻塞IO,可以实现高性能的Web应用程 序。而且OpenResty提供了大量组件如Mysql、Redis、Memcached等等,使在Nginx上开发Web应用更方便更简单。目前在京东如实时价格、秒杀、动态服务、...
OpenResty lua redis 实现
本文给大家介绍的是Nginx利用Lua+Redis实现动态封禁IP的方法,下面话不多说了,来一起看看详细的介绍吧 二、架构 实现 IP 黑名单的功能有很多途径: 1、在操作系统层面,配置 iptables,拒绝指定 IP 的网络请求; 2...
PHP(全称:...总的来说,PHP作为一种成熟的Web开发语言,凭借其易用性、灵活性、丰富的库与框架支持、强大的社区生态以及持续的性能优化,成为了构建各类动态网站、Web应用及API服务的理想选择。
lua+redis构建高性能的服务器架构。
基于CentOS 7 web服务环境搭建(包含JDK+Nginx+Tomcat+Mysql+Redis)-附件资源
【Redis开发】lua脚本开发nginx 与 redis 模块 (Lua script development nginx and Redis modules) 文件列表: lua_files (0, 2017-05-21) lua_files\access_control.lua (1813, 2017-05-21) lua_files\app_redis_...