nginx+lua+redis(openresty)配置

 

nginx+lua+redis(openresty)配置

2014-07-18 11:10 2494人阅读 评论(1) 收藏 举报php

方案一:html

1.安装lua解释器nginx

wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz (http://luajit.org/download.html)
git

配置lua Lib和include/luajit-$version目录为环境变量github

2.下载ngx_devel_kit和lua-nginx-moduleredis

https://codeload.github.com/simpl/ngx_devel_kit/tar.gz/v0.2.19 ( https://github.com/simpl/ngx_devel_kit/tags)json

https://codeload.github.com/openresty/lua-nginx-module/tar.gz/v0.9.10 (https://github.com/openresty/lua-nginx-module/tags)
缓存

3.安装配置nginx服务器

http://nginx.org/download/nginx-1.4.7.tar.gz   (http://nginx.org/en/download.html)
ui

nginx configue时候,将ngx_devel_kit和lua-nginx-module两个编入到nginx中

而后make 和make install

4.安装成功以后

就能够在nginx.conf的location中嵌入lua代码,至关强大

例如

location /lua {
    set $test "hello, world.";
    content_by_lua '
        ngx.header.content_type = "text/plain";
        ngx.say(ngx.var.test);
    ';
}

5.使用redis

lua解析json格式的数据 http://www.kyne.com.au/~mark/software/lua-cjson.php(默认安装就行)

lua封装redis调用接口,https://github.com/openresty/lua-resty-redis,安装,(而且redis.lua上层目录要有resty,由于require “resty.redis”,看代码,负责会出现找不到库)

nginx.conf以下:

[html] view plaincopy

  1. <span style="font-size:14px;">lua_shared_dict config 1m;  

  2. lua_package_path "/opt/vendor/lua/?.lua;;";//指定redis.lua路径,;;指定默认路径  

  3. init_by_lua_file /usr/local/nginx/html/init.lua;  

  4. server {  

  5.    </span>  

[html] view plaincopy

  1. <span style="font-size:14px;">   lua_code_cache off;//避免lua代码缓存,更改代码执行失效  

  2.    location /lua {  

  3.       content_by_lua_file /usr/local/nginx/html/content.lua;  

  4.    }</span>  

[html] view plaincopy

  1. <span style="font-size:14px;">  

  2. }</span>  



代码init.lua:读取redis配置文件,而且设置共享变量

[html] view plaincopy

  1. <span style="font-size:14px;">local cjson = require "cjson";  

  2. local config = ngx.shared.config;  

  3. local file = io.open("/usr/local/nginx/html/config.json","r");  

  4. local content = cjson.decode(file:read("*all"));  

  5. file:close();  

  6. for name,value in pairs(content) do  

  7.    config:set(name,value);  

  8. end</span>  



代码content.lua 链接redis服务器,进行get和set操做

[html] view plaincopy

  1. <span style="font-size:14px;">ngx.header.content_type = "text/plain";  

  2. local redis = require "resty.redis";  

  3. local config = ngx.shared.config;  

  4. local instance = redis:new();  

  5. local host = config:get("host");  

  6. local port = config:get("port");  

  7. local ok,err = instance:connect(host,port);  

  8. if not ok then  

  9.    ngx.log(ngx.ERR,err);  

  10.    ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE);  

  11. end  

  12. instance:set("name","gao");  

  13. local name = instance:get("name");  

  14. instance:close()  

  15. ngx.say("name:",name);</span>  



方案二:直接安装openresty nginx,里面已经配置好各类模块,上来直接能够用。


参考文献:

http://huoding.com/2012/08/31/156

https://github.com/openresty/lua-resty-redis

http://wiki.nginx.org/HttpLuaModule

http://openresty.org/

相关文章
相关标签/搜索