源码地址:https://github.com/Tinywan/Lua-Nginx-Redis

一、 目标

  使用Redis做分布式缓存;使用lua API来访问redis缓存;使用nginx向客户端提供服务,ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求。url请求nginx服务器,然后lua查询redis,返回json数据。

二、准备工作

  系统环境:Ubuntu 14.0 (64位)

  Redis服务安装:apt-get install redis-server

  安装Git:apt-get install git

  安装Lua:

# apt-get install lua5.
# apt-get install liblua5.-dev
# apt-get install liblua5.-socket2
# apt-get install -y lua5. liblua5.- liblua5.--dev

补充:安装模块:stream-lua-nginx-module 出现的错误信息:

make[1]: *** [objs/addon/src/ngx_stream_lua_socket_tcp.o] Error 1

解决办法:

apt-get install lua-socket

三、下载库

1、当前目录:/home/www 目录下面

2、下载ngx_devel_kit (NDK(nginx development kit)模块,是一个拓展nginx服务器核心功能的模块,第三方模块开发可以基于它来快速实现。

wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -zxvf v0.3.0.tar.gz

3、lua-nginx-module 下载。可在 Nginx 中嵌入 Lua 语言,让 Nginx 可以支持 Lua 强大的语法。

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.7.tar.gz
tar -zxvf v0.10.7.tar.gz

4、redis2-nginx-module 下载。是一个支持 Redis 2.0 协议的 Nginx upstream 模块,它可以让 Nginx 以非阻塞方式直接防问远方的 Redis 服务,同时支持 TCP 协议和 Unix Domain Socket 模式,并且可以启用强大的 Redis 连接池功能。

wget https://github.com/openresty/redis2-nginx-module/archive/v0.13.tar.gz
tar -zxvf v0.13.tar.gz

5、set-misc-nginx-module 下载。是标准的HttpRewriteModule指令的扩展,提供更多的功能,如URI转义与非转义、JSON引述,Hexadecimal、MD5、SHA1、Base32、Base64编码与解码、随机数等等

wget https://github.com/openresty/set-misc-nginx-module/archive/v0.31.tar.gz
tar -zxvf v0.31.tar.gz

6、echo-nginx-module 下载,是一个 Nginx 模块,提供直接在 Nginx 配置使用包括 "echo", "sleep", "time" 等指令。

wget https://github.com/openresty/echo-nginx-module/archive/v0.60.tar.gz
tar -zxvf v0.60.tar.gz

7、Nginx 下载

wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -zxvf nginx-1.10.3.tar.gz

四、安装

1、查看所有下载完的包

root@iZ236j3sofdZ:/home/www# ls
echo-nginx-module-0.60 lua-nginx-module-0.10.7 nginx-1.10.3 nginx-1.10.3.tar.gz
ngx_devel_kit-0.3.0 redis2-nginx-module-0.13 redis-lua-2.0.4 set-misc-nginx-module-0.31

2、Nginx 配置文件检测

cd nginx-1.10.3/
./configure --prefix=/usr/local/nginx --with-debug --with-http_addition_module \
--with-http_perl_module --with-http_realip_module --with-http_secure_link_module \
--with-http_stub_status_module --with-http_ssl_module --with-http_sub_module \
--with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl \
--add-module=../ngx_devel_kit-0.3.0 \
--add-module=../echo-nginx-module-0.60 \
--add-module=../lua-nginx-module-0.10.7 \
--add-module=../redis2-nginx-module-0.13 \
--add-module=../set-misc-nginx-module-0.31

【错误信息1】:

 ./configure: error: ngx_http_lua_module requires the Lua library

解决办法:

apt-get install lua5.1-0-dev

【错误信息3】:

./configure: error: the HTTP rewrite module requires the PCRE library.

解决办法:

apt-get install libreadline-dev libncurses5-dev libpcre3-dev \
libssl-dev perl make build-essential curl

【错误信息2】:

/usr/bin/ld: cannot find -lperl
collect2: error: ld returned 1 exit status

说明:/usr/bin/ld: cannot find -lxxx意思是编译过程找不到对应库文件。其中,-lxxx表示链接库文件 libxxx.so。了解更多cannot find -lxxx

解决办法:

apt-get install lperl-dev

五、编译

make
make install

编译过程出现这个问题:

make[1]: Leaving directory `/home/www/nginx-1.10.3' 什么意思

解决办法:

到你的源码目录内,先make clean  然后./config ...

3、安装lua-redis-parser,lua-resty-redis是openresty(1.9.15.1)的一个组件,简单来说,它提供一个lua语言版的redis API,使用socket(lua sock)和redis通信。

//下载源码包:
git clone https://github.com/openresty/lua-resty-redis.git

移动该源码包到/usr/local/nginx/lua/ 这里去

mv lua-resty-redis /usr/local/nginx/lua/

4、使用Redis的提示错误

    [error] 7094#0: *1 lua entry thread aborted: runtime error: /usr/local/nginx/conf/lua/test_redis_basic.lua:11: module 'resty.redis' not found:
no field package.preload['resty.redis']
no file '/home/www/lua-redis-parser-0.12/resty/redis.lua'
no file './resty/redis.lua'
no file '/usr/local/share/lua/5.1/resty/redis.lua'
no file '/usr/local/share/lua/5.1/resty/redis/init.lua'
no file '/usr/local/lib/lua/5.1/resty/redis.lua'
no file '/usr/local/lib/lua/5.1/resty/redis/init.lua'
no file '/usr/share/lua/5.1/resty/redis.lua'
no file '/usr/share/lua/5.1/resty/redis/init.lua'
no file './resty/redis.so'
no file '/usr/local/lib/lua/5.1/resty/redis.so'
no file '/usr/lib/x86_64-linux-gnu/lua/5.1/resty/redis.so'
no file '/usr/lib/lua/5.1/resty/redis.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './resty.so'
no file '/usr/local/lib/lua/5.1/resty.so'
no file '/usr/lib/x86_64-linux-gnu/lua/5.1/resty.so'
no file '/usr/lib/lua/5.1/resty.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:

提示以上错误的原因是Lua的库文件没有加载合适导致的。只需要下载官方的源码包,按照以下应用即可

 lua_package_path "/usr/local/nginx/lua/lua-resty-redis/lib/?.lua;;";

六、测试

1、配置nginx.conf(以下是部分代码)

# nginx.conf
http {
  .....
lua_package_path "/usr/local/nginx/lua/lua-resty-redis/lib/?.lua;;";
#gzip on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /lua {
echo "Hello Lua";
}
location /lua_test {
content_by_lua '
ngx.say("Hello Lua! Tinywan")
';
}
#content_by_lua_block
location =/content_by_lua_block {
default_type 'text/plain';
content_by_lua_block {
ngx.say('Hello : content_by_lua_block')
}
}
location /{
default_type 'text/html';
lua_code_cache off;
content_by_lua_file /usr/local/nginx/conf/lua/test_redis_basic.lua;
}
}
}

test_redis_basic.lua 添加以下内容:

local function close_redis(redis_instance)
if not redis_instance then
return
end
local ok,err = redis_instance:close();
if not ok then
ngx.say("close redis error : ",err);
end
end local redis = require("resty.redis");
--local redis = require "redis"
-- 创建一个redis对象实例。在失败,返回nil和描述错误的字符串的情况下
local redis_instance = redis:new();
--设置后续操作的超时(以毫秒为单位)保护,包括connect方法
redis_instance:set_timeout()
--建立连接
local ip = '127.0.0.1'
local port =
--尝试连接到redis服务器正在侦听的远程主机和端口
local ok,err = redis_instance:connect(ip,port)
if not ok then
ngx.say("connect redis error : ",err)
return close_redis(redis_instance);
end --Redis身份验证
--local auth,err = redis_instance:auth("");
--if not auth then
-- ngx.say("failed to authenticate : ",err)
--end --调用API进行处理
local resp,err = redis_instance:set("msg","hello world")
if not resp then
ngx.say("set msg error : ",err)
return close_redis(redis_instance)
end --调用API获取数据
local resp, err = redis_instance:get("msg")
if not resp then
ngx.say("get msg error : ", err)
return close_redis(redis_instance)
end --得到的数据为空处理
if resp == ngx.null then
resp = 'this is not redis_data' --比如默认值
end
ngx.say("msg : ", resp) close_redis(redis_instance)

我们通过curl 在shell脚本中测试以上配置文件

重启Nginx服务器:

root@iZ236j3sofdZ:/usr/local/nginx/conf# service nginx restart
* Stopping Nginx Server...
* Starting Nginx Server...
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/nginx/conf/nginx.conf:

警告:这个alert是因为objstore.conf中把lua_code_cache为off;若设置为off,nginx不缓存lua脚本,每次改变lua代码,不必reload nginx即可生效;这便于开发和测试。但禁用缓存对性能有影响,故正式环境下一定记得设置为on;

location /lua

root@iZ236j3sofdZ:/usr/local/nginx/lua# curl "http://localhost/lua"
Hello Lua

location /lua_test

root@iZ236j3sofdZ:/usr/local/nginx/lua# curl "http://localhost/lua_test"
Hello Lua! Tinywan

location /content_by_lua_block

root@iZ236j3sofdZ:/usr/local/nginx/lua# curl "http://localhost/content_by_lua_block"
Hello : content_by_lua_block

location /lua_redis_basic

root@iZ236j3sofdZ:/usr/local/nginx/lua# curl "http://localhost/lua_redis_basic"
msg : hello worTinywan

七、一些常见误区

1、redis2-nginx-module和lua-resty-redis

  redis2-nginx-module是一个openresty(1.9.15.1)自带的模块。它能够把请求转发给upstream(redis2_pass)。注意它和lua-resty-redis不同,lua-resty-redis是一个lua语言版的redis API,使用socket(lua sock)和redis通信。而redis2-nginx-module是把请求转发给别的upstream。

2、以下错误解决办法

checking for LuaJIT library in /usr/bin/luajit/lib and  (specified by the LUAJIT_LIB and LUAJIT_INC env, with -ldl) ... not found
checking for LuaJIT library in /usr/bin/luajit/lib and (specified by the LUAJIT_LIB and LUAJIT_INC env) ... not found
./configure: error: ngx_http_lua_module requires the Lua or LuaJIT library and LUAJIT_LIB is defined as
/usr/bin/luajit/lib and LUAJIT_INC (path for lua.h) , but we cannot find LuaJIT there.

下载安装:LuaJIT-2.0.4

更多版本下载地址:http://luajit.org/download.html

wget  http://luajit.org/download/LuaJIT-2.0..tar.gz
tar -zxvf LuaJIT-2.0..tar.gz
make && sudo make install

告诉nginx的构建系统在哪里可以找到LuaJIT 2.0:

export LUAJIT_INC=/usr/local/include/luajit-2.0

http://blog.csdn.net/qq_25551295/article/details/51744815

【重要】Nginx模块Lua-Nginx-Module学习笔记(三)Nginx + Lua + Redis 已安装成功(非openresty 方式安装)的更多相关文章

  1. 《基于Nginx的中间件架构》学习笔记---4.nginx编译参数详细介绍

    通过nginx -V查看编译时参数: 在nginx安装目录下,通过./configure --help,查看对应版本ngnix编译时支持的所有参数: Nginx编译参数详细介绍: --help 显示本 ...

  2. 《基于Nginx的中间件架构》学习笔记---2.nginx的优点以及nginx的安装

    [优势] 优势1:IO多路复用和epoll模型(详见总结知识) 优势2:轻量级(1.功能模块少:只保留了一些核心代码     2.代码模块化) 优势3:CPU亲和 这里的CPU亲和指的是:是一种把cp ...

  3. 《基于Nginx的中间件架构》学习笔记---3.nginx的目录分析

    一.目录分析 用yum的方式进行安装实质上装的都是一个个的rpm包,对于linux系统rpm包管理器,我们使用命令rpm -ql  服务名称 就可以列出我们已经安装的服务所对应安装的每一个文件所在的目 ...

  4. CAS学习笔记三:SpringBoot自动配置与手动配置过滤器方式集成CAS客户端

    本文目标 基于SpringBoot + Maven 分别使用自动配置与手动配置过滤器方式集成CAS客户端. 需要提前搭建 CAS 服务端,参考 https://www.cnblogs.com/hell ...

  5. nginx 学习笔记(2) nginx新手入门

    这篇手册简单介绍了nginx,并提供了一些可以操作的简单的工作.前提是nginx已经被安装到你的服务器上.如果没有安装,请阅读上篇:nginx 学习笔记(1) nginx安装.这篇手册主要内容:1. ...

  6. Requests:Python HTTP Module学习笔记(一)(转)

    Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...

  7. [Firefly引擎][学习笔记三][已完结]所需模块封装

    原地址:http://www.9miao.com/question-15-54671.html 学习笔记一传送门学习笔记二传送门 学习笔记三导读:        笔记三主要就是各个模块的封装了,这里贴 ...

  8. ES6学习笔记<三> 生成器函数与yield

    为什么要把这个内容拿出来单独做一篇学习笔记? 生成器函数比较重要,相对不是很容易理解,单独做一篇笔记详细聊一聊生成器函数. 标题为什么是生成器函数与yield? 生成器函数类似其他服务器端语音中的接口 ...

  9. angular学习笔记(三十一)-$location(2)

    之前已经介绍了$location服务的基本用法:angular学习笔记(三十一)-$location(1). 这篇是上一篇的进阶,介绍$location的配置,兼容各版本浏览器,等. *注意,这里介绍 ...

随机推荐

  1. [usaco]2013-jan Liars and Truth Tellers 真假奶牛

    Description 约翰有N头奶牛,有一部分奶牛是真话奶牛,它们只说真话,而剩下的是假话奶牛,只说假话.有一天,约翰从奶牛的闲谈中陆续得到了M句话,第i句话出自第Xi头奶牛,它会告诉约翰第Yi头是 ...

  2. 关于VS2005中C#代码用F12转到定义时,总是显示从元数据的问题

    元数据是:NET 程序集中的标记信息. 是在代码中选择了转到定义时候给定位的吧.因为没有找到源代码,VS通过反射读取元数据中的信息生成了那个. 解决方法: 1. 要把项目先添加到解决方案中. 2. 再 ...

  3. 软盘相关知识和通过BIOS中断访问软盘

    一. 软盘基础知识介绍 (1) 3.5英寸软盘 3.5英寸软盘分为上下两面,每面有80个磁道,每个磁道又分为18个扇区,每个扇区大小为512个字节. 软盘大小计算: 2面 * 80磁道 * 18扇区 ...

  4. windows 下查看进程开始运行的时间

    1. 打开运行 输入 msinfo32 然后在软件环境- 正在运行任务- 就能够看到运行开始的时间了.

  5. php父级目录文件包函问题

    问题: php子目录不能包函父目录中的文件. 环境: 网站根目录:/var/www/html/ PHP版本: 5.3.3 Apache版本:2.2 好了,创建三个文件: //文件路径:/var/www ...

  6. Java多线程(二) —— 深入剖析ThreadLocal

    对Java多线程中的ThreadLocal类还不是很了解,所以在此总结一下. 主要参考了http://www.cnblogs.com/dolphin0520/p/3920407.html 中的文章. ...

  7. C# QR二维码DEMO

    QR二维码 二维码的一种 相关类库 ThoughtWorks.QRCode 第三方类库 DEMO功能 Encode 生成二维码图片 Encoding 编码 Correction Level 等级 Ve ...

  8. Chrome神器Vimium快捷键学习记录

    今天下午折腾了一下Chrome下面的一个插件Vimium的使用,顿时发现该插件功能强大,能够满足减少鼠标的使用.至于为何要使用这个插件,源于我手腕上的伤一直没有好,使用鼠标的时候有轻微的疼痛.而且,由 ...

  9. Spring Boot 学习笔记1---初体验之3分钟启动你的Web应用

    前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...

  10. 【python】vscode python环境配置

    安装python插件:ext install python 配置flake8:pip install flake8 配置yapf:pip install yapf(在VScode中按Alt+Shift ...