日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

主要邏輯

nginx+lua搭建文件上傳下載服務(wù)問(wèn)題怎么解決

上傳

前端請(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ù),并寫(xiě)到本地文件。

下載

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

下載

解壓tar -xvf nginx-1.10.3.tar.gz

2、安裝 luajit(輕量級(jí) lua)

修改 makefile 里面的安裝路徑export prefix= /usr/local/luajit

然后安裝make &make install

3、安裝nginx_lua_module

下載

解壓

4、 安裝ngx_devel_kit (ndk提供函數(shù)和宏處理一些基本任務(wù),減輕第三方模塊開(kāi)發(fā)的代碼量)

下載

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

nginx+lua搭建文件上傳下載服務(wù)問(wèn)題怎么解決

7、增加mysql.lua下載 拷貝到lua_package_path 目錄下就可以了

8、增加 csjon

修改 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下載

11、md5下載

主要代碼

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ù)寫(xiě)到 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寫(xiě)二進(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)

以上就是nginx+lua搭建文件上傳下載服務(wù)問(wèn)題怎么解決的詳細(xì)內(nèi)容。

分享到:
標(biāo)簽:上傳下載 搭建 文件 服務(wù)器 解決
用戶無(wú)頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過(guò)答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定