In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article "nginx+lua build file upload download service how to solve the problem" Most people do not understand the knowledge points of the article, so Xiaobian summarized the following content for everyone, the content is detailed, the steps are clear, and it has certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "nginx+lua build file upload download service how to solve the problem".
main logic
upload
The front-end requests nginx services, and nginx calls upload scripts. The scripts find the corresponding logical storage path and ip and port of the agent of the physical storage machine by searching the configuration, send the packets to the corresponding agent through tcp, and deploy the agent on the corresponding machine to receive the data and write it to the local file.
download
http download request nginx, nginx calls the download script, the script parses the link parameters, finds the corresponding agent address according to the parameters, requests to return the binary content of the file, the script receives the data returned by the agent, and returns it to the requester.
Configure nginx+lua
Next, let's talk about nginx installation configuration (including lua binary stream processing lpack, md5 calculation, mysql operation, json operation)
1. Install nginx
download
Unzip tar -xvf nginx-1.10.3.tar.gz
2. Install luajit (lightweight lua)
Change the installation path in makefile export prefix= /usr/local/luajit
Then install make &make install
3, Install nginx_lua_module
download
decompression
Install ngx_devel_kit (ndk provides functions and macros to handle some basic tasks, reducing the amount of code developed by third-party modules)
download
5. Installation, compilation and import
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
Start/usr/local/nginx/sbin/nginx restart command ` usr/local/nginx/sbin/nginx -s reload v
The requested URL/usr/local/lib/libluajit-5.1.so.2 /lib/64/libluajit-5.1.so.2 was not found on this server.
Test nginx directly open the browser can http:10.x.x.x:8080 can see the welcome interface
6, Configure conf/nginx.conf Run lua script
Add lua_package_path, lua_package_cpath for lua library
7. Add mysql.lua download and copy to lua_package_path directory.
8. Add csjon
Modify prefix=/usr/local/luajit in makefile to be luajit installation path, copy generated cjson.so to
lua_package_cpath directory
9. To install lpack, you can copy the ready-made lpack.lua to lua_package_path or compile it with https://github.com/luadist/lpack to generate lpack.so and copy it to lua_package_cpath 64 bits. You need to add the compilation command-fpic
10. upload.lua Download
11, md5 download
major code
1. The front-end uploads the page code
file upload example
2. Upload upload code. In the process of parsing file upload request, this module mainly adopts simple algorithm similar to finite state machine to realize, and different states are processed by corresponding handler.
--File download service writes saverotpath.. "/" .. 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(10000) 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 --Get file suffix name fileextension=getextension(filename) local linuxtime=tostring(os.time()) filepath=saverootpath .. "/" .. linuxtime.. filename filetosave,errmsg = io.open(filepath, "w+") --File path stored --ngx.say("failed to open file ", filepath) if not filetosave then --ngx.say("failed to open file ", filepath .. errmsg) errormsg="Failed to open file".. filepath .. errmsg return 1 end else errormsg="File name not found for request parameters" return 1 end --Out of the loop 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 0end
TCP receives binary data
--read bytefunction readint8(tcp) local next, val = string.unpack (tcp:receive(1), "b") return tonumber(val);end--read int16function readint16(tcp) local next, val = string.unpack (tcp:receive(2), "h"); return tonumber(val);end--read int32function readint32(tcp) local next, val = string.unpack (tcp:receive(4), ">i"); return tonumber(val);end--read string function readstring(tcp,len) return tostring(tcp:receive(len));end
4, tcp writes binary data, where the communication protocol with the agent is: start flag + packet length +json string + end flag, so the parameter corresponding to pack is biab,> is converted to big endian
jsondata["filename"]=filemd532 .. ". " .. fileextensionjsondata["cmd"]="write"jsondata["filesize"]=tostring(filelen)jsondata["path"]=system.. "/".. storagedatelocal jsonstr=cjson.encode(jsondata)local uilen=string.len(jsonstr)senddata=bpack(">b1iab",startindex,uilen,jsonstr,endindex)socket:send(senddata)
5. When downloading an error, use redirect to jump directly to the error page to facilitate the output of error messages. In fact, you can also do user token verification here.
local errorurl="/downloaderror.html"errormsg="url parameter parsing problem".. indexreturn ngx.redirect(errorurl.. "? msg=".. errormsg,`` ngx.http_moved_temporarily) The above is about the content of this article on "How to solve the problem of uploading and downloading files built by nginx+lua". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.