nginx_lua_graphicmagick搭建图片处理服务器

ngx_lua_module 是一个nginx http模块,它把 lua 解析器内嵌到 nginx,用来解析并执行lua 语言编写的网页后台脚本

特性很牛叉,可自行百度查看,这里主要是示范一下,如何在Nginx下安装lua-nginx-module模块

安装包下载

nginx  地址:http://www.nginx.org
luajit 地址:http://luajit.org/download.html
HttpLuaModule 地址:http://wiki.nginx.org/HttpLuaModule

1.安装luajit
cd /usr/local/src
wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar -xzvf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make
make install

2.安装nginx
cd /usr/local/src
wget http://nginx.org/download/nginx-1.10.2.tar.gz
tar -xzvf nginx-1.10.2.tar.gz
cd nginx-1.10.2

3.导入环境变量,根据实际路径 有可能是/usr/local/luajit/下
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
4.下载最新的lua-nginx-module 模块 :https://github.com/openresty/lua-nginx-module/tags,不需要安装
cd /usr/local/src
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.7.tar.gz
tar -xzvf v0.10.7.tar.gz

./configure –prefix=/usr/server/nginx \
–conf-path=/usr/server/nginx/nginx.conf \
–add-module=/usr/server/nginx/lua-nginx-module-0.8.6
make -j2
make install

./sbin/nginx -t
如果出现error:
 error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file
就查找到so文件,做个软连接到/usr/lib 或 /usr/lib64下再运行nginx
ln -s /usr/local/lib/libluajit-5.1.so.2 /usr/lib/
ln -s /usr/local/lib/libluajit-5.1.so.2 /usr/lib64/
配置nginx.conf

location ~* ^/lua(/.*) {
      default_type 'text/plain';
      content_by_lua 'ngx.say("hello,lua")';
}
重启nginx
访问localhost/lua试试看

5.安装graphicmagick
wget ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/1.3/GraphicsMagick-1.3.25.tar.gz
tar zxf GraphicsMagick-1.3.25.tar.gz
cd GraphicsMagick-1.3.25
./configure –prefix=/usr/local/graphicsmagick
make&make install
/usr/local/graphicsmagick/bin/gm version

我贴下配置文件和lua代码

以下是nginx 
#对类似_100x100.gif/jpg/png/jpeg进行缩略图处理
 location ~* _([0-9]+)x([0-9]+)\.(gif|jpg|png|jpeg)$ { #匹配文件名规则
 #rewrite ^/(.*)$ /123.jpg last;
 root /home/wwwroot/; #站点根目录
 set $image_root /home/wwwroot; #图片目录
 set $thumbnail_root /home/wwwroot/thumbnail; #缩略图存放目录
 set $image_ext $3; #图片扩展
 #如果缩略图文件存在,直接返回
 set $file $thumbnail_root$uri;
 if (-f $file) {
 rewrite ^/(.*)$ /thumbnail/$1 last;
 }
 #如果缩略图文件不存在,则应用缩略图模块处理
 if (!-f $file) {
 rewrite_by_lua_file /data/lua/thumbnail.lua;
 }
 }
以下/data/lua/thumbnail.lua文件
local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)");
 local originalUri = string.sub(ngx.var.uri, 0, index-2) .. '.' .. ngx.var.image_ext;--源文件路径
 lua_log('img_original_uri_new===' .. originalUri .. index .. ngx.var.image_ext ,ngx.ERR);--把信息输出到nginx的日志文件上 用于调试
 local area = string.sub(ngx.var.uri, index); 
 index = string.find(area, "([.])"); 
 area = string.sub(area, 0, index-1); 
 
 local image_sizes = {"80x80", "800x600", "40x40"}; --限制分辨率生成 也可以做一些别的判断
 function table.contains(table, element) 
  for _, value in pairs(table) do 
   if value == element then 
    return true 
   end 
  end 
  return false 
 end 
 function lua_log(msg,log_level) 
  ngx.log(log_level,msg)
 end
 if table.contains(image_sizes, area) then
 local command = "/usr/local/graphicsmagick/bin/gm convert " .. ngx.var.image_root .. originalUri .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.file;
 os.execute(command);--执行转换命令 格式可以上网查
 --ngx.redirect("http://www.netpc.com.cn", 302);
 ngx.req.set_uri(ngx.var.uri, true); 
 else 
 ngx.exit(404); 
 end;
此条目发表在开源代码分类目录,贴了, 标签。将固定链接加入收藏夹。

发表回复