1.下载nginx、lua、redis

nginx下载地址 wget  http://nginx.org/download/nginx-1.8.0.tar.gz

lua下载地址 wget http://www.lua.org/ftp/lua-5.1.5.tar.gz

redis下载地址 wget https://github.com/antirez/redis/archive/2.8.23.tar.gz

2.安装lua、luajit、redis

安装lua

tar zxf lua-5.1.5.tar.gz

yum install -y readline readline-devel

cd lua-5.1.5

make linux && make install

然后输入lua,就会进入lua命令行

安装luajit

wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz

tar zxf LuaJIT-2.0.4.tar.gz

cd LuaJIT-2.0.4

make && make install
安装redis

tar zxf 2.8.23.tar.gz

cd  redis-2.8.23

make && make install

然后输入redis-server,打开redis服务

3.获取nginx依赖模块

mkdir -p /home/modules && cd /home/modules

git clone https://github.com/openresty/lua-nginx-module.git

git clone https://github.com/simpl/ngx_devel_kit.git

git clone https://github.com/openresty/redis2-nginx-module.git

git clone https://github.com/openresty/set-misc-nginx-module.git

git clone https://github.com/openresty/echo-nginx-module.git

4.安装nginx

tar zxf nginx-1.8.0.tar.gz

cd nginx-1.8.0

yum -y install pcre-devel openssl openssl-devel

./configure --prefix=/usr/local/nginx --add-module=/home/modules/ngx_devel_kit --add-module=/home/modules/lua-nginx-module --add-module=/home/modules/redis2-nginx-module --add-module=/home/modules/set-misc-nginx-module --add-module=/home/modules/echo-nginx-module
make && make install

5.安装lua-cjson

wget http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz

tar zxf lua-cjson-2.1.0.tar.gz

cd lua-cjson-2.1.0

修改Makefile文件 LUA_INCLUDE_DIR =   $(PREFIX)/include/luajit-2.0

make && make install

6.利用resty.redis模块写个demo

写个test.lua文件放在 /home/modules/lua-resty-redis/lib/,并赋予执行权限

 local redis = require "resty.redis"
local red = redis:new()
local json = require("cjson")
red:set_timeout() -- 1 sec
local ok, err = red:connect("127.0.0.1", )
local user = {} --table
user["name"] = "confused"
user["age"] =
local str = json.encode(user)
if not ok then
ngx.say("failed to connect: ", err)
return
end
red:init_pipeline() --利用管道操作
red:set("user", str)
red:get("user")
local results, err = red:commit_pipeline()
if not results then
ngx.say("failed to commit the pipelined requests: ", err)
return
end
for i, res in ipairs(results) do
if type(res) == "table" then
if res[] == false then
ngx.say("failed to run command ", i, ": ", res[])
else
ngx.say("ok ", i, ": ", res[])
end
else
ngx.say("ok ", i, ": ", res)
end
end

7.访问链接

配置nginx.conf

在http块中增加一个变量

lua_package_path "/home/modules/lua-resty-redis/lib/?.lua;;";

增加一个location

location /test {
            default_type "text/html";
            content_by_lua_file /home/modules/lua-resty-redis/lib/test.lua;
         }
重启nginx killall nginx && /usr/local/nginx/sbin/nginx

利用curl访问 curl localhost/test

你会得到ok 1: OK ok 2: {"name":"confused","age":24}

说明你成功了

结语

这只是自己的一个调研,准备用nginx+lua写通知接口(处理简单数据的读写,存储在redis),效率很高,可以看下openresty的性能评测,此篇作为入门篇,若有错误,望指正

参考链接

https://openresty.org/cn/

https://github.com/openresty/lua-resty-redis

