编译自己的nginx环境
如果已有不需要安装
yum install lua-devel
yum install ImageMagick wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz cd LuaJIT-2.0.5/ make & make install wget https://github.com/openresty/lua-nginx-module/archive/v0.10.12.tar.gz wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0rc1.tar.gz wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.20.tar.gz 解压 mv到 /usr/local/ export LUAJIT_LIB=/usr/local/lib需要修改,参照 https://github.com/happyfish100/fastdfs-nginx-module/issues/32
fastdfs-nginx-module/src/config ngx_module_incs="/usr/include/fastdfs /usr/include/fastcommon/" CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"wget http://nginx.org/download/nginx-1.15.3.tar.gz
./configure --prefix=/data/sdc/daipengxiang/nginx-1.15.3 --add-module=/usr/local/ngx_devel_kit-0.3.0rc1 --add-module=/usr/local/lua-nginx-module-0.10.13 --add-module=/usr/local/fastdfs-nginx-module-1.20/srcmake
make installcp /data/sdk/testEnv/softwares/fastdfs-5.05/conf/http.conf /data/sdk/testEnv/softwares/fastdfs-5.05/conf/mime.types /etc/fdfs/
nginx 配置
location /M00 { root /data/sdf/test_storage1/data; ngx_fastdfs_module; }ln -s /data/sdf/test_storage1/data /data/sdf/test_storage1/data/M00
ln -s /data/sdk/test_storage1/data /data/sdk/test_storage1/data/M01 ln -s /data/sdj/test_storage1/data /data/sdj/test_storage1/data/M02 ln -s /data/sdl/test_storage1/data /data/sdl/test_storage1/data/M03 需要注意 /data/sdf/目录权限,一般是fastdb用户,nginx 用户取不到,改成755解决
nginx 配置
#缩略图访问路径 location /fastdfs_tmp { alias /data/sdc/daipengxiang/tmp/fastdfs_tmp; } location /test1/M00 { error_log /data/sdc/daipengxiang/nginx1.15/logs/error.log debug; alias /data/sdf/test_storage1/data; set $image_root "/data/sdf/test_storage1/data"; #缩略图保存位置 set $buf_dir "/data/sdc/daipengxiang/tmp/fastdfs_tmp"; #缩略图访问路径 对应上面配置的缩略图访问路径 set $buf_root "/fastdfs_tmp"; #access_by_lua_file "/data/sdc/daipengxiang/nginx-lua-fastdfs-GraphicsMagick-master/lua/access.lua"; 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 $file_name "$5"; set $file "$image_dir$file_name"; } if ($arg_compress) { # 关闭lua代码缓存,方便调试lua脚本 #lua_code_cache off; content_by_lua_file "/data/sdc/daipengxiang/nginx-lua-fastdfs-GraphicsMagick-master/lua/fastdfs.lua"; } if ($arg_size) { # 关闭lua代码缓存,方便调试lua脚本 #lua_code_cache off; content_by_lua_file "/data/sdc/daipengxiang/nginx-lua-fastdfs-GraphicsMagick-master/lua/fastdfs.lua"; } ngx_fastdfs_module; }
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 falseend-- 检测文件是否存在local file_exists = function(name) local f=io.open(name,"r") if f~=nil then io.close(f) return true else return false endendlocal area = nillocal compress = nil-- 资源local originalUri = ngx.var.uri;-- 完整的原文件路径local originalFile = ngx.var.file;-- 文件名local fileName = ngx.var.file_name;-- 缩略图存放路径 TODO 哈希散列文件目录local bufDir = ngx.var.buf_dir;local bufRoot = ngx.var.buf_root;-- 请求参数local args = ngx.req.get_uri_args();if args.size then area = args.size;endif args.compress then compress = args.compress;end-- check original fileif not file_exists(originalFile) then local fileid = string.sub(originalUri, 2); -- main local fastdfs = require('restyfastdfs') local fdfs = fastdfs:new() fdfs:set_tracker("10.19.19.23", 22222) 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) endend-- 创建缩略图或压缩图local image_sizes = {"960x540", "540x360", "60x60","100x100"};function table.contains(table, element) for _, value in pairs(table) do if value == element then return true end end return false end-- 判断是否是缩略图local isNew = false;-- 目标文件位置local targetFile = bufDir .. "/" .. fileName;-- 目标文件新增后缀 如 _100x100.jpg_compress70.jpglocal newSuffix = "";-- 目标文件urilocal newUri = bufRoot .. "/" .. fileName;-- 拼接后缀if table.contains(image_sizes, area) then isNew = false; newSuffix = "_" .. area .. ".jpg";end;if compress~=nil then isNew = false; newSuffix = newSuffix .. "_compress" .. compress .. ".jpg";end;-- 生成新的uri和目标文件路径if newSuffix~="" then targetFile = targetFile .. newSuffix; newUri = newUri .. newSuffix;end-- 不是缩略图直接返回if isNew==true then ngx.exec(ngx.var.uri) ngx.exit(200)end-- 已经存在直接返回 newUri 和 targetFile是对应的if file_exists(targetFile) then ngx.exec(newUri) ngx.exit(200)endif table.contains(image_sizes, area) then local command = "convert " .. originalFile .. " -thumbnail " .. area .. " -background white -gravity center -extent " .. area .. " " .. targetFile; os.execute(commandmv); originalFile = targetFile;end;if compress~=nil then local command = "identify -verbose -format '%Q' " .. originalFile .. " "; local oriquality = io.popen(command):read("*all"); local compressNew = oriquality*compress/100; --ngx.header['Content-Type'] = 'text/html'; --ngx.say(oriquality); --ngx.exit(404); local command = "convert -format jpg -quality " .. compressNew .."% " .. originalFile .. " " .. targetFile; --ngx.say(bufDir); --ngx.exit(404); os.execute(command); -- local commandmv = "mv " .. ngx.var.file .. ".jpg " .. ngx.var.file; -- os.execute(commandmv);end;--ngx.say(newUri);--ngx.say(targetFile);--ngx.exit(200);if file_exists(targetFile) then --ngx.req.set_uri(ngx.var.uri, true); ngx.exec(newUri)else ngx.exec(ngx.var.uri)end
权限控制lua
生成链接的部分就是简单的MD5或者更简单高效的crc32。如果动态的token和ttl,肯恩有无法缓存的问题。
-- 获取请求路径,不包括参数。例如:/group1/M00/00/00/wKjlpltF-K-AZQQsAABhhboA1Kk469.pnglocal uri = ngx.var.uri;-- 获取请求参数local args = ngx.req.get_uri_args();-- 获取请求参数中时间戳信息,传入的是毫秒local ts = args["expire"];-- 获取请求参数中 token 信息local token1 = args["token"];-- 更新系统缓存时间戳ngx.update_time();-- 获取当前服务器系统时间,ngx.time() 获取的是秒local getTime = ngx.time();-- 计算时间差local diffTime = tonumber(ts) - getTime;-- md5 加盐加密local token2 = ngx.md5(tostring(uri) .. "4621d373cade4e83" .. tostring(ts));-- 判断时间是否有效if (tonumber(diffTime) > 0) then -- 校验 token 是否相等 if token1 ~= token2 then -- 校验通过则转发请求 ngx.exit(ngx.HTTP_FORBIDDEN) endelse ngx.exit(ngx.HTTP_FORBIDDEN)end
增加vanish配置,参照官网install,yum配置