Nginx 安装 Lua + Redis

1、安装 ngx_devel_kit

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

2、安装 lua-nginx-module

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

3、安装 lua 编译器

wget https://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar -zxvf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make install PREFIX=/usr/local/luajit


# 配置环境变量,不然无法安装

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

4、安装 nginx

CentOS 6.8 编译安装 Nginx 和 echo-nginx-module 模块_tom.ma的博客-CSDN博客

# 修改编译参数

./configure --prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--add-module=/usr/local/src/ngx_devel_kit-0.3.0 \
--add-module=/usr/local/src/lua-nginx-module-0.10.13

5、建立软链接

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

6、验证

vim /usr/local/nginx/conf/nginx.conf

# 在 server 标签里填加一个

location /hello {
    default_type 'text/plain';
    content_by_lua 'ngx.say("hello, lua")';
}

7、访问

curl http://192.168.2.103/hello

# 返回结果
hello, lua

8、Nginx 调用 Lua 指令

模块语法lua指令:

set_by_lua            # 设置nginx变量 可以实现复杂赋值逻辑

set_by_lua_file       # 设置nginx变量 可以实现复杂赋值逻辑

access_by_lua         # 请求访问阶段处理,用于访问控制

access_by_lua_file    # 请求访问阶段处理,用户访问控制

content_by_lua        # 内容处理器,处理接受和响应输出

content_by_lua_file   # 内容处理器,处理接受和响应输出

nginx lua api

ngx.var nginx         # 变量

ngx.req.get_headers   # 获取请求头

ngx.req.get_uri_args  # 获取url请求参数

ngx.redirect          # 重定向

ngx.print             # 输出响应内容体

ngx.say               # 输出响应内容体,但最后会输出一个换行符

ngnx.header           # 输出响应头

lua-resty-redis,是一个lua语言的redis API,使用socket(lua sock)和redis通信

git clone https://github.com/openresty/lua-resty-redis.git
mkdir /usr/local/nginx/lua
cp lua-resty-redis/lib/resty/redis.lua /usr/local/nginx/lua/

10、配置

http {

    # 引入 redis.lua 包
    lua_package_path "/usr/local/nginx/lua/redis.lua";

    ......
    
    location /redis{
        default_type 'text/html';
        lua_code_cache off;
        content_by_lua_file /usr/local/nginx/conf/conf.d/test-redis.lua;
     }
    ...........
}

 11、test-redis.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");

-- 创建一个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)

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值