首先我們選擇使用OpenResty,其是由Nginx核心加很多第三方模塊組成捎琐,其最大的亮點是默認集成了Lua開發(fā)環(huán)境会涎,使得Nginx可以作為一個Web Server使用。借助于Nginx的事件驅(qū)動模型和非阻塞IO野哭,可以實現(xiàn)高性能的Web應(yīng)用程序在塔。而且OpenResty提供了大量組件如Mysql、Redis拨黔、Memcached等等蛔溃,使在Nginx上開發(fā)Web應(yīng)用更方便更簡單。目前在京東如實時價格篱蝇、秒殺贺待、動態(tài)服務(wù)、單品頁零截、列表頁等都在使用Nginx+Lua架構(gòu)麸塞,其他公司如淘寶、去哪兒網(wǎng)等涧衙。
安裝環(huán)境
安裝步驟可以參考http://openresty.org/#Installation哪工。
1、創(chuàng)建目錄/usr/servers弧哎,以后我們把所有軟件安裝在此目錄
mkdir-p/usr/servers
cd/usr/servers/
2雁比、安裝依賴(我的環(huán)境是ubuntu,可以使用如下命令安裝撤嫩,其他的可以參考openresty安裝步驟)
apt-getinstall libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl
3偎捎、下載ngx_openresty-1.7.7.2.tar.gz并解壓
wget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz
tar-xzvf ngx_openresty-1.7.7.2.tar.gz
ngx_openresty-1.7.7.2/bundle目錄里存放著nginx核心和很多第三方模塊,比如有我們需要的Lua和LuaJIT序攘。
3茴她、安裝LuaJIT
cd bundle/LuaJIT-2.1-20150120/
make clean&&make&&make install
ln-sf luajit-2.1.0-alpha/usr/local/bin/luajit
4、下載ngx_cache_purge模塊程奠,該模塊用于清理nginx緩存
cd/usr/servers/ngx_openresty-1.7.7.2/bundle
wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz
tar-xvf2.3.tar.gz
5丈牢、下載nginx_upstream_check_module模塊,該模塊用于ustream健康檢查
cd/usr/servers/ngx_openresty-1.7.7.2/bundle
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz
tar-xvf v0.3.0.tar.gz
6梦染、安裝ngx_openresty
cd/usr/servers/ngx_openresty-1.7.7.2
./configure--prefix=/usr/servers--with-http_realip_module--with-pcre--with-luajit--add-module=./bundle/ngx_cache_purge-2.3/--add-module=./bundle/nginx_upstream_check_module-0.3.0/-j2
make&&make install
--with***?????????????? ?安裝一些內(nèi)置/集成的模塊
--with-http_realip_module ?取用戶真實ip模塊
-with-pcre????????????? ?Perl兼容的達式模塊
--with-luajit????????????? 集成luajit模塊
--add-module??????????? 添加自定義的第三方模塊赡麦,如此次的ngx_che_purge
8朴皆、到/usr/servers目錄下
cd/usr/servers/
ll
會發(fā)現(xiàn)多出來了如下目錄,說明安裝成功
/usr/servers/luajit:luajit環(huán)境泛粹,luajit類似于java的jit遂铡,即即時編譯,lua是一種解釋語言晶姊,通過luajit可以即時編譯lua代碼到機器代碼扒接,得到很好的性能;
/usr/servers/lualib:要使用的lua庫们衙,里邊提供了一些默認的lua庫钾怔,如redis,json庫等蒙挑,也可以把一些自己開發(fā)的或第三方的放在這宗侦;
/usr/servers/nginx :安裝的nginx;
通過/usr/servers/nginx/sbin/nginx? -V 查看nginx版本和安裝的模塊
7忆蚀、啟動nginx
/usr/servers/nginx/sbin/nginx
接下來該配置nginx+lua開發(fā)環(huán)境了
配置環(huán)境
配置及Nginx HttpLuaModule文檔在可以查看http://wiki.nginx.org/HttpLuaModule矾利。
1、編輯nginx.conf配置文件
vim/usr/servers/nginx/conf/nginx.conf
2馋袜、在http部分添加如下配置
#lua模塊路徑男旗,多個之間”;”分隔,其中”;;”表示默認搜索路徑欣鳖,默認到/usr/servers/nginx下找
lua_package_path"/usr/servers/lualib/?.lua;;";#lua 模塊
lua_package_cpath"/usr/servers/lualib/?.so;;";#c模塊
3察皇、為了方便開發(fā)我們在/usr/servers/nginx/conf目錄下創(chuàng)建一個lua.conf
#lua.conf
server{
listen80;
server_name? _;
}
4、在nginx.conf中的http部分添加include lua.conf包含此文件片段
include lua.conf;
5泽台、測試是否正常
/usr/servers/nginx/sbin/nginx-t
如果顯示如下內(nèi)容說明配置成功
nginx: the configuration file /usr/servers/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/servers/nginx/conf/nginx.conf test is successful
HelloWorld
1什荣、在lua.conf中server部分添加如下配置
location/lua{
default_type'text/html';
content_by_lua'ngx.say("hello world")';
}
2、測試配置是否正確
/usr/servers/nginx/sbin/nginx-t
3怀酷、重啟nginx
/usr/servers/nginx/sbin/nginx-s reload
4溃睹、訪問如http://192.168.1.6/lua(自己的機器根據(jù)實際情況換ip),可以看到如下內(nèi)容
hello world
5胰坟、lua代碼文件
我們把lua代碼放在nginx配置中會隨著lua的代碼的增加導(dǎo)致配置文件太長不好維護,因此我們應(yīng)該把lua代碼移到外部文件中存儲泞辐。
vim/usr/servers/nginx/conf/lua/test.lua
#添加如下內(nèi)容
ngx.say("hello world");
然后lua.conf修改為
location/lua{
default_type'text/html';
content_by_lua_file conf/lua/test.lua;#相對于nginx安裝目錄
}
此處conf/lua/test.lua也可以使用絕對路徑/usr/servers/nginx/conf/lua/test.lua笔横。
6、lua_code_cache
默認情況下lua_code_cache? 是開啟的咐吼,即緩存lua代碼吹缔,即每次lua代碼變更必須reload nginx才生效,如果在開發(fā)階段可以通過lua_code_cache? off;關(guān)閉緩存锯茄,這樣調(diào)試時每次修改lua代碼不需要reload nginx厢塘;但是正式環(huán)境一定記得開啟緩存茶没。
location/lua{
default_type'text/html';
lua_code_cache off;
content_by_lua_file conf/lua/test.lua;
}
開啟后reload nginx會看到如下報警
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/servers/nginx/conf/lua.conf:8
7、錯誤日志
如果運行過程中出現(xiàn)錯誤晚碾,請不要忘記查看錯誤日志抓半。
tail-f/usr/servers/nginx/logs/error.log
到此我們的基本環(huán)境搭建完畢。
nginx+lua項目構(gòu)建
以后我們的nginx lua開發(fā)文件會越來越多格嘁,我們應(yīng)該把其項目化笛求,已方便開發(fā)。項目目錄結(jié)構(gòu)如下所示:
example
example.conf???? ---該項目的nginx 配置文件
lua????????????? ---我們自己的lua代碼
test.lua
lualib??????????? ---lua依賴庫/第三方依賴
*.lua
*.so
其中我們把lualib也放到項目中的好處就是以后部署的時候可以一起部署糕簿,防止有的服務(wù)器忘記復(fù)制依賴而造成缺少依賴的情況探入。
我們將項目放到到/usr/example目錄下。
/usr/servers/nginx/conf/nginx.conf配置文件如下(此處我們最小化了配置文件)
#user? nobody;
worker_processes2;
error_log? logs/error.log;
events{
worker_connections1024;
}
http{
include? ? ? mime.types;
default_type? text/html;
#lua模塊路徑懂诗,其中”;;”表示默認搜索路徑蜂嗽,默認到/usr/servers/nginx下找
lua_package_path"/usr/example/lualib/?.lua;;";#lua 模塊
lua_package_cpath"/usr/example/lualib/?.so;;";#c模塊
include/usr/example/example.conf;
}
通過絕對路徑包含我們的lua依賴庫和nginx項目配置文件。
/usr/example/example.conf配置文件如下
server{
listen80;
server_name? _;
location/lua{
default_type'text/html';
lua_code_cache off;
content_by_lua_file/usr/example/lua/test.lua;
}
}
lua文件我們使用絕對路徑/usr/example/lua/test.lua殃恒。
到此我們就可以把example扔svn上了植旧。