nginx lua fastdfs动态缩略图

来源:互联网 发布:windows xp桌面 编辑:程序博客网 时间:2024/05/16 06:14


1.安装软件基础包

useradd -r nginx -s /sbin/nologinyum -y install epel-release gityum install -y gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel gd-develyum 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.gitgit clone https://github.com/simpl/ngx_devel_kit.gitgit clone https://github.com/openresty/echo-nginx-module.gitgit clone https://github.com/openresty/lua-nginx-module.git # 安装LuaJITcd /usr/local/srcwget http://luajit.org/download/LuaJIT-2.0.4.tar.gztar -zxf LuaJIT-2.0.4.tar.gzcd LuaJIT-2.0.4makemake installexport LUAJIT_LIB=/usr/local/libexport LUAJIT_INC=/usr/local/include/luajit-2.0ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2 # 安装Luacd /usr/local/srcwget http://www.lua.org/ftp/lua-5.3.1.tar.gz  tar -zxvpf lua-5.3.1.tar.gzcd lua-5.3.1make linux && make install # 安装GMcd /usr/local/srcwget ftp://ftp.graphicsmagick.org/pub/GraphicsMagick/1.3/GraphicsMagick-1.3.18.tar.gztar -zxvf GraphicsMagick-1.3.18.tar.gzcd GraphicsMagick-1.3.18./configure --prefix=/usr/local/GraphicsMagick-1.3.18 --enable-sharedmake  && make installln -s /usr/local/GraphicsMagick-1.3.18 /usr/local/GraphicsMagick # 安装nginxcd /usr/local/srcwget http://nginx.org/download/nginx-1.10.2.tar.gztar -zxvf nginx-1.10.2.tar.gzcd 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  makemake 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.lualocal 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.gitcd nginx-lua-fastdfs-GraphicsMagick/luacp ./* /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/datachown -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



0 0
原创粉丝点击