1. 程式人生 > >nginx lua fastdfs動態縮圖

nginx lua fastdfs動態縮圖



1.安裝軟體基礎包

useradd -r nginx -s /sbin/nologin

yum -y install epel-release git

yum install -y gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel gd-devel

yum install -y libpng libjpeg libpng-devel libjpeg-devel ghostscript libtiff libtiff-devel freetype freetype-devel readline-devel ncurses-devel

2.下載相關軟體,其中nginx-http-concat和echo-nginx-module模組非必須

git clone https://github.com/alibaba/nginx-http-concat.git

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

git clone https://github.com/openresty/echo-nginx-module.git

git clone https://github.com/openresty/lua-nginx-module.git

 

# 安裝LuaJIT

cd /usr/local/src

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

tar -zxf 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

ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

 

# 安裝Lua

cd /usr/local/src

wget http://www.lua.org/ftp/lua-5.3.1.tar.gz  

tar -zxvpf lua-5.3.1.tar.gz

cd lua-5.3.1

make linux && make install

 

# 安裝GM

cd /usr/local/src

wget ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/1.3/GraphicsMagick-1.3.18.tar.gz

tar -zxvf GraphicsMagick-1.3.18.tar.gz

cd GraphicsMagick-1.3.18

./configure --prefix=/usr/local/GraphicsMagick-1.3.18 --enable-shared

make  && make install

ln -s /usr/local/GraphicsMagick-1.3.18 /usr/local/GraphicsMagick

 

# 安裝nginx

cd /usr/local/src

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

tar -zxvf nginx-1.10.2.tar.gz

cd nginx-1.10.2

./configure --prefix=/usr/local/nginx-1.10.2 \

--user=nginx \

--group=nginx \

--with-http_ssl_module \

--with-http_realip_module \

--with-http_sub_module \

--with-http_flv_module \

--with-http_dav_module \

--with-http_gzip_static_module \

--with-http_stub_status_module \

--with-http_addition_module \

--add-module=/usr/local/src/fastdfs-nginx-module/src \

--with-http_image_filter_module \

--with-pcre \

--add-module=../nginx-http-concat \

--add-module=../lua-nginx-module \

--add-module=../ngx_devel_kit \

--add-module=../echo-nginx-module \

--with-ld-opt=-Wl,-rpath,$LUAJIT_LIB 

 

make

make install

 

ln -s /usr/local/nginx-1.10.2 /usr/local/nginx

 3.第一種情況:普通上傳的檔案生成縮圖

  a. 準備gm的lua指令碼

cd /usr/local/nginx/conf/

mkdir lua

 

# GM的lua指令碼

vim ImageResizer.lua

local command = "/usr/local/GraphicsMagick/bin/gm convert   -auto-orient -strip " .. ngx.var.request_filepath .. " -resize " .. ngx.var.width .. "x" .. ngx.var.height .. " +profile \"*\" " .

. ngx.var.request_filepath .. "_" .. ngx.var.width .. "x" .. ngx.var.height .. "." .. ngx.var.ext;

os.execute(command);

ngx.exec(ngx.var.request_uri);


b.修改nginx配置檔案

server {

        listen       80;

        server_name  192.168.1.19;

        root /data/attached/b2b;

   

        location /lua1 {

        default_type 'text/plain';

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

        }

   

   

          location ~* ^(.+\.(jpg|jpeg|gif|png))_(\d+)x(\d+)\.(jpg|jpeg|gif|png)$ {

                root /data/attached/b2b;

                if (!-f $request_filename) {                     # 如果檔案不存在時才需要裁剪

                        add_header X-Powered-By 'Lua GraphicsMagick';# 此 HTTP Header 無實際意義,用於測試

                        add_header file-path $request_filename;      # 此 HTTP Header 無實際意義,用於測試

                        #lua_code_cache off;                         # 在編寫外部 Lua 指令碼時,設定為 off Nginx 不會快取 Lua,方便除錯

                        set $request_filepath /data/attached/b2b$1;  # 設定原始圖片路徑,如:/document_root/1.gif

                        set $width $3;                               # 設定裁剪/縮放的寬度

                        set $height $4;                              # 設定裁剪/縮放的高度

                        set $ext $5;                                 # 圖片檔案格式字尾

                        content_by_lua_file /usr/local/nginx/conf/lua/ImageResizer.lua;    # 載入外部 Lua 檔案

                }

   

   

        }

}
c.圖片目錄賦予網站使用者寫的許可權
chown nginx.nginx /data/attached/

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

d.檢視原圖

  e.縮圖

4.第二種情況:fastdfs上傳的檔案生成縮圖

   a.下載lua指令碼到/usr/local/nginx/conf/lua目錄下

git clone https://github.com/hpxl/nginx-lua-fastdfs-GraphicsMagick.git

cd nginx-lua-fastdfs-GraphicsMagick/lua

cp ./* /usr/local/nginx/conf/lua/

 

cd /usr/local/nginx/conf/lua/

vim fastdfs.lua                 # 46行配置tracker的地址,72行配置gm的命令變數

 46     fdfs:set_tracker("10.160.43.105", 22122)

 47     fdfs:set_timeout(1000)

 48     fdfs:set_tracker_keepalive(0, 100)

 49     fdfs:set_storage_keepalive(0, 100)

 50     local data = fdfs:do_download(fileid)

 51     if data then

 52        -- check image dir

 53         if not is_dir(ngx.var.image_dir) then

 54             os.execute("mkdir -p " .. ngx.var.image_dir)

 55         end

 56         writefile(originalFile, data)

 57     end

 58 end

 59 

 60 -- 建立縮圖

 61 local image_sizes = {"80x80", "800x600", "40x40", "60x60"};

 62 function table.contains(table, element)  

 63     for _, value in pairs(table) do

 64         if value == element then

 65             return true 

 66         end  

 67     end

 68     return false

 69 end 

 70 

 71 if table.contains(image_sizes, area) then

 72     local command = "/usr/local/GraphicsMagick/bin/gm convert " .. originalFile  .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.file;

       

 73     os.execute(command);

 74 end;

 75 

 76 if file_exists(ngx.var.file) then

 77     --ngx.req.set_uri(ngx.var.uri, true);  

 78     ngx.exec(ngx.var.uri)

 79 else

 80     ngx.exit(404)

 81 end


b.配置nginx檔案

#

server {

        listen 8801;

        server_name 10.160.43.26;

 

        # LUA

        location /hello {

                default_type 'text/plain';

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

                }

 

        # fastdfs 縮圖生成

        location /group1/M00 {

                alias /data/fastdfs/data/data;

 

                set $image_root "/data/fastdfs/data/data";

                if ($uri ~ "/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/(.*)") {

                  set $image_dir "$image_root/$3/$4/";

                  set $image_name "$5";

                  set $file "$image_dir$image_name";

                }

 

                if (!-f $file) {

        #         # 關閉lua程式碼快取,方便除錯lua指令碼

                  #lua_code_cache off;

                  content_by_lua_file "/usr/local/nginx/conf/lua/fastdfs.lua";

                }

                ngx_fastdfs_module;

        }

 

        # log file

        access_log  logs/img_access.log access;

}

c.圖片目錄賦予網站使用者寫的許可權

/data/fastdfs/data

chown -R nginx.nginx data

/usr/local/nginx/sbin/nginx -t

/usr/local/nginx/sbin/nginx -s reload

 d.測試檔案

 原圖

參考文件:http://ylw6006.blog.51cto.com/470441/1830002

              https://github.com/hpxl/nginx-lua-fastdfs-GraphicsMagick