您的位置:首页 > 数据库 > Redis

安装openresty+redis+lua

2015-12-29 21:27 826 查看
1.安装luajit: http://luajit.org/download.html

    make & make install

2.安装openresty: http://openresty.org/

   注意安装条件:apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential

3.安装redis: sudo apt-get redis-server

4.安装lua-redis开发包: git clone https://github.com/agentzh/lua-resty-redis.git 

    加在 http 段里加上: lua_package_path "/your path/?.lua;;";

    在 server 段里,加入代码,如果不加此代码或者设置为 on 时,则修改lua后需要重启 Nginx才能生效:  lua_code_cache off;

    在 Nginx 配置文件中,加入一个Location:location /lua {content_by_lua_file /your path/test.lua;}

5.编写脚本文件test.lua

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

res, err = cache:set("dog", "an aniaml")

if not ok then

        ngx.say("failed to set dog: ", err)

        return

end

ngx.say("set result: ", res)

local res, err = cache:get("dog")

if not res then

        ngx.say("failed to get dog: ", err)

        return

end

if res == ngx.null then

        ngx.say("dog not found.")

        return

end

ngx.say("dog: ", res)

local ok, err = cache:close()

if not ok then

        ngx.say("failed to close:", err)

        return

end

6.使用一下方法可以根据后缀调用不同的lua文件

 location ~ ^/([-_a-zA-Z0-9/]+) {

            content_by_lua_file  /opt/openresty/p12/lua/$1.lua;

        }

7.如果浏览器访问出现下载界面,则MIMEtype不正确,在修改default_typ

    在location中加入:  default_type text/plain;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: