请问nginx+lua上传文件和下载文件怎么做更好?

817 views
Skip to first unread message

Salforis

unread,
Dec 5, 2012, 9:06:39 PM12/5/12
to open...@googlegroups.com
各位,
我现在需要一个上传和下载文件的webshell,服务器上只装了lua_nginx_module。
文本文件可以直接用过body处理,可是二进制文件就不行了?
各位有什么好办法?
拜谢各位了。。。

wgm.china

unread,
Dec 5, 2012, 9:13:31 PM12/5/12
to openresty
为什么不能直接处理二进制的文件,我上传jpg图片很正常阿
 
2012-12-06

wgm.china

发件人:Salforis
发送时间:2012-12-06 10:06
主题:[openresty] 请问nginx+lua上传文件和下载文件怎么做更好?
抄送:
 
--  
邮件自: 列表“openresty”,专用于技术讨论! 
发言: 请发邮件到 open...@googlegroups.com 
退订: 请发邮件至 openresty+...@googlegroups.com 

Salforis

unread,
Dec 5, 2012, 9:18:08 PM12/5/12
to open...@googlegroups.com
你是怎么传的?我只知道用ngx.req.get_body_data,还有其他方法吗?

lhmwzy

unread,
Dec 6, 2012, 12:25:25 AM12/6/12
to open...@googlegroups.com
http://blog.163.com/lhmwzy@126/blog/static/64215736201210209285365/
这是我之前写的,希望能对你有帮助。


2012/12/6 Salforis <mid...@gmail.com>

agentzh

unread,
Dec 6, 2012, 12:35:43 AM12/6/12
to open...@googlegroups.com
Hello!

On Wed, Dec 5, 2012 at 6:18 PM, Salforis wrote:
> 你是怎么传的?我只知道用ngx.req.get_body_data,还有其他方法吗?
>

一个常见的错误是你在调用 ngx.req.get_body_data() 之前没有调用 ngx.req.read_body() 函数。

如果你确实调用了 ngx.req.read_body(),但 ngx.req.get_body_data() 仍然返回
nil,此时则很可能是因为请求体的大小因为超出了你的 client_body_buffer_size 的配置从而被缓冲到了临时文件中,见:

http://wiki.nginx.org/HttpCoreModule#client_body_buffer_size

此时你可以进一步调用 ngx.req.get_body_file() 得到该临时文件路径,然后在 Lua 中读取此文件的内容,见

http://wiki.nginx.org/HttpLuaModule#ngx.req.get_body_file

如果你压根不想让请求体被缓冲到文件系统,则可以在 nginx.conf 配置文件中将 client_body_buffer_size
设置成和 client_max_body_size 一样大,见:

http://wiki.nginx.org/HttpCoreModule#client_max_body_size

当然,如果你希望需要流式地读取请光体,则不应使用 ngx.req.read_body(), ngx.req.get_body_data()
或者 ngx.req.get_body_file(),而应当使用 ngx.req.socket():

http://wiki.nginx.org/HttpLuaModule#ngx.req.socket

Best regards,
-agentzh

Salforis

unread,
Dec 6, 2012, 12:45:23 AM12/6/12
to open...@googlegroups.com
ngx.req.get_body_data() 之前调用 ngx.req.read_body() 了,也读到数据了。
只是我读到的是文本,这个手动处理文件内容之前的头,不过如果是二进制文件,我要怎样处理,把头去掉,直接保存文件内容?

上面是上传文件,下载文件是要一点一点的把文件读到缓冲区,然后用什么方法发送给客户端?

kindy

unread,
Dec 6, 2012, 1:04:40 AM12/6/12
to openresty
文件上传请参考 lhmwzy 提供的地址。应该足够的。

文件下载,建议添加 acl 后,让nginx以静态文件的方式来下载。
如果文件不在本地,就用 lua 读取一部分 ngx.print(content); ngx.flush(true) 的来做。



2012/12/6 Salforis <mid...@gmail.com>



--
- - - - - - - - - - - -
林青(Kindy Lin)

Salforis

unread,
Dec 6, 2012, 9:22:32 AM12/6/12
to open...@googlegroups.com
好的,谢谢各位,我试试看!

Salforis

unread,
Dec 6, 2012, 11:43:07 AM12/6/12
to open...@googlegroups.com
怎样才能让nginx以静态文件形式下载,我要下载的文件目录是不确定的,不一定在web的root目录下面,有专门的方法可以下载吗?


在 2012-12-6,下午2:04,kindy <kin...@gmail.com> 写道:

kindy

unread,
Dec 6, 2012, 9:32:48 PM12/6/12
to openresty
location /download/ {
    alias /any/path/to/files/
}

/download/id/1234 -> /any/path/to/files/id/1234



2012/12/7 Salforis <mid...@gmail.com>

lhmwzy

unread,
Dec 6, 2012, 9:42:21 PM12/6/12
to open...@googlegroups.com
我估计他的意思是目录不确定?

kindy

unread,
Dec 6, 2012, 9:52:21 PM12/6/12
to openresty
那我提供一个危险的做法 (切记要做好 acl 哈)

location /any-download/ {
    alias /;
    internal;
}

location /download/ {
    rewrite_by_lua "
    if (has_access()) {
        ngx.set_uri('/any-download/path/to/file', true);
    }
";
}


2012/12/7 lhmwzy <lhm...@gmail.com>

kindy

unread,
Dec 6, 2012, 9:52:57 PM12/6/12
to openresty
请详细参考相关文档 http://wiki.nginx.org/HttpLuaModule#ngx.req.set_uri


2012/12/7 kindy <kin...@gmail.com>

Salforis

unread,
Dec 6, 2012, 10:31:18 PM12/6/12
to open...@googlegroups.com
帅气! 要的就是这个方法!

多谢..

lhmwzy

unread,
Dec 7, 2012, 12:38:09 AM12/7/12
to open...@googlegroups.com
如有可能,把全部代码开源一下?

Salforis

unread,
Dec 7, 2012, 3:18:22 AM12/7/12
to open...@googlegroups.com
我只是做一个web shell,给开发抓日志用,不对公网暴露的,还在写。
Reply all
Reply to author
Forward
0 new messages