Nginx + Lua搭建文件上傳下載服務

收錄待用,修改轉載已取得騰訊雲受權html


最新騰訊雲技術公開課直播,提問騰訊W3C表明,如何從小白成爲技術專家?點擊瞭解活動詳情前端

做者 | 莊進發
編輯 | 迷鹿mysql

莊進發,信息安所有後臺開發工程師,主要負責內部oa系統的後臺搭建linux

導語

項目須要作一個文件上傳下載服務,利用 nginx+lua 作一個代理服務,上傳入口統一,分發到不一樣的機器存儲,下載連接和物理存儲隔離,支持添加 agent 的方式擴容,這裏主要講一下思路和搭建配置過程,大神勿噴。nginx

主要邏輯

上傳

前端請求 nginx 服務, nginx 調用 upload 腳本,腳本經過查找配置,找到對應的邏輯存儲路徑和物理存儲機器的 agent 的 ip 和端口,經過 tcp 發包到對應 agent ,部署在對應機器的 agent 接受數據,並寫到本地文件。git

下載

http下載請求 nginx , nginx 調用 download 腳本,腳本解析連接參數,根據參數找到對應的 agent 地址,請求返回文件二進制內容,腳本接受到 agent 返回的數據,返回給請求端。github

配置Nginx+lua

接下來主要講一下 nginx 安裝配置(這裏包括lua的二進制流處理 lpack, md5計算, mysql 操做, json 操做)算法

一、安裝 nginxsql

下載http://nginx.org/en/download.html
解壓tar -xvf nginx-1.10.3.tar.gzjson

二、安裝 luajit(輕量級 lua)

http://luajit.org/download.html
修改 makefile 裏面的安裝路徑export PREFIX= /usr/local/luajit
而後安裝make &make install

三、安裝nginx_lua_module
下載https://github.com/openresty/lua-nginx-module
解壓

四、 安裝ngx_devel_kit (NDK提供函數和宏處理一些基本任務,減輕第三方模塊開發的代碼量)

下載https://github.com/simpl/ngx_devel_kit/

五、 安裝編譯,導入

export LUAJIT_LIB=/usr/local/luajit/lib  

export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0 

./configure --prefix=/usr/local/nginx  --with-http_stub_status_module --with-http_ssl_module --add-module=/home/oicq/jeffzhuang/ngx_devel_kit-0.3.0 --add-module=/home/oicq/jeffzhuang/lua-nginx-module-0.10.3

make -j2 
make install

