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.8 tar xzf nginx_upload_module-2.0.12.tar.gz cd nginx_upload_moule-2.0.12 patchngx_http_upload_module.c davromaniak.txt cd ../nginx-1.5.8./configure --add-module=../nginx_upload_moule-2.0.12 make & sudo make install
2.配置nginx的upload-module
upstream tornadoserver{server127.0.0.1:8001; } server { listen8080; 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 controlforCORSadd_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";#10 minute pre-flight approvaladd_header Access-Control-Max-Age 600;return204;}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 controlforCORSadd_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";#10 minute pre-flight approvaladd_header Access-Control-Max-Age 600;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_cleanup400 404 499 500-505;}upload_pass @fast_upload_endpoint;# {a..z} not usefull when use zsh#mkdir {1..9} {a..z} {A..Z}upload_store/tmp/uploads 1;# set permissions on the uploaded filesupload_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;
}
}}
转载于:https://www.cnblogs.com/apexchu/p/4261324.html
tornado+nginx上传视频文件相关推荐
- tp5.1 乐视云上传视频文件(https请求http乐视云上传接口)http网址下上传视频(https API接口)
一.sdk_php_v2.0.zip 上传视频 网址:http://www.lecloud.com/zh-cn/help/api.html tp5.1 乐视云上传视频文件(https请求http乐视云 ...
- html5视频上传云,vue+七牛云上传视频文件
Qiniu-JavaScript-SDK基于七牛云存储官方 API 构建,其中上传功能基于 H5 File API.开发者基于 JS-SDK 可以方便的从浏览器端上传文件至七牛云存储,并对上传成功后的 ...
- FileUpload 上传视频文件简单实例
我们都知道 当Html input 标签类型为file时 同时将from 表单元素的enctype属性设置为 multipart/form-data 这样就可以上传文件 ,但是这种方式 只能上传文本 ...
- 【phpcms-v9】phpcms-v9上传视频文件时的解决方案
1.不建议直接在后台上传视频文件,因为视频文件一般都比较大,直接上传影响带宽:可先通过ftp工具将视频文件上传到指定目录,然后再后台引入视频文件的地址即可 2.如果在上传视频的时候,只想显示" ...
- 上传视频文件到又拍云,jsp内嵌window media player在线播放
上传视频文件到又拍云,jsp内嵌window media player在线播放 完成上述功能的后台路径 filePath.jsp(ajax)->UpYunController.java--> ...
- php上传视频文件代码,PHP视频文件上传完整示例代码
这里有新鲜揭晓的PHP面向对象编程,程序猫速率看过来! PHP开源脚本语言PHP(外文名: Hypertext Preprocessor,中文名:"超文本预处理器")是一种通用开源 ...
- GitHub 支持上传视频文件啦!
大家好,我是若川.今天周六,分享一篇热点新闻.文章较短,预计5分钟可看完. 近日 Github 宣布支持了视频上传功能,意味着,大家在提 issue 时可以携带视频了,这极大地提高了开发者和维护者的效 ...
- Nginx 上传大文件超大超时解决办法
问题:用nginx作代理服务器,上传大文件时(本人上传8M左右的文件),提示上传超时或文件过大. 浏览器调试报413 (Request Entity Too Large)错误,如下图所示 原因是ngi ...
- html上传视频文件前端显示,文件分片上传之前端文件分片
分片上传的原理就是在前端将文件分片,然后一片一片的传给服务端,由服务端对分片的文件进行合并,从而完成大文件的上传.分片上传可以解决文件上传过程中超时.传输中断造成的上传失败,而且一旦上传失败后,已经上 ...
最新文章
- 就在昨天,又一次上亿级的架构事故发生了!
- 用angr解二进制题目1
- 《大道至简》第七八章读后感
- C++初学者必看的50个建议 绝对经典
- 权限管理-整合SpringSecurity
- Git ----fatal: unable to access ‘https://gitee.com/***.git/‘: SSL certificate problem: unable
- RDS关系型数据库服务
- 远程桌面配置php,Win2008 R2实现多用户远程连接设置方法(图)
- MyEclipse IDE中的代码追踪功能
- 全触摸模式,让你尽享ipad 开发出的精品
- 达梦数据库处理用户过期密码方式
- css常见居中方法总结
- RabbitMQ 端口详解
- 判断质数、分解质因数
- win10找不到便签(便利贴)怎么办,Win10找回便签功能的方法
- xshell突然连接不上
- 【Python爬虫案例】批量采集网站壁纸,实现自动更换桌面壁纸
- IP和MNC地址协议
- Singing Contest
- 谷牛期权告诉您什么是个股期权
热门文章
- Yahoo中国变脸?
- SAP事务码f-02做账界面显示“页数”字段
- 如何在React中使用Typescript
- 手动部署OpenStack环境(三:OpenStack环境预配置)
- 往阿里云服务器上安装Mysql
- 1062 Talent and Virtue
- 学习Web前端需要避免哪些错误
- linux===Ubuntu 上安装 Node.js
- Sublime Text 3 个人使用总结
- 《ArcGIS Runtime SDK for Android开发笔记》——(13)、图层扩展方式加载Google地图...