tornado+nginx上传视频文件

[http://arloz.me/tornado/2014/06/27/uploadvideotornado.html]

[NGINX REFRER: Nginx upload module]

由于tornado通过表达上传的数据最大限制在100M,所以如果需要上传视屏文件的情况在需要通过其他方式实现, 此处采用nginx的nginx-upload-modulejQuery-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
patch ngx_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{
    server 127.0.0.1:8001;
}
server {
listen       8080;
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";
        # 10 minute pre-flight approval
        add_header Access-Control-Max-Age 600;
        return 204;
    }
    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";
        # 10 minute pre-flight approval
        add_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_cleanup 400 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 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页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!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服务器处理。

通过如下方式获得相关参数:

1
2
3
4
5
6
7
8
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.其它配置文件参考
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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;
}
}
 
}

  

 
 
posted @   CalvinChu  阅读(1603)  评论(0编辑  收藏  举报
编辑推荐:
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
阅读排行:
· C# 13 中的新增功能实操
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· langchain0.3教程:从0到1打造一个智能聊天机器人
· 用一种新的分类方法梳理设计模式的脉络
点击右上角即可分享
微信分享提示