tornado+nginx上传视频文件
[http://arloz.me/tornado/2014/06/27/uploadvideotornado.html]
[NGINX REFRER:Nginx upload module]
由于tornado通过表达上传的数据最大限制在100M,所以如果需要上传视屏文件的情况在需要通过其他方式实现, 此处采用nginx的nginx-upload-module和jQuery-File-Upload实现。
1.编译安装nginx-upload-module
- 下载nginx-1.5.8
- 下载nginx-upload-module2.0
- 由于nginx-upload-module不支持最新版的nginx,直接编译会出错,需要打补丁 davromaniak.txt
tar xzf nginx-1.5.
tar xzf nginx_upload_module-2.0..tar.gz
cd nginx_upload_moule-2.0.
patch ngx_http_upload_module.c davromaniak.txt
cd ../nginx-1.5.
./configure --add-module=../nginx_upload_moule-2.0.
make & sudo make install
2.配置nginx的upload-module
upstream tornadoserver{
server 127.0.0.1:;
}
server {
listen ;
server_name localhost;
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://tornadoserver;
}
client_max_body_size 4G;
client_body_buffer_size 1024k; if ($host !~* ^(localhost) ) {
}
location = /uploads {
if ($request_method = OPTIONS) {
add_header Pragma no-cache;
add_header X-Content-Type-Options nosniff; # Access control for CORS
add_header Access-Control-Allow-Origin "http://localhost";
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
add_header Access-Control-Allow-Headers "cache-control, content-range, accept,\
origin, session-id, content-disposition, x-requested-with, ctent-type,\
content-description, referer, user-agent";
add_header Access-Control-Allow-Credentials "true";
# minute pre-flight approval
add_header Access-Control-Max-Age ;
return ;
}
if ($request_method = POST) {
add_header Pragma no-cache;
add_header X-Content-Type-Options nosniff;
#add_header Cache-control "no-story, no-cache, must-revalidate"; # Access control for CORS
add_header Access-Control-Allow-Origin "http://localhost";
add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS";
add_header Access-Control-Allow-Headers "cache-control, content-range, accept,\
origin, session-id, content-disposition, x-requested-with,\
content-type, content-description, referer, user-agent";
add_header Access-Control-Allow-Credentials "true";
# minute pre-flight approval
add_header Access-Control-Max-Age ; upload_set_form_field $upload_field_name.name "$upload_file_name";
upload_set_form_field $upload_field_name.content_type "$upload_content_type";
upload_set_form_field $upload_field_name.path "$upload_tmp_path"; upload_pass_form_field "^X-Progress-ID$|^authenticity_token$";
upload_cleanup -;
} upload_pass @fast_upload_endpoint; # {a..z} not usefull when use zsh
# mkdir {..} {a..z} {A..Z}
upload_store /tmp/uploads ; # set permissions on the uploaded files
upload_store_access user:rw group:rw all:r;
}
location @fast_upload_endpoint {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass_header 'Access-Control-Allow-Origin';
proxy_pass http://tornadoserver;
}
}
3.demo页面
<!DOCTYPE HTML>
<html>
<head> <meta charset="utf-8">
<title>jQuery File Upload Example</title>
<style>
.bar {
height: 18px;
background: green;
}
</style></head>
<body>
<input id="fileupload" type="file" name="files[]" data-url="uploads" multiple>
<script src=""></script>
<script src="/static/js/vendor/jquery.ui.widget.js"></script>
<script src="/static/js/jquery.iframe-transport.js"></script>
<script src="/static/js/jquery.fileupload.js"></script>
<script>
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
done: function (e, data) {
$.each(data.result.files, function (index, file) {
$('<p/>').text(file.name+" "+file.size).appendTo(document.body);
});
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .bar').css( 'width', progress + '%');
}
});
});
</script>
<div id="progress">
<div class="bar" style="width: 10%;"></div>
</div>
</body>
</html>
4.tornado处理
当nginx的upload-module完成视频文件的传输之后,其会设置表单数据,并转发给后台tornado服务器处理。
通过如下方式获得相关参数:
name = self.get_argument("files[].name","")
content_type = self.get_argument("files[].content_type","")
oldpath = self.get_argument("files[].path","")
size = os.path.getsize(oldpath)
files = []
files.append({"name":name,"type":content_type,"size":size})
ret = {"files":files}
self.write(tornado.escape.json_encode(ret))
5.其它配置文件参考
user root;
worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
upload_resumable on;
client_max_body_size 100m;
listen 8888;
server_name localhost;
# Upload form should be submitted to this location
location /upload {
# Pass altered request body to this location
upload_pass @test;
upload_store /tmp 1; upload_store_access user:r; upload_set_form_field $upload_field_name[name] "$upload_file_name";
upload_set_form_field $upload_field_name[content_type] "$upload_content_type";
upload_set_form_field $upload_field_name[path] "$upload_tmp_path"; upload_aggregate_form_field "$upload_field_name[md5]" "$upload_file_md5";
upload_aggregate_form_field "$upload_field_name[size]" "$upload_file_size";
upload_pass_form_field "^submit$|^description$";
upload_cleanup 400 404 499 500-505;
} # Pass altered request body to a backend
location @test {
proxy_pass http://mongrel;
} } server{
listen 30091;
root /var/www/sms/public/video;
} upstream mongrel {
server 127.0.0.1:3001;
server 127.0.0.1:3002;
server 127.0.0.1:3003;
} server {
upload_resumable on;
client_max_body_size 100m;
listen 3000; root /var/www/sms/public;
access_log /var/www/sms/log/nginx.access.log; if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html last;
break;
} #upload video
location /upload_video {
# Pass altered request body to this location
upload_pass @rails;
upload_store /tmp 1; upload_store_access user:r; upload_set_form_field $upload_field_name[name] "$upload_file_name";
upload_set_form_field $upload_field_name[content_type] "$upload_content_type";
upload_set_form_field $upload_field_name[path] "$upload_tmp_path"; upload_aggregate_form_field "$upload_field_name[md5]" "$upload_file_md5";
upload_aggregate_form_field "$upload_field_name[size]" "$upload_file_size";
upload_pass_form_field "^submit$|^description$";
upload_cleanup 400 404 499 500-505;
} # Upload image
location /upload_image {
# Pass altered request body to this location
upload_pass @rails;
upload_store /tmp 1; upload_store_access user:r; upload_set_form_field $upload_field_name[name] "$upload_file_name";
upload_set_form_field $upload_field_name[content_type] "$upload_content_type";
upload_set_form_field $upload_field_name[path] "$upload_tmp_path"; upload_aggregate_form_field "$upload_field_name[md5]" "$upload_file_md5";
upload_aggregate_form_field "$upload_field_name[size]" "$upload_file_size";
upload_pass_form_field "^submit$|^description$";
upload_cleanup 400 404 499 500-505;
} # Pass altered request body to a backend
location @rails {
proxy_pass http://mongrel;
} location / {
proxy_set_header X-Real-IP $remote_addr; # needed for HTTPS
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_max_temp_file_size 0; if (-f $request_filename) {
break;
} if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
} if (-f $request_filename.html) {
rewrite (.*) $1.html break;
} if (!-f $request_filename) {
proxy_pass http://mongrel;
break;
}
} error_page 500 502 503 504 /500.html;
location = /500.html {
root /var/www/sms/public;
}
} }
tornado+nginx上传视频文件的更多相关文章
- 以springMVC为例获取上传视频文件时长
毕设项目是一个在线学习系统,教师用户有上传视频的功能,在答辩之前赶了一个demo出来,好多功能都写死了,比如课程学习进度就是被我写死在前端的一个变量,最近导师要我把项目打包发给他,这才心慌慌赶紧把这些 ...
- plupload分片上传视频文件源码展示
plupload分片上传视频文件目录结构如下: |- images//视频上传小图片 |-js// plupload js文件 |-uploads//视频文件存放文件夹 里面是按日期存放 |-ajax ...
- java上传视频文件
需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在500M内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以501M来进行限制. 第一步: 前端修改 由于项目使用的是 ...
- 利用bootstrap上传视频文件,mvc做后台处理
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...
- DJANGO技巧两则:模拟MKDIR -P及配合NGINX上传大文件不使超时
这都是在开发当哪遇到的问题,网上转转,作个记录: http://blog.chinaunix.net/uid-25525723-id-1596574.html http://bookshadow.co ...
- 使用HttpUtils 上传视频文件
private void shangchuan(){ //文件的路径 //File file=new File(path); File fi ...
- h5上传视频文件
从一开始我就掉坑里了,<input type="file" style="display: block;" id="img-upload&quo ...
- nginx上传大文件,413错误解决
在nginx里增加了配置. client_max_body_size 500m; proxy_connect_timeout 600; proxy_read_timeout 600; proxy_se ...
- 使用resumable.js上传大文件(视频)兵转换flv格式
前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Video.asp ...
随机推荐
- Fiddler2 模拟文件上传
最近遇到一个需求,需要上传音频文件, 服务端使用webService 通过spring3 进行文件上传.代码完成后使用 html 通过post 方式请求接口成功了,但不知道如何使用Fiddler2工具 ...
- mysql 常见参数
my.cnf[client] 对mysql的所有客端都生效的[mysql] 只对mysql这个命令有效了[mysqd][mysqld_multi] 多实例启动[mysqld_safe][mysqldN ...
- timequest学习之黑金动力(一)
黑金动力的资料还是非常有价值的.通过建模篇,对于给定的时序关系,我总能实现.但是,这总是很初级的能力.也只是为后面的建模服务.所以,现阶段我的能力还是非常有限.我相信我一定会成为牛人,能够独挡一面.借 ...
- Linux - 目录结构及文件操作
根目录 “/”:Linux 系统中最高层的目录 这个就是根目录 用 / 表示根目录 bin 目录:存放可执行文件 bin 目录下的文件都是平常使用的命令 在 Linux 系统中,一切都是文件 sbin ...
- Python Twisted系列教程14:Deferred用于同步环境
作者:dave@http://krondo.com/when-a-deferred-isnt/ 译者:杨晓伟(采用意译) 你可以从这里从头开始阅读这个系列. 介绍 这部分我们要介绍Deferred的 ...
- CSS——优先级
转自:http://www.planabc.net/2008/05/06/css_specificity/ CSS2.1 中规定了关于 CSS 规则 Specificity(特异性)的计算方式,用一个 ...
- leetcode796
public class Solution { public bool RotateString(string A, string B) { string temp = A; int len = A. ...
- ffmpeg码率控制
一.VBR与CBR的含义和区别 VBR是动态码率.CBR是静态码率. VBR(Variable Bitrate)动态比特率.也就是没有固定的比特率,压缩软件在压缩时根据音频数据即时确定使用什么比特率, ...
- 辽宁工程技术大学校园网(深澜) 叠加小水管提速,多wan叠加负载均衡
教程没啥大用了.可以直接修改路由器为DHCP自动获取ip,然后直接登录校园网. 昨天晚上尝试了下用潘多拉固件多wan叠加网速,负载均衡,算是提高了速度. 转载请注明出处.教程供参考.本教程不是破解教程 ...
- 201671010140. 2016-2017-2 《Java程序设计》java学习第八周
第八周Java学习 本周,老师带领我们完善了一下继承,借口,拷贝,lambda表达式,内部类方面欠缺,不完善的地方,帮助我们查漏补缺. 以拷贝的学习为例,我本来对拷贝的理解非常浅 ...