2013-09-07 16:40:39
来 源
itjs.cn
Nginx
该篇讲述结合Nginx+lua_Nginx+GraphicsMagick来实现实时缩略图,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。

安装graphi magic

wget http://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.16/GraphicsMagick-1.3.16.tar.gz/download

./configure --prefix=/usr/local/GraphicsMagick-1.3.16

make;make install

安装nginx需要的模块如下

liujit             http://luajit.org  

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

echo-nginx-module  https://github.com/agentzh/echo-nginx-module  

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

#下载luajit

http://luajit.org/download/LuaJIT-2.0.0-beta10.tar.gz

cd  LuaJIT-2.0.0-beta10

make && make install PREFIX=/usr/local/lj2

ln -sf luajit-2.0.0-beta10 /usr/local/lj2/bin/luajit

#下载  ngx_devel_kit 模块

https://github.com/simpl/ngx_devel_kit/zipball/master

unzip simpl-ngx_devel_kit-v0.2.17-10-g4192ba6.zip 

#下载 lua-nginx-module 模块

https://github.com/chaoslawful/lua-nginx-module/tarball/master

unzip chaoslawful-lua-nginx-module-v0.6.0-3-g936653c.zip 

#下载echo

https://github.com/agentzh/echo-nginx-module/zipball/master

unzip echo-nginx-module.zip 

#下载cache

http://labs.frickle.com/files/ngx_cache_purge-1.6.tar.gz

tar zvxf ngx_cache_purge-1.6.tar.gz

#下载nginx

wget http://nginx.org/download/nginx-1.2.1.tar.gz 

tar zvxf nginx-1.2.1.tar.gz 

设置环境变量

export LUAJIT_LIB=/usr/local/lj2/lib

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

export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH 

export  PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

GM_HOME=/usr/local/GraphicsMagick-1.3.16;

PATH=$GM_HOME/bin:$PATH;

export PATH

export GM_HOME

使环境变量生效:source /etc/profile

编译参数:

--prefix=/usr/local/nginx --user=tomcat --with-http_stub_status_module --add-module=/usr/src/simpl-ngx_devel_kit-4192ba6 --with-ld-opt=-Wl,-rpath,/usr/local/lj2/lib --add-module=/usr/src/ngx_cache_purge-1.6 --with-http_perl_module --add-module=/usr/src/chaoslawful-lua-nginx-module-936653c --add-module=/usr/src/agentzh-echo-nginx-module-b3ad5c1

注意:图片存储目录的用户需要跟nginx运行用户是同一用户,否则会出现缩略图文件不可写情况

lua模块测试

nginx.conf配置

location /lua1 {

default_type 'text/plain';

content_by_lua 'ngx.say("hello, lua")';

}

location  /image {

set $image_root /mnt/mfs/web/sq/webroot;

set $file "$image_root$uri";

if (!-f $request_filename) {

 rewrite_by_lua '

    local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)");

    local originalUri = string.sub(ngx.var.uri, 0, index-2);

    local area = string.sub(ngx.var.uri, index);

    index = string.find(area, "([.])");

    area = string.sub(area, 0, index-1);

    local image_sizes = {"140x140", "800x800", "90x90"};

    function table.contains(table, element)

       for _, value in pairs(table) do

          if value == element then

             return true

          end

       end

       return false

    end

    if table.contains(image_sizes, area) then

       local command = "/usr/local/GraphicsMagick-1.3.16/bin/gm convert " .. ngx.var.image_root ..  originalUri  .. " -thumbnail " .. area .. " -gravity center -extent " .. area .. " " .. ngx.var.file;

       os.execute(command);

    else

       ngx.exit(401);

    end;

 ';

}

alias /mnt/mfs/web/sq/webroot/image/;

expires 7d;

}

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。