当前位置: > Linux服务器 > nginx >

Nginx添加nginx_lua_module模块

时间:2016-06-06 13:07来源:linux.it.net.cn 作者:IT

nginx_lua_module是由淘宝的工程师清无(王晓哲)和春来(章亦春)所开发的nginx第三方模块,它能将lua语言嵌入到nginx配置中,从而使用lua就极大增强了nginx的能力。

nginx_lua_module项目地址:https://github.com/openresty/lua-nginx-module

安装前准备:

cd oneinstack/src

wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz 下载LuaJIT

wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz 下载ngx_devle_kit

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.2.tar.gz 下载lua_nginx_module

具体操作步骤:

  • 安装oneinstack一键包配置生产环境(步骤略)
  • 获取Nginx原有编辑配置
  • 编译及安装LuaJIT,ngx_devle_kit,lua_nginx_module
  • 配置nginx.conf,测试
安装LuaJIT
cd oneinstack/src
wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar xzf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make && make install
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0

解压模块
cd oneinstack/src
tar xzf v0.2.19.tar.gz
tar xzf v0.10.2.tar.gz

# /usr/local/nginx/sbin/nginx -V #查看nginx已经编译参数
cd nginx-1.10.0
make clean
./configure --prefix=/usr/local/nginx --user=www --group=www \
--with-ld-opt=-Wl,-rpath,/usr/local/lib/ \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-ipv6 \
--with-http_gzip_static_module \
--with-http_realip_module \
--with-http_flv_module \
--with-ld-opt=-ljemalloc \
--add-module=../lua-nginx-module-0.10.2 \
--add-module=../ngx_devel_kit-0.2.19
make
mv /usr/local/nginx/sbin/nginx{,_`date +%F`}
cp objs/nginx /usr/local/nginx/sbin

解决安装错误:

1.如遇到找不到库文件
echo “/usr/local/lib” > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig
即可
2./usr/local/nginx/sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory  
在 Nginx 编译时,需要指定 RPATH,记得加入下面选项:
./configure --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"  
或者export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH

测试是否成功:(修改/usr/local/nginx/conf/nginx.conf,在server下添加如下代码)

location /hello {
      default_type 'text/plain';
      content_by_lua 'ngx.say("hello, lua")';
}

service nginx reload

service nginx restart

再访问 http://ip/hello,出现“hello,lua”即安装完成!

修改配置文件实现url请求不区分大小写(如:/usr/local/nginx/conf/vhost/www.korwah.com.conf):

location ~ .*\ {
        if ($uri ~ [A-Z]){
                rewrite_by_lua 'return ngx.redirect(string.lower(ngx.var.uri),ngx.HTTP_MOVED_PERMANENTLY)';
                }
        }


(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容