解決nginx+lua搭建文件上傳下載服務(wù)問(wèn)題
導(dǎo)語(yǔ)
項(xiàng)目需要做一個(gè)文件上傳下載服務(wù),利用 nginx+lua 做一個(gè)代理服務(wù),上傳入口統(tǒng)一,分發(fā)到不同的機(jī)器存儲(chǔ),下載鏈接和物理存儲(chǔ)隔離,支持添加 agent 的方式擴(kuò)容,這里主要講一下思路和搭建配置過(guò)程,大神勿噴。
主要邏輯

上傳
前端請(qǐng)求 nginx 服務(wù), nginx 調(diào)用 upload 腳本,腳本通過(guò)查找配置,找到對(duì)應(yīng)的邏輯存儲(chǔ)路徑和物理存儲(chǔ)機(jī)器的 agent 的 ip 和端口,通過(guò) tcp 發(fā)包到對(duì)應(yīng) agent ,部署在對(duì)應(yīng)機(jī)器的 agent 接受數(shù)據(jù),并寫到本地文件。
下載
http下載請(qǐng)求 nginx , nginx 調(diào)用 download 腳本,腳本解析鏈接參數(shù),根據(jù)參數(shù)找到對(duì)應(yīng)的 agent 地址,請(qǐng)求返回文件二進(jìn)制內(nèi)容,腳本接受到 agent 返回的數(shù)據(jù),返回給請(qǐng)求端。
配置Nginx+lua
接下來(lái)主要講一下 nginx 安裝配置(這里包括lua的二進(jìn)制流處理 lpack, md5計(jì)算, mysql 操作, json 操作)
1、安裝 nginx
下載http://nginx.org/en/download.html
解壓tar -xvf nginx-1.10.3.tar.gz
2、安裝 luajit(輕量級(jí) lua)
http://luajit.org/download.html
修改 makefile 里面的安裝路徑export PREFIX= /usr/local/luajit
然后安裝make &make install
3、安裝nginx_lua_module
下載https://github.com/openresty/lua-nginx-module
解壓
4、 安裝ngx_devel_kit (NDK提供函數(shù)和宏處理一些基本任務(wù),減輕第三方模塊開(kāi)發(fā)的代碼量)
下載https://github.com/simpl/ngx_devel_kit/
5、 安裝編譯,導(dǎo)入
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. make -j2 make install
啟動(dòng)/usr/local/nginx/sbin/nginx 重啟命令` usr/local/nginx/sbin/nginx -s reload v
如果報(bào)錯(cuò)找不到luajit庫(kù)ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
測(cè)試nginx直接打開(kāi)瀏覽器就可以了http:10.x.x.x:8080就可以看到歡迎界面了
6 、配置conf/nginx.conf運(yùn)行 lua 腳本
增加lua庫(kù)的查找路徑lua_package_path,lua_package_cpath

7、增加mysql.lua下載 https://github.com/openresty/lua-resty-mysql 拷貝到lua_package_path 目錄下就可以了
8、增加 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目錄下
9、安裝lpack 可以用現(xiàn)成的 lpack.lua 拷貝到 lua_package_path 或者用 https://github.com/LuaDist/lpack 編譯生成 lpack.so拷貝到 lua_package_cpath 64位需要增加編譯命令 -fPIC
10、upload.lua下載https://github.com/openresty/lua-resty-upload
11、md5下載 https://github.com/openresty/lua-resty-string
主要代碼
1、前端上傳頁(yè)面代碼
<!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>
2、upload上傳代碼,該模塊在解析文件上傳請(qǐng)求的過(guò)程中,主要采用了簡(jiǎn)單的類似有限狀態(tài)機(jī)的算法來(lái)實(shí)現(xiàn)的,在不同的狀態(tài)由相應(yīng)的 handler 進(jìn)行處理。
--文件下載服務(wù)寫到 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+")
--存儲(chǔ)的文件路徑
--ngx.say("failed to open file ", filePath)
if not fileToSave then
--ngx.say("failed to open file ", filePath .. errmsg)
ErrorMsg="打開(kāi)文件失敗"..filePath .. errmsg
return 1
end
else
ErrorMsg="請(qǐng)求參數(shù)找不到文件名字"
return 1
end
--跳出循環(huán)
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
3、tcp接收二進(jìn)制數(shù)據(jù)
-- 讀取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
4、tcp寫二進(jìn)制數(shù)據(jù),這里和 agent 的通信協(xié)議是:開(kāi)始標(biāo)志位+包長(zhǎng)度+json 字符串+結(jié)束標(biāo)志位,所以對(duì)應(yīng) pack 用的參數(shù)就是 bIAb ,> 就是轉(zhuǎn)化為大端
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)
5、下載錯(cuò)誤的時(shí)候,使用了 redirect 直接跳轉(zhuǎn)到錯(cuò)誤頁(yè)面,方便輸出錯(cuò)誤信息,其實(shí)這里還可以做用戶 token 校驗(yàn)
local ErrorUrl="/downloadError.html" ErrorMsg="url 參數(shù)解析有問(wèn)題 "..index return ngx.redirect(ErrorUrl.."?msg="..ErrorMsg,``` ngx.HTTP_MOVED_TEMPORARILY)
總結(jié)
以上所述是小編給大家介紹的解決nginx+lua搭建文件上傳下載服務(wù)問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
關(guān)于nginx報(bào)錯(cuò)405?not?allowed解決方法總結(jié)
這篇文章主要給大家介紹了關(guān)于nginx報(bào)錯(cuò)405?not?allowed解決方法的相關(guān)資料,nginx遇到post請(qǐng)求靜態(tài)文件會(huì)得到405錯(cuò)誤,文中通過(guò)代碼介紹的非常詳細(xì),也給出了推薦方法,需要的朋友可以參考下2023-10-10
如何利用nginx通過(guò)正則攔截指定url請(qǐng)求詳解
這篇文章主要介紹了如何利用nginx通過(guò)正則攔截指定url請(qǐng)求的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用nginx具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
解析阿里云centos7服務(wù)器nginx配置及常見(jiàn)問(wèn)題解答
這篇文章主要介紹了阿里云centos7服務(wù)器nginx配置及常見(jiàn)問(wèn)題解答,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Nginx設(shè)置靜態(tài)頁(yè)面壓縮和緩存過(guò)期時(shí)間的方法
這篇文章主要介紹了Nginx設(shè)置靜態(tài)頁(yè)面壓縮和緩存過(guò)期時(shí)間的方法,也是服務(wù)器架設(shè)后的必備設(shè)置,需要的朋友可以參考下2015-07-07
Nginx服務(wù)器中414錯(cuò)誤和504錯(cuò)誤的配置解決方法
這篇文章主要介紹了Nginx服務(wù)器中414錯(cuò)誤和504錯(cuò)誤的配置解決方法,分別對(duì)應(yīng)Request-URI Too Large和Gateway Time-out這樣的錯(cuò)誤提示,需要的朋友可以參考下2015-12-12
Mac使用Nginx設(shè)置代理并禁用自帶Apache的問(wèn)題記錄
本文介紹如何在Mac上禁用自帶的Apache服務(wù)并安裝Nginx,首先需要關(guān)閉Apache并禁止其自啟動(dòng),接著,通過(guò)Homebrew安裝Nginx,并配置其文件和目錄,最后,介紹了如何生成SSL/自簽名證書,詳細(xì)步驟包括修改Apache配置、安裝Nginx、編輯Nginx配置文件以及驗(yàn)證和重啟Nginx服務(wù)2024-09-09
Nginx rewrite和proxy_pass的區(qū)別及說(shuō)明
這篇文章主要介紹了Nginx rewrite和proxy_pass的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

