今天安裝lua試試,這個從開始裝的,發現一篇文字,字數雖少,但是卻講的很清楚。如下:
最先將Nginx,Lua組合到一起的是OpenResty,它有一個ngx_lua模塊,將Lua嵌入到了Nginx里面;隨后Tengine也包含了ngx_lua模塊。至於二者的區別:OpenResty是Nginx的Bundle;而Tengine則是Nginx的Fork。值得一提的是,OpenResty和Tengine均是國人自己創建的項目,前者主要由春哥和曉哲開發,后者主要由淘寶打理。
至於OpenResty和Tengine孰優孰劣,留給大家自己判斷,如下資料可供參考:
推薦看看春哥在Tech-Club上關於『由Lua粘合的Nginx生態環境』的演講實錄,有料!
安裝
需要最新版的Nginx,LuaJIT,ngx_devel_kit,ngx_lua等安裝文件。
安裝Lua或者LuaJIT都是可以的,但是出於效率的考慮,推薦安裝LuaJIT。
shell> wget http://luajit.org/download/LuaJIT-<VERSION>.tar.gz shell> tar zxvf LuaJIT-<VERSION>.tar.gz shell> cd LuaJIT-<VERSION> shell> make shell> make install
因為安裝在缺省路徑,所以LuaJIT對應的lib,include均在/usr/local目錄里。
shell> export LUAJIT_LIB=/usr/local/lib shell> export LUAJIT_INC=/usr/local/include/luajit-<VERSION>
下面就可以編譯Nginx了:
shell> wget http://nginx.org/download/nginx-<VERSION>.tar.gz shell> tar zxvf nginx-<VERSION>.tar.gz shell> cd nginx-<VERSION> shell> ./configure --add-module=/path/to/ngx_lua \ --add-module=/path/to/ngx_devel_kit shell> make shell> make install
試着啟動一下Nginx看看,如果你運氣不好的話,可能會遇到如下錯誤:
cannot open shared object file: No such file or directory
這是神馬情況?可以用ldd命令來看看:
shell> ldd /path/to/nginx libluajit-<VERSION>.so => not found
此類問題通常使用ldconfig命令就能解決:
shell> echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf shell> ldconfig
再試着啟動Nginx看看,應該就OK了。
應用
我們先用一個簡單的程序來暖暖場:把下面的代碼加入到Nginx的配置文件nginx.conf,並重啟Nginx,然后瀏覽,就能看到效果了。
location /lua { set $test "hello, world."; content_by_lua ' ngx.header.content_type = "text/plain"; ngx.say(ngx.var.test); '; }
在深入學習ngx_lua之前,建議大家仔細閱讀一遍春哥寫的Nginx教程。
這里我就說關鍵的:Nginx配置文件所使用的語言本質上是『聲明性的』,而非『過程性的』。Nginx處理請求的時候,指令的執行並不是由定義指令時的物理順序來決定的,而是取決於指令所屬的階段,Nginx常用的階段按先后順序有:rewrite階段,access階段,content階段等等。演示代碼中的set指令屬於rewrite階段,content_by_lua指令屬於content階段,如果試着把兩條指令的順序交換一下,會發現程序依然能夠正常運行。
下面我們嘗試結合Redis寫個更實戰一點的例子。
首先,我們需要創建一個Redis配置文件config.json,內容如下:
{ "host": "<HOST>", "port": "<PORT>" }
然后,我們創建一個解析配置文件的腳本init.lua,其中用到了Lua CJSON模塊:
local cjson = require "cjson"; local config = ngx.shared.config; local file = io.open("config.json", "r"); local content = cjson.decode(file:read("*all")); file:close(); for name, value in pairs(content) do config:set(name, value); end
說明:代碼里用到了共享內存,這樣就不必每次請求都解析一遍配置文件了。
接着,我們創建一個渲染內容的腳本content.lua,用到了Resty Redis模塊:
ngx.header.content_type = "text/plain"; local redis = require "resty.redis"; local config = ngx.shared.config; local instance = redis:new(); local host = config:get("host"); local port = config:get("port"); local ok, err = instance:connect(host, port); if not ok then ngx.log(ngx.ERR, err); ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE); end instance:set("name", "laowang"); local name = instance:get("name") instance:close(); ngx.say("name: ", name);
說明:建議把Resty Redis模塊放到vendor目錄下,稍后在Nginx中統一設置。
最后,我們需要在Nginx配置文件里設置一下:
lua_shared_dict config 1m; lua_package_path "/path/to/vendor/?.lua;;"; init_by_lua_file /path/to/init.lua; server { lua_code_cache off; location /lua { content_by_lua_file /path/to/content.lua; } ... }
說明:為了方便調試,我關閉了lua_code_cache,如果是生產環境,應該開啟它。
…
我最近參與的一個項目,提供了一些用於Web輪詢的接口,都是用Nginx+Lua實現的,雖然總共只有十幾台服務器,但是每天可以提供幾十億次的請求量,賊拉拉的強。
最后,讓我引用某位屌絲的語錄做結束語吧:Lua,未婚男性程序員的最愛。
摘自:http://huoding.com/2012/08/31/156
另外也可以直接安裝 ngx_OpenResty 地址http://openresty.org/#OpenResty 其實他就是nginx,里面還有很多有用的Nginx模塊,還有很多有用Lua庫的軟件集合。這個需要提前裝下依賴庫。具體安裝方式很簡單,上面地址上也有。