让nginx支持文件上传的几种模式
文件上传的几种不同语言和不同方法的总结。
第一种模式 : PHP 语言来处理
这个模式比较简单, 用的人也是最多的, 类似的还有用 .net 来实现, jsp来实现, 都是处理表单。只有语言的差别, 本质没有任何差别。
file.php 文件内容如下 :
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
?>
测试命令 :
curl -F "action=file.php" -F "file=@xxx.c" http://192.168.1.162/file.php
这样就可以把本地文件 xxx.c 通过表单的形式提交到服务器, file.php文件就会处理该表单。
第二种模式: lua 语言来处理
这种模式需要用 ngx_lua 模块的支持, 你可以直接下载 ngx_openresty 的源码安装包, 该项目由春哥负责。
春哥为了处理 文件上传, 还专门写了个lua的 upload.lua 模块。
网址为 https://github.com/agentzh/lua-resty-upload 大家可以下载, 里面只用到 upload.lua 文件即可, 把这个文件放到
/usr/local/openresty/lualib/resty/ 这个目录即可(该目录是缺省安装的目录, ./configure --prefix=/usr 可以改变目录)
下来写一个 savefile.lua 的文件来处理上传上来的文件, 文件内容如下 :
package.path = '/usr/local/share/lua/5.1/?.lua;/usr/local/openresty/lualib/resty/?.lua;'
package.cpath = '/usr/local/lib/lua/5.1/?.so;' local upload = require "upload" local chunk_size = 4096
local form = upload:new(chunk_size)
local file
local filelen=0
form:set_timeout(0) -- 1 sec
local filename function get_filename(res)
local filename = ngx.re.match(res,'(.+)filename="(.+)"(.*)')
if filename then
return filename[2]
end
end local osfilepath = "/usr/local/openresty/nginx/html/"
local i=0
while true do
local typ, res, err = form:read()
if not typ then
ngx.say("failed to read: ", err)
return
end
if typ == "header" then
if res[1] ~= "Content-Type" then
filename = get_filename(res[2])
if filename then
i=i+1
filepath = osfilepath .. filename
file = io.open(filepath,"w+")
if not file then
ngx.say("failed to open file ")
return
end
else
end
end
elseif typ == "body" then
if file then
filelen= filelen + tonumber(string.len(res))
file:write(res)
else
end
elseif typ == "part_end" then
if file then
file:close()
file = nil
ngx.say("file upload success")
end
elseif typ == "eof" then
break
else
end
end
if i==0 then
ngx.say("please upload at least one file!")
return
end
我把上面这个 savefile.lua 文件放到了 nginx/conf/lua/ 目录中
nginx.conf 配置文件中添加如下的配置 :
location /uploadfile
{
content_by_lua_file 'conf/lua/savefile.lua';
}
用下面的上传命令进行测试成功
curl -F "action=uploadfile" -F "file=@abc.zip" http://127.0.0.1/uploadfile
第三种模式: perl 语言来处理
编译 nginx 的时候 用 --with-http_perl_module 让其支持perl脚本
然后用perl来处理表单, 这种模式我还没做测试, 如果有那位弟兄试验过, 帮我补充一下。
下面的代码为 PERL 提交表单的代码:
use strict;
use warnings;
use WWW::Curl::Easy;
use WWW::Curl::Form; my $curl = WWW::Curl::Easy->new;
my $form = WWW::Curl::Form->new; $form->formaddfile("11game.exe", 'FILE1', "multipart/form-data");
# $form->formadd("FIELDNAME", "VALUE");
$curl->setopt(CURLOPT_HTTPPOST, $form); $curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_URL, 'http://127.0.0.1/uploadfile'); # A filehandle, reference to a scalar or reference to a typeglob can be used here.
my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body); # Starts the actual request
my $retcode = $curl->perform; # Looking at the results...
if ($retcode == 0)
{
print("Transfer went ok\n");
my $response_code = $curl->getinfo(CURLINFO_HTTP_CODE);
# judge result and next action based on $response_code
print("Received response: \n$response_body\n");
}
else
{
# Error code, type of error, error message
print("An error happened: $retcode ".$curl->strerror($retcode)." ".$curl->errbuf."\n");
}
服务器端的代码我不是用perl CGI, 而是嵌入nginx 的 perl脚本, 所以处理表单的代码还没有测试通过,等有时间了在研究一下。
第四种模式:用 http 的dav 模块的 PUT 方法
编译 nginx 的时候 用 --with-http_dav_module 参数让其支持 dav 模式
nginx.conf文件中配置如下 :
location / {
client_body_temp_path /usr/local/openresty/nginx/html/tmp;
dav_methods PUT DELETE MKCOL COPY MOVE;
create_full_put_path on;
dav_access group:rw all:r;
root html;
#index index.html index.htm;
}
用下面的命令进行测试可以成功 :
curl --request PUT --data-binary "@11game.exe" --header "Content-Type: application/octet-stream" http://127.0.0.1/game.exe
其中11game.exe为上传的本地文件。
只有第四种是 PUT 方法, 其他的三种都属于 POST 表单的方法。
让nginx支持文件上传的几种模式的更多相关文章
- 文件上传的三种模式-Java
文件上传的三种方式-Java 前言:因自己负责的项目(jetty内嵌启动的SpringMvc)中需要实现文件上传,而自己对java文件上传这一块未接触过,且对 Http 协议较模糊,故这次采用渐进的方 ...
- Openresty + nginx-upload-module支持文件上传
0. 说明 这种方式其实复杂,麻烦!建议通过这个方式搭建Openresty文件上传和下载服务器:http://www.cnblogs.com/lujiango/p/9056680.html 1. 包下 ...
- RPC基于http协议通过netty支持文件上传下载
本人在中间件研发组(主要开发RPC),近期遇到一个需求:RPC基于http协议通过netty支持文件上传下载 经过一系列的资料查找学习,终于实现了该功能 通过netty实现文件上传下载,主要在编解码时 ...
- nginx实现文件上传和下载
nginx实现文件上传和下载 发布时间:2020-06-05 16:45:27 来源:亿速云 阅读:156 作者:Leah 栏目:系统运维 这篇文章给大家分享的是nginx实现文件上传和下载的方法.小 ...
- java nio 写一个完整的http服务器 支持文件上传 chunk传输 gzip 压缩 使用过程 和servlet差不多
java nio 写一个完整的http服务器 支持文件上传 chunk传输 gzip 压缩 也仿照着 netty处理了NIO的空轮询BUG 本项目并不复杂 代码不多 ...
- curl文件上传有两种方式,一种是post_fileds,一种是infile
curl文件上传有两种方式,一种是POSTFIELDS,一种是INFILE,POSTFIELDS传递@实际地址,INFILE传递文件流句柄! );curl_setopt($ch, CURLOPT_PO ...
- springmvc学习笔记--支持文件上传和阿里云OSS API简介
前言: Web开发中图片上传的功能很常见, 本篇博客来讲述下springmvc如何实现图片上传的功能. 主要讲述依赖包引入, 配置项, 本地存储和云存储方案(阿里云的OSS服务). 铺垫: 文件上传是 ...
- 设置nginx中文件上传的大小限制度
通过设置nginx的client_max_body_size解决nginx+php上传大文件的问题: 用nginx来做webserver的时,上传大文件时需要特别注意client_max_body_s ...
- 让UpdatePanel支持文件上传(2):服务器端组件 .
我们现在来关注服务器端的组件.目前的主要问题是,我们如何让页面(事实上是ScriptManager控件)认为它接收到的是一个异步的回送?ScriptManager控件会在HTTP请求的Header中查 ...
随机推荐
- WPF常用控件应用demo
WPF常用控件应用demo 一.Demo 1.Demo截图如下: 2.demo实现过程 总体布局:因放大缩小窗体,控件很根据空间是否足够改变布局,故用WrapPanel布局. <ScrollVi ...
- LINQ 101——约束、投影、排序
什么是LINQ:LINQ 是一组 .NET Framework 扩展模块集合,内含语言集成查询.集合以及转换操作.它使用查询的本机语言语法来扩展 C# 和 Visual Basic,并提供利用这些功能 ...
- Codevs 1140 Jam的计数法 2006年NOIP全国联赛普及组
1140 Jam的计数法 2006年NOIP全国联赛普及组 传送门 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Jam是个喜欢标 ...
- 百练_2409 Let it Bead(Polya定理)
描述 "Let it Bead" company is located upstairs at 700 Cannery Row in Monterey, CA. As you ca ...
- C# 绘制窗体客户非客户区要用WM_PAINT和WM_NCPAINT
窗体分为两部分:客户区(Client area)和非客户区(Non-Client area) WM_PAINT消息.OnPaint()方法.GetDC()API函数都是处理窗体客户区绘制的 而标题 ...
- ACM HDU Primes(素数判断)
Problem Description Writea program to read in a list of integers and determine whether or not eachnu ...
- Linux 多用户和多用户边界
1. 需求背景 2. 多用户的边界: 独立的工作目录 3. 多用户的边界:可操作/访问的资源 4. 多用户的边界: 可执行的操作 5. 多用户的特性标识: UID和GID -------------- ...
- Python核心编程2第三章课后练习
1. 标识符.为什么Python 中不需要变量名和变量类型声明? Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程.每个变量在内存中创建,都包括变量的标识,名称和数据这些信息.每 ...
- JDK源码阅读(二) AbstractList
package java.util; public abstract class AbstractList<E> extends AbstractCollection<E> i ...
- Spring4.0整合Hibernate3 .6
转载自:http://greatwqs.iteye.com/blog/1044271 6.5 Spring整合Hibernate 时至今日,可能极少有J2EE应用会直接以JDBC方式进行持久层访问. ...