最新消息:我不是很想,只是偶尔想想

FastDFS+GraphicsMagick+tengine+lua实现实时动态缩略图

linux 冯某某 4125浏览 0评论

系统环境:CentOS 6.8

安装tengine及fastdfs模块(storage节点上),主要编译参数如下(提前下载tengine-2.2.0、fastdfs-nginx-module压缩包)

 yum -y install lua-devel GeoIP-devel gd-devel libxslt-devel libxml2-devel openssl-devel pcre-devel gcc
tar -zxvf /tmp/fastdfs-nginx-module.tar.gz -C /usr/local/src
tar -zxvf /tmp/tengine-2.2.0.tar.gz -C /tmp/
cd /tmp/tengine-2.2.0 && ./configure --user=nginx --group=nginx --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --enable-mods-static=all --enable-mods-shared=all --add-module=/usr/local/src/fastdfs-nginx-module/src && make && make install

安装GraphicsMagick

yum -y install GraphicsMagick GraphicsMagick-devel

安装Lua相关依赖

yum -y install  lua lua-devel readline readline-devel

配置tengine(/etc/nginx/nginx.conf)

主要注意dso和location,配置文件中G01指的是fastdfs的groupname

user nginx;
worker_processes 4;
events {
worker_connections 1024;
}
dso {
load ngx_http_access_module.so;
load ngx_http_addition_filter_module.so;
load ngx_http_autoindex_module.so;
load ngx_http_browser_module.so;
load ngx_http_charset_filter_module.so;
load ngx_http_concat_module.so;
load ngx_http_empty_gif_module.so;
load ngx_http_fastcgi_module.so;
load ngx_http_flv_module.so;
load ngx_http_footer_filter_module.so;
load ngx_http_geoip_module.so;
load ngx_http_image_filter_module.so;
load ngx_http_limit_conn_module.so;
load ngx_http_limit_req_module.so;
load ngx_http_lua_module.so;
load ngx_http_map_module.so;
load ngx_http_memcached_module.so;
load ngx_http_mp4_module.so;
load ngx_http_random_index_module.so;
load ngx_http_referer_module.so;
load ngx_http_reqstat_module.so;
load ngx_http_rewrite_module.so;
load ngx_http_scgi_module.so;
load ngx_http_secure_link_module.so;
load ngx_http_slice_module.so;
load ngx_http_split_clients_module.so;
load ngx_http_sub_filter_module.so;
load ngx_http_sysguard_module.so;
load ngx_http_trim_filter_module.so;
load ngx_http_upstream_ip_hash_module.so;
load ngx_http_upstream_least_conn_module.so;
load ngx_http_upstream_session_sticky_module.so;
load ngx_http_user_agent_module.so;
load ngx_http_userid_filter_module.so;
load ngx_http_uwsgi_module.so;
load ngx_http_xslt_filter_module.so;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
location /G01/M00 {
root /data/storage/data;
ngx_fastdfs_module;
}
location ~* ^/G01/M00/(.+.(png|jpg|jpeg|gif))@(d+)w_(d+)h$ {
root /data/storage/data;
ngx_fastdfs_module;
set $image_root /data/storage/data/;
set $file "$image_root$1@$3w_$4h";
if ( !-f $file ) {
add_header X-Powered-By 'Lua GraphicsMagick';
add_header file-path $request_filename;
lua_code_cache off;
set $request_filepath /data/storage/data/$1;
set $width $3;
set $height $4;
content_by_lua_file /etc/nginx/lua/image.lua;
}
default_type image/png;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

创建lua脚本,内容如下

/etc/nginx/lua/image.lua

local command = "/usr/bin/gm convert " .. ngx.var.request_filepath .. " -resize " .. ngx.var.width .. "x" .. ngx.var.height .. " +profile "*" " .. ngx.var.request_filepath .. "@" .. ngx.var.width .. "w_" .. ngx.var.height .. "h";
os.execute(command);
ngx.exec(ngx.var.request_uri);

访问地址为http://nginx/groupname/xx/xx/filename.png@100w_200h

转载请注明:冯某某 » FastDFS+GraphicsMagick+tengine+lua实现实时动态缩略图

发表我的评论
取消评论
表情