让Ningx支持Lua

nginx的强大,lua的高性能,真是一个不错的组合,合到一块儿就无敌了,呵呵。nginx


下面开始配置nginx,使其支持lua,是经过一个nginx模块实现的,git

模块地址:https://github.com/chaoslawful/lua-nginx-modulegithub

下载连接:https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.10.tar.gzbash


一、下载源码、解压缩app

a、nginx源码:curl

[root@localhost ~]# wget http://nginx.org/download/nginx-1.4.2.tar.gz
[root@localhost ~]# tar zxvf nginx-1.4.2.tar.gz

b、lua模块ide

[root@localhost ~]# wget -O lua-nginx-module-0.8.10.tar.gz https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.10.tar.gz
[root@localhost ~]# tar zxvf lua-nginx-module-0.8.10.tar.gz

c、luajit(lua即时编译器)
工具

[root@localhost ~]# wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz
[root@localhost ~]# tar zxvf LuaJIT-2.0.2.tar.gz

d、ngx_devel_kit(nginx开发工具包)性能

[root@localhost ~]# wget -O ngx_devel_kit-0.2.18.tar.gz https://github.com/simpl/ngx_devel_kit/archive/v0.2.18.tar.gz
[root@localhost ~]# tar zxvf ngx_devel_kit-0.2.18.tar.gz


二、安装luajit开发工具

[root@localhost ~]# make
[root@localhost ~]# make install


三、安装nginx

方法a、使用luajit即时编译器

[root@localhost nginx-1.4.1]# export LUAJIT_LIB=/usr/local/lib
[root@localhost nginx-1.4.1]# export LUAJIT_INC=/usr/local/include/luajit-2.0
[root@localhost nginx-1.4.1]# ./configure \
--prefix=/usr/local/nginx-1.4.1 \
--with-http_stub_status_module \
--add-module=../lua-nginx-module-0.8.9 \
--add-module=../ngx_devel_kit-0.2.18
[root@localhost nginx-1.4.1]# make -j 4
[root@localhost nginx-1.4.1]# make install
[root@localhost ~]# echo '/usr/local/lib' >> /etc/ld.so.conf.d/lua.conf
[root@localhost ~]# ldconfig

方法b、使用lua编译器

注意:使用lua编译器的话,须要先安装lua执行环境,通常的系统都是预装好的,若是没有预装 好的话,能够用yum安装,以下:

[root@localhost ~]# yum install lua lua-devel

安装nginx:

[root@localhost nginx-1.4.1]# export LUA_LIB=/usr/lib64
[root@localhost nginx-1.4.1]# export LUA_INC=/usr/include
[root@localhost nginx-1.4.1]# ./configure \
--prefix=/usr/local/nginx-1.4.1 \
--with-http_stub_status_module \
--add-module=../lua-nginx-module-0.8.9 \
--add-module=../ngx_devel_kit-0.2.18
[root@localhost nginx-1.4.1]# make -j 4
[root@localhost nginx-1.4.1]# make install

注意:让nginx支持lua,有两种方法,一是使用luajit即时编译器,二是使用lua编译器。推荐使用luajit,由于效率高。其中ngx_devel_kit的做用有2个,一是开发用的,二是能够在错误日志中记录nginx处理阶段信息(rewrite phase,access phase,content phase),须要将错误日志级别调高,调试时能够设置成debug。


四、验证安装

使用lua编译器时显示以下:

[root@localhost ~]# lsof -p 3359 | grep -i lua
nginx  3359 root  mem  REG 8,3  183920 394551 /usr/lib64/liblua-5.1.so

使用luajit即时编译器时显示以下:

[root@localhost ~]# lsof -p 13177 | grep -i lua
nginx 13177 root mem REG 8,3 452024 405089 /usr/local/lib/libluajit-5.1.so.2.0.2


五、验证配置指令和输出

修改nginx.conf配置文件,加入下面指令:

location / {
    content_by_lua 'ngx.say("hello world!")';
}

重启nginx,用curl测试

[root@localhost ~]# curl -i localhost
HTTP/1.1 200 OK
Server: nginx/1.4.1
Date: Tue, 24 Sep 2013 23:23:58 GMT
Content-Type: application/octet-stream
Transfer-Encoding: chunked
Connection: keep-alive
hello world!


六、大功告成

更多可用指令请查阅:http://wiki.nginx.org/HttpLuaModule

相关文章
相关标签/搜索