nginx+lua搭建文件上传下载服务
主要逻辑
上传
前端请求nginx服务,nginx调用upload脚本,脚本通过查找配置,找到对应的逻辑存储路径和物理存储机器的agent的ip和端口,通过tcp发包到对应agent,部署在对应机器的agent接受数据,并写到本地文件。
下载
http下载请求nginx,nginx调用download脚本,脚本解析链接参数,根据参数找到对应的agent地址,请求返回文件二进制内容,脚本接受到agent返回的数据,返回给请求端。
配置Nginx+lua
接下来主要讲一下nginx安装配置(这里包括lua的二进制流处理lpack,md5计算,mysql操作,json操作)
1、安装nginx 下载http://nginx.org/en/download.html 解压tar -xvf nginx-1.10.3.tar.gz
2、安装luajit(轻量级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提供函数和宏处理一些基本任务,减轻第三方模块开发的代码量)下载https://github.com/simpl/ngx_devel_kit/
5、安装编译 ,导入
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
如果报错找不到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
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 可以用现成的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
10、upload.lua 下载https://github.com/openresty/lua-resty-upload
11、md5下载 https://github.com/openresty/lua-resty-string
主要代码
1、前端上传页面代码
<!DOCTYPE html>
<html><head><title>File upload example</title></head><body><formaction="emer_upload/order_system_storage"method="post"enctype="multipart/form-data"><inputtype="file"name="testFileName"/><inputtype="submit"name="upload"value="Upload"/></form></body>
</html>
2、upload上传代码,该模块在解析文件上传请求的过程中,主要采用了简单的类似有限状态机的算法来实现的,在不同的状态由相应的 handler 进行处理。
--文件下载服务写到 saveRootPath .."/" .. filename 下面
functionDownLoad()local chunk_size = 4096local form,err=upload:new(chunk_size)if not form thenngx.log(ngx.ERR, "failed to new upload: ", err)ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)end form:set_timeout(100000)while true dolocal typ,res,err=form:read()if not typ thenErrorMsg="failed to read :"..errreturn 1endif typ =="header" thenlocal key=res[1]local value=res[2]if key =="Content-Disposition" thenlocal kvlist=string.split(value,';')for _, kv in ipairs(kvlist) dolocal seg = string.trim(kv)if seg:find("filename") thenlocal 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..filenamefileToSave,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 .. errmsgreturn 1endelseErrorMsg="请求参数找不到文件名字"return 1end--跳出循环break endendendelseif typ =="body" thenif fileToSave thenfileToSave:write(res)fileMd5:update(res)endelseif typ =="part_end" thenif fileToSave thenlocal md5_sum=fileMd5:final()--ngx.say("md5: ", str.to_hex(md5_sum))fileMD532=str.to_hex(md5_sum)fileToSave:close()fileToSave = nilend elseif typ =="eof" thenbreakelsengx.log(ngx.INFO, "do other things")endendreturn 0
end
3、tcp接收二进制数据
-- 读取byte
functionreadInt8(tcp)local next, val = string.unpack(tcp:receive(1), "b")return tonumber(val);
end
-- 读取int16
functionreadInt16(tcp)local next, val = string.unpack(tcp:receive(2), "h");return tonumber(val);
end
-- 读取int32
functionreadInt32(tcp)local next, val = string.unpack(tcp:receive(4), ">i");return tonumber(val);
end
-- 读取字符串
functionreadString(tcp,len)return tostring(tcp:receive(len));
end
4、tcp写二进制数据,这里和agent的通信协议是:开始标志位+包长度+json字符串+结束标志位,所以对应pack用的参数就是bIAb ,>就是转化为大端
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、下载错误的时候,使用了redirect直接跳转到错误页面,方便输出错误信息,其实这里还可以做用户token校验
local ErrorUrl="/downloadError.html"
ErrorMsg="url 参数解析有问题 "..index
return ngx.redirect(ErrorUrl.."?msg="..ErrorMsg, ngx.HTTP_MOVED_TEMPORARILY)
nginx+lua搭建文件上传下载服务相关推荐
- nginx搭建文件上传下载服务器
最近要搭建文件一个服务器, 准备使用python的web框架tornado来做.发现了这篇blog,准备使用nginx来实现文件上传和下载的功能.下面把实现步骤记录下来,中间有很多坑. 系统环境:ce ...
- java文件上传下载服务并发超时,先收藏了
一.阿里 (会员中台) 1. Java 重写hashcode()是否需要重写equals(),不重写会有什么后果 2. 并发 自旋锁和阻塞锁的区别 公平锁和非公平锁的区别 jdk中哪种数据结构或工具可 ...
- java文件上传下载服务并发超时,震撼来袭免费下载!
前言 正值金九银十面试旺季,现在的面试官一天少说得看几百份简历,你投出去的简历如果没有特别吸引人的点和新颖突出,毫无疑问你的简历不是在垃圾桶就是在回收站里边待着了,那么什么样的简历才能吸引到面试官的眼 ...
- linux上存的私钥文件不能访问_linux中配置vsftpd服务器匿名用户访问,实现文件上传下载服务...
1.安装vsftpd服务器 安装VSFTPD 安装更新完毕 2.添加防火墙使用规则 添加防火墙iptables使用规则 iptables -F 查看iptables使用情况 添加前查看防火墙列表 添加 ...
- WebService实现文件上传下载
一:服务端:一个普通java web工程 package com.wzh.file;import com.sun.xml.ws.developer.StreamingAttachment; impor ...
- webservice文件上传下载
使用DataHandler实现webservice的文件上传下载 服务端代码: package com.hello.weChat.controller;import javax.activation. ...
- 微信小程序开发之文件上传下载应用场景(附Demo源码)
微信小程序开发之文件上传下载应用场景(附Demo源码),Demo为小相册应用,源码在附件中,本示例需要腾讯云支持. http://www.henkuai.com/forum.php?mod=viewt ...
- xftp实现本地与服务器的文件上传下载(windows)
背景: Jemter环境搭建,需上传下载服务器文件到aws服务器上,由于secureCRT的局限性它只支持pub格式的密钥,不支持pem格式密钥,xshell是支持pem格式的,所以尝试安装xshel ...
- JSON+AJAX+ThreadLocal+文件上传下载
文章目录 JSON和AJAX文档介绍 1. JSON介绍 1.1 JSON快速入门 1.2 JSON和字符串转换 1.2.1 JSON转字符串 1.2.2 字符串转JSON 1.2.3 JSON和字符 ...
- 渗透测试-文件上传/下载/包含
渗透测试-文件上传/下载/包含 概述 一.常见校验上传文件的方法 客户端校验 服务器端校验 1.校验请求头 content-type字段 2. 正则匹配来判断文件幻数(文件头)内容是否符合要求 3. ...
最新文章
- Windows 2008 VS2008 IIS7 中调试Asp.net 2.0 两个问题
- OpenCV 获取并修改图中的像素点
- 障碍物规避算法的MATLAB仿真
- 应用系统开发思想的变迁
- Facebook力推导航库:React Navigation使用详解
- LeetCode 388. 文件的最长绝对路径(不用栈,前缀和)
- poj 3131 Cubic Eight-Puzzle 双向广搜 Hash判重
- B2憨宝275版本暖岛正版子主题-wordpress主题
- java连接打印机访问被拒绝_java – 尝试访问spring security中的登录页面时访问被拒绝的异常...
- win7 OEM设置工具
- vue element-ui只有一条信息时默认选中按钮,且不能取消,多条信息时可以手动选择
- iOS开发之 WebView
- python 饼图代码_Python中使用Matplotlib画出饼图的代码实例
- C语言中插入法的应用
- 天神娱乐:因资金状况紧张 未能清偿部分到期债务
- 阿里云企业实名认证教程
- python将中文数字转化成阿拉伯数字
- Jetpack-Compose-自定义绘制
- 采集绿盾网的10个经典方法
- C语言学习day01