# wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
# tar -zxvf LuaJIT-2.0.4.tar.gz
# cd LuaJIT-2.0.2
# make
# make install
--安装lua-nginx-module
下载ngx_devel_kit,nginx_lua_module解压
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_package_path "/usr/local/nginx/conf/lua/?.lua;;";
server {
listen 80;
server_name localhost;
location ~ '/images/thumbnail/([0-9a-z]+)_([0-9]+)x([0-9]+).jpg$' {
root /var;
}
location ~ '/images/([0-9a-z]+)_([0-9]+)x([0-9]+).jpg$' {
root /var;
set $image_root '/var/images';
set $fileName $1;
set $width $2;
set $height $3;
set $origin $image_root/$fileName.jpg;
set $file $image_root/thumbnail/${fileName}_${width}x$height.jpg;
set $uriNew /images/thumbnail/${fileName}_${width}x$height.jpg;
if (-f $file) {
rewrite ^ $uriNew;
break;
}
if (!-f $origin) {
return 404;
}
rewrite_by_lua '
local width = tonumber(ngx.var.height);
local height = tonumber(ngx.var.height);
if width and height then
local command = "convert -sample "..ngx.var.width.."x"..ngx.var.height.." "..ngx.var.origin.." "..ngx.var.file;
os.execute(command);
ngx.req.set_uri(ngx.var.uriNew, true);
else
ngx.exit(ngx.HTTP_NOT_FOUND);
end
';
}
location /group1/M00 {
alias /var/images;
#set $image_root "/usr/local/openresty/nginx/proxy_tmp/images";
set $image_root "/var/images";
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 "conf/lua/fastdfs.lua";
}
#ngx_fastdfs_module;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
fastdfs.lua
-- 写入文件
local function writefile(filename, info)
local wfile=io.open(filename, "w") --写入文件(w覆盖)
assert(wfile) --打开时验证是否出错
wfile:write(info) --写入传入的内容
wfile:close() --调用结束后记得关闭
end
-- 检测路径是否目录
local function is_dir(sPath)
if type(sPath) ~= "string" then return false end
local response = os.execute( "cd " .. sPath )
if response == 0 then
return true
end
return false
end
-- 检测文件是否存在
local file_exists = function(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
local area = nil
local originalUri = ngx.var.uri;
local originalFile = ngx.var.file;
local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)");
if index then
originalUri = string.sub(ngx.var.uri, 0, index-2);
area = string.sub(ngx.var.uri, index);
index = string.find(area, "([.])");
area = string.sub(area, 0, index-1);
local index = string.find(originalFile, "([0-9]+)x([0-9]+)");
originalFile = string.sub(originalFile, 0, index-2)
end
-- check original file
if not file_exists(originalFile) then
local fileid = string.sub(originalUri, 2);
-- main
local fastdfs = require('restyfastdfs')
local fdfs = fastdfs:new()
fdfs:set_tracker("192.168.117.100", 22122)
fdfs:set_timeout(1000)
fdfs:set_tracker_keepalive(0, 100)
fdfs:set_storage_keepalive(0, 100)
local data = fdfs:do_download(fileid)
if data then
-- check image dir
if not is_dir(ngx.var.image_dir) then
os.execute("mkdir -p " .. ngx.var.image_dir)
end
writefile(originalFile, data)
end
end
-- 创建缩略图
local image_sizes = {"80x80", "800x600", "40x40", "60x60"};
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 = "convert " .. originalFile .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.file;
os.execute(command);
end;
if file_exists(ngx.var.file) then
--ngx.req.set_uri(ngx.var.uri, true);
ngx.exec(ngx.var.uri)
else
ngx.exit(404)
end
参考资料:
https://github.com/openresty/lua-nginx-module
http://www.ttlsa.com/nginx/nginx-modules-ngx_lua/
http://www.iyunv.com/os/201504/387948.html
http://houxiyang.com/archives/112/
https://github.com/hpxl/nginx-lua-fastdfs-GraphicsMagick
https://github.com/azurewang/lua-resty-fastdfs