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/

相關文章
相關標籤/搜索