Nginx Tornado文件上传

时间:2011-07-14 07:59:48

标签: python file-upload nginx tornado

我正在尝试通过nginx_upload_module 2.2.0上传文件。我有nginx 1.0.4设置作为反向代理,后端有龙卷风服务器。 下面是我的nginx.conf:

#user  nobody;
worker_processes  1;


#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        /var/log/nginx.pid;


events {
    worker_connections  1024;
}


http {
include       mime.types;
index index.html
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  access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

gzip  on;

upstream frontends {
    server 127.0.0.1:8888;
}



server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

# Allow file uploads max 50M for example
    client_max_body_size 50M;

    #access_log  logs/host.access.log  main;
    error_log  /var/log/error.log info;


#POST URLn
    location /upload {
        # Pass altered request body to this location
        upload_pass @after_upload;

        # Store files to this directory
        upload_store /tmp;

        # Allow uploaded files to be read only by user
        upload_store_access user:rw;

        # Set specified fields in request body
        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”;

        # Inform backend about hash and size of a file
        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 “some_hidden_field_i_care_about”;

        upload_cleanup 400 404 499 500-505;
    }

location / {
        root   /opt/local/html;
    }


    location @after_upload {
        proxy_pass   http://127.0.0.1:8888;
    }

}
}

我已经测试了设置,nginx会将请求转发给龙卷风。但是当我尝试上传文件时,它会给我一个400:Bad Request http状态代码。随着龙卷风日志声明,它缺少请求中的upfile.path。当我尝试去nginx本应存储上传文件的文件夹时,它不在那里。因此400错误。

有人能指出为什么nginx没有将文件存储在指定的目录/ tmp中?

龙卷风日志:

  

警告:root:400 POST / upload(127.0.0.1):缺少参数upfile_path   警告:root:400 POST / upload(127.0.0.1)2.31ms

Nginx错误日志:

  

127.0.0.1 - - [14 / Jul / 2011:13:14:31 +0530]“POST / upload HTTP / 1.1”400 73“http://127.0.0.1/”“Mozilla / 5.0(X11; Linux i686; rv:6.0)Gecko / 20100101 Firefox / 6.0“

带有信息选项的更详细的错误日志:

  

2011/07/14 16:17:00 [info] 7369#0:* 1开始将文件“statoverride”上传到“/ tmp / 0000000001”(字段“upfile”,内容类型“application / octet-stream” ),client:127.0.0.1,server:localhost,request:“POST / upload HTTP / 1.1”,host:“127.0.0.1”,referrer:“http://127.0.0.1/”

     

2011/07/14 16:17:00 [info] 7369#0:* 1完成上传文件“statoverride”到“/ tmp / 0000000001”,客户端:127.0.0.1,服务器:localhost,请求:“POST / upload HTTP / 1.1“,host:”127.0.0.1“,referrer:”http://127.0.0.1/“

     

2011/07/14 16:17:00 [info] 7369#0:* 1在关闭请求,客户端:127.0.0.1,服务器:0.0后,在http状态400之后完成文件“/ tmp / 0000000001”的清理。 0.0:80

带有调试选项的更详细的错误日志:

http://pastebin.com/4NVCdmrj

编辑1: 因此,我们可以从上面的错误日志中推断出的一点是,该文件正由nginx上传到/ tmp,但随后会被清理。不知道为什么,在这里需要帮助。

1 个答案:

答案 0 :(得分:0)

我刚刚编写了一个带有tornado和nginx-upload-module的web应用程序,它可以工作。 根据您提供的龙卷风日志,我想您可以尝试更改代码

self.get_argument('upfile_path')

self.get_argument('upload_tmp_path')

nginx确实存储了该文件, 但是“upload_cleanup 400 404 499 500-505;”当您的应用程序使用指定的HTTP代码响应时,此行告诉它清理文件。