編譯本身的nginx環境html
若是已有不須要安裝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/libgit
須要修改,參照 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/"github
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/src緩存
make
make install服務器
cp /data/sdk/testEnv/softwares/fastdfs-5.05/conf/http.conf /data/sdk/testEnv/softwares/fastdfs-5.05/conf/mime.types /etc/fdfs/app
nginx 配置
location /M00 {
root /data/sdf/test_storage1/data;
ngx_fastdfs_module;
}ide
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解決ui
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 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 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; end if args.compress then compress = args.compress; 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("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) end end -- 建立縮略圖或壓縮圖 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.jpg local newSuffix = ""; -- 目標文件uri local 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) end if 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.png local 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) end else ngx.exit(ngx.HTTP_FORBIDDEN) end
增長vanish配置,參照官網install,yum配置