nginx + lua環境搭建

Lua 是一個小巧的腳本語言。該語言的設計目的是爲了嵌入應用程序中,從而爲應用程序提供靈活的擴展和定製功能。
Lua腳本可以很容易的被C/C++代碼調用,也可以反過來調用C/C++的函數,這使得Lua在應用程序中可以被廣泛應用。
不僅僅作爲擴展腳本,也可以作爲普通的配置文件,代替XML,Ini等文件格式,並且更容易理解和維護。


直接使用官方的nginx + lua_nginx_module 比較麻煩,本文介紹兩種nginx+lua的環境搭建
系統環境:CentOS release 6.6 x64

CentOS上面需要先安裝如下依賴:yum install openssl openssl-devel curl curl-devel zlib zlib-devel pcre pcre-devel lua lua-devel

CentOS6.6 默認安裝的lua版本是:Lua 5.1.4


方法一:直接使用tengine

安裝步驟如下:

wget http://tengine.taobao.org/download/tengine-2.1.2.tar.gz

tar -xvf tengine-2.1.2.tar.gz

cd tengine-2.1.2

./configure --prefix=/usr/local/tengine --with-http_lua_module

make

make install 

這樣,就安裝好了。

然後,vim /usr/local/tengine/conf/nginx.conf

加入如下代碼

location /lua {
       default_type text/plain;
       content_by_lua 'ngx.say("hello world lua")';
}
最後,啓動tengine

訪問http://127.0.0.1/lua    

頁面出現hello world lua ,表示安裝成功


方法二:使用OpenResty

OpenResty 是一個基於 Nginx 與 Lua 的高性能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數的依賴項。用於方便地搭建能夠處理超高併發、擴展性極高的動態 Web 應用、Web 服務和動態網關。

官方網站:https://openresty.org/en/

安裝文檔:https://openresty.org/en/installation.html

安裝步驟:

wget https://openresty.org/download/ngx_openresty-1.9.7.2.tar.gz

tar -xvf ngx_openresty-1.9.7.2.tar.gz

cd ngx_openresty-1.9.7.2

./configure --prefix=/usr/local/openresty  (默認集成了lua環境)

gmake

gmake install

這樣,就安裝好了。

然後 vim /usr/local/openresty/nginx/conf/nginx.conf

加入如下代碼:

location /lua {
       default_type text/plain;
       content_by_lua 'ngx.say("hello world lua")';
}
最後,執行 /usr/local/openresty/nginx/sbin/nginx 啓動nginx

訪問http://127.0.0.1/lua    

頁面出現hello world lua ,表示安裝成功


我們把lua代碼放在nginx配置中會隨着lua的代碼的增加導致配置文件太長不好維護,因此我們應該把lua代碼移到外部文件中存儲。

nginx + lua簡單使用示例

1:vim /lua/test.lua

內容如下:

ngx.say("hello world");

修改nginx.conf,添加如下內容

location /luafile {  
    default_type 'text/html';  
    content_by_lua_file /lua/test.lua;
}
然後訪問/luafile 即可看到效果

默認情況下lua_code_cache  是開啓的,即緩存lua代碼,即每次lua代碼變更必須reload nginx才生效,如果在開發階段可以通過lua_code_cache  off;關閉緩存,這樣調試時每次修改lua代碼不需要reload nginx;

開啓後reload nginx會看到如下報警
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:54

下面是一個使用lua獲取http請求數據的例子 vim /lua/info.lua

--請求頭  
local headers = ngx.req.get_headers()
ngx.say("<p>請求頭信息</p>")
ngx.say("<p>Host : ", headers["Host"], "</p>")
ngx.say("<p>user-agent : ", headers["user-agent"], "</p>")
ngx.say("<p>user-agent2 : ", headers.user_agent, "</p>")
--GET請求參數
local uri_args = ngx.req.get_uri_args()
ngx.say("<p>GET請求參數</p>")
ngx.say("<p>id : ", uri_args["id"], "</p>")
ngx.say("<p>type : ", uri_args["type"], "</p>")
--POST請求參數  
ngx.req.read_body()
local post_args = ngx.req.get_post_args()
ngx.say("<p>POST請求參數</p>")
ngx.say("<p>username : ", post_args["username"], "</p>")
ngx.say("<p>password : ", post_args["password"], "</p>")
--獲取其他請求信息
ngx.say("<p>獲取其他請求信息</p>")
ngx.say("<p>request_uri : ", ngx.var.request_uri, "</p>")
ngx.say("<p>請求的http協議版本 : ", ngx.req.http_version(), "</p>")
ngx.say("<p>請求方法  : ", ngx.req.get_method(), "</p>")
ngx.say("<p>原始的請求頭內容 : ",  ngx.req.raw_header(), "</p>")
ngx.say("<p>請求的body內容體 : ", ngx.req.get_body_data(), "</p>")

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章