代码改变世界

nginx 安装Lua扩展 使服务器更强大

2013-08-02 15:50  zwz2008  阅读(630)  评论(0编辑  收藏  举报

一、下载luajit 2.0

http://luajit.org/download.html

二、下载nginx源代码

http://www.nginx.org/en/download.html

三、下载nginx_devel_kit

https://github.com/simpl/ngx_devel_kit/tags

四、下载lua-nginx-module

https://github.com/chaoslawful/lua-nginx-module/tags

五、先安装luajit

make & make install

六、设置环境变量:

export LUAJIT_LIB=/usr/local/lib

export LUAJIT_INC=/usr/local/include/luajit-2.0

七、安装nginx

./configure --with-http_stub_status_module --add-module=/mnt/hgfs/vmware/ngx_devel_kit-0.2.17rc2 --add-module=/mnt/hgfs/vmware/lua-nginx-module-0.7.5rc1

make -j2

make install

八、运行

报错误:

[root@localhost nginx]# sbin/nginx -v
sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory

解决:

echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf

/sbin/ldconfig

 

测试方法:

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

实例:

1)图片处理

 1 location /upload/ {
 2                 if (-f $request_filename) {
 3                         rewrite ^ /$uri last;
 4                 }
 5                 #default_type 'text/plain';
 6                 set $image_root /home/www/zyyproj/static/misc;
 7                 if ($uri ~* "/upload/(([0-9a-zA-Z]+)/([\d]+)/([0-9a-zA-Z]+).(jpg|png|gif)_([\d]+x[\d]+)+:?([\d|-]+)?:?([\d|-]+)?.(jpg|png|gif))$") {
 8                         set $resize "$6";
 9                         set $rotate "$7";
10                         set $trans "$8";
11                         set $tarExt "$9";
12                         set $resource "$image_root/upload/$2/$3/$4.$5";
13                         set $target "$image_root$uri";
14                 }
15                 rewrite_by_lua '
16                         function table.contains(table, element)
17                                 for _, value in pairs(table) do
18                                         if value == element then
19                                                 return true
20                                         end
21                                 end
22                                 return false
23                         end
24                         --table.contains对传入参数作处理
25                         local command = "/usr/local/imagemagick/bin/convert";
26                         if(ngx.var.trans == "1" or ngx.var.tarExt == "png") then
27                                 command = command.." -bordercolor white -border 1x1 -matte -fill none -fuzz 20% -draw \'matte 0,0 floodfill\' -shave 1x1";
28                         end
29                         if(ngx.var.resize ~= "") then
30                                 command = command.." -resize "..ngx.var.resize;
31                         end
32                         if(ngx.var.rotate ~= "") then
33                                 command = command.." -rotate "..ngx.var.rotate;
34                         end
35                         command = command.." "..ngx.var.resource.." "..ngx.var.target;
36                         os.execute(command);
37                         --ngx.req.set_uri($request_filename, true);
38                         --ngx.say(command)
39                 ';
40                 #content_by_lua  '
41                 #
42                 #';
43         }