主要逻辑
前端请求nginx服务,nginx调用upload脚本,脚本通过查找配置,找到对应的逻辑存储路径和物理存储机器的agent的ip和端口,通过tcp发包到对应agent,部署在对应机器的agent接受数据,并写到本地文件。
配置nginx+lua
接下来主要讲一下nginx安装配置(这里包括lua的二进制流处理lpack,md5计算,mysql操作,json操作)
2、安装luajit(轻量级lua)
修改makefile里面的安装路径exportprefix=/usr/local/luajit
4、安装ngx_devel_kit(ndk提供函数和宏处理一些基本任务,减轻第三方模块开发的代码量)
exportluajit_lib=/usr/local/luajit/lib
exportluajit_inc=/usr/local/luajit/include/luajit-2.0
https://www.fruan.com/post/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.
makeinstall
10、upload.lua下载
主要代码
<title>fileuploadexample</title>
<formaction="emer_upload/order_system_storage"method="post"enctype="multipart/form-data">
<inputtype="file"name="testfilename"/>
<inputtype="submit"name="upload"value="upload"/>
</html>
2、upload上传代码,该模块在解析文件上传请求的过程中,主要采用了简单的类似有限状态机的算法来实现的,在不同的状态由相应的handler进行处理。
--文件下载服务写到saverootpath.."/"..filename下面
localform,err=upload:new(chunk_size)
ngx.log(ngx.err,"failedtonewupload:",err)
ngx.exit(ngx.http_internal_server_error)
localtyp,res,err=form:read()
errormsg="failedtoread:"..err
ifkey=="content-disposition"then
localkvlist=string.split(value,';')
for_,kvinipairs(kvlist)do
ifseg:find("filename")then
localkvfile=string.split(seg,"=")
filename=string.sub(kvfile[2],2,-2)

locallinuxtime=tostring(os.time())
filepath=saverootpath.."/"..linuxtime..filename
filetosave,errmsg=io.open(filepath,"w+")
--ngx.say("failedtoopenfile",filepath)
--ngx.say("failedtoopenfile",filepath..errmsg)
errormsg="打开文件失败"..filepath..errmsg
errormsg="请求参数找不到文件名字"
elseiftyp=="part_end"then
localmd5_sum=filemd5:final()
--ngx.say("md5:",str.to_hex(md5_sum))
filemd532=str.to_hex(md5_sum)
ngx.log(ngx.info,"dootherthings")
end
3、tcp接收二进制数据
localnext,val=string.unpack(tcp:receive(1),"b")
localnext,val=string.unpack(tcp:receive(2),"h");
localnext,val=string.unpack(tcp:receive(4),">i");
functionreadstring(tcp,len)
returntostring(tcp:receive(len));
end
4、tcp写二进制数据,这里和agent的通信协议是:开始标志位+包长度+json字符串+结束标志位,所以对应pack用的参数就是biab,>就是转化为大端
jsondata["filename"]=filemd532.."."..fileextension
jsondata["filesize"]=tostring(filelen)
jsondata["path"]=system.."/"..storagedate
localjsonstr=cjson.encode(jsondata)
localuilen=string.len(jsonstr)
senddata=bpack(">b1iab",startindex,uilen,jsonstr,endindex)
socket:send(senddata)
5、下载错误的时候,使用了redirect直接跳转到错误页面,方便输出错误信息,其实这里还可以做用户token校验
localerrorurl="/downloaderror.html"
errormsg="url参数解析有问题"..index
returnngx.redirect(errorurl.."?msg="..errormsg,```ngx.http_moved_temporarily)
以上就是nginx+lua搭建文件上传下载服务问题怎么解决的详细内容,更多请关注主机测评网其它相关文章!
本文来源:国外服务器--nginx安装lua
本文地址:https://www.idcbaba.com/guowai/4435.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 1919100645@qq.com 举报,一经查实,本站将立刻删除。



