Nginx+Lua访问Redis

14 篇文章 2 订阅
2 篇文章 0 订阅

一、代码

1.1 准备

1. 启动redis

        src/redis-server

2. 启动redis客户端,设置domain

        src/redis-cli

        set domain *.baidu.com


1.2 代码

local redis = require "resty.redis"

local cache = redis.new()

local ok, err = cache.connect(cache, '127.0.0.1', '6379')

cache:set_timeout(60000)

if not ok then
        ngx.say("failed to connect:", err)
        return
end

local res, err = cache:get("domain")
if not res then
        ngx.say("failed to get domain: ", err)
        return
end

if res == ngx.null then
        ngx.say("domain not found.")
        return
end

ngx.say("domain: ", res)


local ok, err = cache:close()

if not ok then
        ngx.say("failed to close:", err)
        return
end
1.3 Nginx配置文件

location /lua {
           content_by_lua_file conf/lua/hello.lua;
        }
二、运行结果

现在就可以访问那个Nginx机器了

curl 192.168.175.131/lua



参考资料:

        Nginx + Lua + Redis:http://blog.csdn.net/vboy1010/article/details/7892120

        Lua:Nginx Lua环境配置,第一个Nginx Lua代码:http://blog.csdn.net/guowenyan001/article/details/48250589

        Redis下载、安装、简单实用:http://blog.csdn.net/guowenyan001/article/details/49909233

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值