Nginx Tornado 文件上传

发布于 2024-11-24 07:15:02 字数 3327 浏览 0 评论 0原文

我正在尝试通过 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 警告:root:400 POST /上传(127.0.0.1)2.31ms

Nginx 错误日志:

127.0.0.1 - - [14/Jul/2011:13:14:31 +0530] “POST /上传 HTTP/1.1” 400 73 “http://127.0.0.1/” “Mozilla/5.0 (X11; Linux i686;RV:6.0)壁虎/20100101火狐/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 /上传 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 /上传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,但随后会被清理。不知道为什么,这里需要帮助。

I am trying to upload file via nginx_upload_module 2.2.0. I have nginx 1.0.4 setup as a reverse proxy with a tornado server at the backend.
Below is my 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;
    }

}
}

I have already tested the setup, and nginx does forward the request to tornado. But when I try to upload the file it gives me a 400: Bad Request http status code. With the tornado log stating that, it's missing the upfile.path in the request. And when I try to go to the folder where nginx should have supposedly stored the uploaded file it isn't there. And hence the 400 error.

Can anyone point why is nginx not storing the file at the specified directory /tmp ?

Tornado Log :

WARNING:root:400 POST /upload (127.0.0.1): Missing argument upfile_path
WARNING:root:400 POST /upload (127.0.0.1) 2.31ms

Nginx Error Log :

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"

More verbose Error Log with Info option:

2011/07/14 16:17:00 [info] 7369#0: *1 started uploading file "statoverride" to "/tmp/0000000001" (field "upfile", content type "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 finished uploading file "statoverride" to "/tmp/0000000001", 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 finished cleanup of file "/tmp/0000000001" after http status 400 while closing request, client: 127.0.0.1, server: 0.0.0.0:80

More verbose Error Log with Debug option:

http://pastebin.com/4NVCdmrj

Edit 1:
So, one point that we can infer from the above error log is that, the file is being uploaded by nginx to /tmp but is getting cleaned up subsequently. Don't know why, need help here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装不在乎 2024-12-01 07:15:02

我刚刚用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 代码进行响应时,此行告诉它清理文件。

I have just written a web application with tornado and nginx-upload-module,and it works.
According to the tornado log you've provided, I guess you can try to change your code

self.get_argument('upfile_path')

to

self.get_argument('upload_tmp_path')

nginx did store the file,
but "upload_cleanup 400 404 499 500-505;" this line tells it to cleanup the file, when your application response with the specified HTTP code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文