【问题标题】:Nginx Tornado File UploadNginx 龙卷风文件上传
【发布时间】:2011-07-14 23:58:44
【问题描述】:

我正在尝试通过 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 确实将请求转发给了 tornado。但是当我尝试上传文件时,它给了我一个 400: Bad Request http 状态码。龙卷风日志指出,它缺少请求中的 upfile.path。当我尝试转到 nginx 应该存储上传文件的文件夹时,它不存在。因此出现 400 错误。

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

龙卷风日志:

警告:root:400 POST /upload (127.0.0.1):缺少参数 upfile_path WARNING: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” ),客户端:127.0.0.1,服务器:localhost,请求:“POST /upload HTTP/1.1”,主机:“127.0.0.1”,引用者:“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”,主机:“127.0.0.1”,引用者:“http://127.0.0.1/”

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

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

http://pastebin.com/4NVCdmrj

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

【问题讨论】:

    标签: python file-upload nginx tornado


    【解决方案1】:

    我刚刚用 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 代码响应时清理文件。

    【讨论】:

    • 这不起作用,而是给出 500 错误状态代码。如果可能的话,你能分享你的代码吗?
    • 你能把你的代码贴一下,这样我就知道哪里出了问题。你可以打印 self.request.arguments 列表来检查 nginx 给你的参数。在我的代码中,我使用 self.get_argument ('files[].name') 获取文件名和 self.get_argument('files[].path') 获取路径。(ps。对不起我的英语不好)
    猜你喜欢
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2015-05-01
    • 1970-01-01
    • 2012-11-02
    相关资源
    最近更新 更多