nginx+lua+redis初体验的更多相关文章

  1. Nginx+Lua+Redis 对请求进行限制

    Nginx+Lua+Redis 对请求进行限制 一.概述 需求:所有访问/myapi/**的请求必须是POST请求,而且根据请求参数过滤不符合规则的非法请求(黑名单), 这些请求一律不转发到后端服务器 ...

  2. Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Discuz!

    Nginx+Lua+Redis整合实现高性能API接口 - 网站服务器 - LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区! - Powered by Disc ...

  3. nginx lua redis 访问频率限制(转)

    1. 需求分析 Nginx来处理访问控制的方法有多种,实现的效果也有多种,访问IP段,访问内容限制,访问频率限制等. 用Nginx+Lua+Redis来做访问限制主要是考虑到高并发环境下快速访问控制的 ...

  4. nginx+lua+redis构建高并发应用(转)

    nginx+lua+redis构建高并发应用 ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求. url请求nginx服务器,然后lua查询redis, ...

  5. 基于nginx+lua+redis高性能api应用实践

    基于nginx+lua+redis高性能api应用实践 前言 比较传统的服务端程序(PHP.FAST CGI等),大多都是通过每产生一个请求,都会有一个进程与之相对应,请求处理完毕后相关进程自动释放. ...

  6. nginx限制请求之三:Nginx+Lua+Redis 对请求进行限制

    相关文章: <高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <n ...

  7. nginx+lua+redis 处理APK包替换

    nginx + lua +redis 安装与使用入门: http://huoding.com/2012/08/31/156 nginx httpEchoModule : http://wiki.ngi ...

  8. nginx+lua+redis

    git clone --branch master https://github.com/openresty/lua-resty-redis.git yum install openssl opens ...

  9. nginx+lua+redis实现灰度发布_test

    nginx+lua+redis实现灰度发布: 灰度发布是指在黑白之间能够平滑过渡的一种方式 AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见, ...

随机推荐

  1. 关于程序路径Path.Combine以及AppDomain.CurrentDomain.BaseDirectory

    关于程序路径 LucenePath:@(System.Configuration.ConfigurationManager.AppSettings["LucenePath"])&l ...

  2. 【转】linux-系统启动流程详解

    第二十章.启动流程.模块管理与 Loader 最近升级日期:2009/09/14 1. Linux 的启动流程分析 1.1 启动流程一览 1.2 BIOS, boot loader 与 kernel ...

  3. Page.User.Identity.Name获取不到结果

    如果在IIS部署后Page.User.Identity.Name获取不到值,需要检查以下设置: 1.web.config设置<authentication mode="Windows& ...

  4. Sublime Text 介绍、用法、插件等

    个人常用插件: AlignmentBracket Highlighter 此插件能完成括号高亮匹对DocBlockrEmmentNodejsPackage ControlPrefixr   CSS3中 ...

  5. 黄聪:wordpress如何获取当前分类页面的ID、名称、别名(slug)

    <? global $wp_query; $cat_ID = get_query_var('cat'); $category = get_category($cat_ID); echo $cat ...

  6. 黄聪:WordPress 多站点建站教程(六):使用WP_Query、switch_to_blog函数实现获取子站点分类中的文章

    首先在你使用主题的funtions.php里面添加下代码: //根据时间显示最新的分类文章内容,每个站点显示一篇内容 //$blog_id 子站点ID //$catid 分类ID wp_reset_q ...

  7. 创建immutable类

    不可变对象(immutable objects) 那么什么是immutable objects?什么又是mutable Objects呢? immutable Objects就是那些一旦被创建,它们的 ...

  8. c++101rule

    组织策略0,不拘于小结缩进, 行的长度,命名,注释,空格,制表,1-4,高警告级别干净利落地进行编译,使用构建系统,使用版本控制,代码审查风格5,一个实体应该只有一个紧凑的职责. (依赖性管理,继承, ...

  9. RMAN_学习实验1_RMAN备份标准过程(案例)

    2014-12-23 Created By BaoXinjian

  10. DBA_Oracle Erp加密和解密账户密码(案例)

    2014-09-09 Created By BaoXinjian