Nginx + Lua + Redis (非openresty 方式安装)

源码地址: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:

yum install lua5.1
yum install liblua5.1-dev
yum install liblua5.1-socket2
yum install -y lua5.1 liblua5.1-0 liblua5.1-0-dev
yum install lua-socket
yum install install lua5.1-0-dev
yum install libreadline-dev libncurses5-dev libpcre3-dev \
libssl-dev perl make build-essential curl
yum install lperl-dev

三、下载库

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

五、编译

make
make install 

编译过程出现这个问题:

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

解决办法:

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

安装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
mv lua-resty-redis /usr/local/nginx/lua/

使用Redis的提示错误

094#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']

提示以上错误的原因是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(1000)
--建立连接
local ip = '127.0.0.1'
local port = 6379
--尝试连接到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)
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/nginx/conf/nginx.conf:69

警告:这个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

七、一些常见误区

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.4.tar.gz
tar -zxvf  LuaJIT-2.0.4.tar.gz
make && sudo make install
export LUAJIT_INC=/usr/local/include/luajit-2.0
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值