Nginx Tornado文件上传
我正在尝试通过nginx_upload_module 2.2.0上传文件。我已经设置了nginx 1.0.4作为反向代理,后面连接的是一个tornado服务器。以下是我的nginx配置文件:
#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: 错误请求的HTTP状态码。tornado的日志显示,请求中缺少了upfile.path。而当我去nginx应该存储上传文件的文件夹时,发现根本没有这个文件。因此出现了400错误。
有没有人能告诉我,为什么nginx没有把文件存储在指定的目录/tmp呢?
tornado日志:
警告:root:400 POST /upload (127.0.0.1): 缺少参数upfile_path
警告:root:400 POST /upload (127.0.0.1) 2.31毫秒
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 [信息] 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 [信息] 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 [信息] 7369#0: *1 在关闭请求时,HTTP状态为400,完成了对文件"/tmp/0000000001"的清理,客户端: 127.0.0.1,服务器: 0.0.0.0:80
更详细的错误日志(调试选项):
http://pastebin.com/4NVCdmrj
编辑1:
从上面的错误日志中我们可以推断出,文件确实是被nginx上传到了/tmp,但随后又被清理掉了。不知道为什么,这里需要帮助。
1 个回答
我刚刚用tornado和nginx-upload-module写了一个网页应用,而且它运行得很好。根据你提供的tornado日志,我猜你可以试着把你的代码改成
self.get_argument('upfile_path')
改成
self.get_argument('upload_tmp_path')
nginx确实保存了文件,但这一行“upload_cleanup 400 404 499 500-505;”是告诉它,当你的应用返回指定的HTTP状态码时,要清理这个文件。