啓動/usr/local/nginx/sbin/nginx 重啓命令` usr/local/nginx/sbin/nginx -s reload v

若是報錯找不到luajit庫ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

測試nginx直接打開瀏覽器就能夠了http:10.x.x.x:8080就能夠看到歡迎界面了

6 、配置conf/nginx.conf運行 lua 腳本

增長lua庫的查找路徑lua_package_path,lua_package_cpath

七、增長mysql.lua下載 https://github.com/openresty/lua-resty-mysql 拷貝到lua_package_path 目錄下就能夠了

八、增長 csjon http://www.kyne.com.au/~mark/software/download/lua-cjson-2.1.0.tar.gz

修改 Makefile 裏面的 PREFIX=/usr/local/luajit就是luajit 的安裝路徑,make後將生成的 cjson.so拷貝到 lua_package_cpath目錄下

九、安裝lpack 能夠用現成的 lpack.lua http://blog.csdn.net/tom_221x/article/details/41119715 拷貝到 lua_package_path 或者用 https://github.com/LuaDist/lpack 編譯生成 lpack.so拷貝到 lua_package_cpath 64位須要增長編譯命令 -fPIC

十、upload.lua下載https://github.com/openresty/lua-resty-upload

十一、md5下載 https://github.com/openresty/lua-resty-string

主要代碼

一、前端上傳頁面代碼

<!DOCTYPE html>
<html>
    <head>
         <title>File upload example</title>
    </head>
    <body>
           <form action="emer_upload/order_system_storage" method="post" enctype="multipart/form-data">
           <input type="file" name="testFileName"/>
           <input type="submit" name="upload" value="Upload" />
           </form>
    </body>
</html>

二、upload上傳代碼,該模塊在解析文件上傳請求的過程當中,主要採用了簡單的相似有限狀態機的算法來實現的,在不一樣的狀態由相應的 handler 進行處理。

--文件下載服務寫到 saveRootPath .."/" .. filename 下面 
function DownLoad()

    local chunk_size = 4096
    local form,err=upload:new(chunk_size)
    if not form then
         ngx.log(ngx.ERR, "failed to new upload: ", err)
         ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
    end 

    form:set_timeout(100000)

    while true do
        local typ,res,err=form:read()
        if not typ then
            ErrorMsg="failed to read :"..err
            return 1
        end

        if typ =="header" then
            local key=res[1]
            local value=res[2]
            if key =="Content-Disposition" then
                local kvlist=string.split(value,';')
                 for _, kv in ipairs(kvlist) do
                    local seg = string.trim(kv)
                    if seg:find("filename") then
                        local kvfile = string.split(seg, "=")
                        filename = string.sub(kvfile[2], 2, -2)
                        if filename then
                            --獲取文件後綴名字
                            fileExtension=getExtension(filename)
                            local linuxTime=tostring(os.time())
                            filePath=saveRootPath .."/" ..linuxTime..filename
                            fileToSave,errmsg = io.open(filePath, "w+")
                            --存儲的文件路徑                    
                            --ngx.say("failed to open file ", filePath)
                            if not fileToSave then
                                --ngx.say("failed to open file ", filePath .. errmsg)
                                ErrorMsg="打開文件失敗"..filePath .. errmsg
                                return 1
                            end
                        else
                            ErrorMsg="請求參數找不到文件名字"
                            return 1
                        end
                        --跳出循環
                        break 
                    end
                 end
            end
        elseif typ =="body" then
            if fileToSave then
               fileToSave:write(res)
               fileMd5:update(res)
            end
        elseif typ =="part_end" then
            if fileToSave then
               local md5_sum=fileMd5:final()
               --ngx.say("md5: ", str.to_hex(md5_sum))
               fileMD532=str.to_hex(md5_sum)
               fileToSave:close()
               fileToSave = nil
            end            
        elseif typ =="eof" then
            break
        else
            ngx.log(ngx.INFO, "do other things")
        end
    end
    return 0
end

三、tcp接收二進制數據

-- 讀取byte
function readInt8(tcp)
    local next, val = string.unpack(tcp:receive(1), "b")
    return tonumber(val);
end
-- 讀取int16
function readInt16(tcp)
    local next, val = string.unpack(tcp:receive(2), "h");
    return tonumber(val);
end
-- 讀取int32
function readInt32(tcp)
    local next, val = string.unpack(tcp:receive(4), ">i");
    return tonumber(val);
end
-- 讀取字符串
function readString(tcp,len)
    return tostring(tcp:receive(len));
end

四、tcp寫二進制數據,這裏和 agent 的通訊協議是:開始標誌位+包長度+json 字符串+結束標誌位,因此對應 pack 用的參數就是 bIAb ,> 就是轉化爲大端

jsonData["filename"]=fileMD532 .. "." .. fileExtension
jsonData["cmd"]="write"
jsonData["fileSize"]=tostring(filelen)
jsonData["path"]=System.."/"..StorageDate
local Jsonstr=cjson.encode(jsonData)
local uiLen=string.len(Jsonstr)
senddata=bpack(">b1IAb",startIndex,uiLen,Jsonstr,endIndex)
socket:send(senddata)

五、下載錯誤的時候,使用了 redirect 直接跳轉到錯誤頁面,方便輸出錯誤信息,其實這裏還能夠作用戶 token 校驗

local ErrorUrl="/downloadError.html"
ErrorMsg="url 參數解析有問題 "..index
return ngx.redirect(ErrorUrl.."?msg="..ErrorMsg,``` ngx.HTTP_MOVED_TEMPORARILY)

原文連接:https://www.qcloud.com/community/article/291137

相關文章
相關標籤/搜索