Nginx Tornado File Upload
walterwefft
walterwefft at gmail.com
Fri Jul 15 10:41:50 MSD 2011
akash.gangil wrote:
> 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.
The following works for me:
location ~ upload$ {
# Pass altered request body to this location
upload_pass @after_upload;
# Store files to this directory
upload_store /www/media/tmp;
# 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 "_xsrf";
upload_cleanup 400 404 499 500-505;
}
location @after_upload {
proxy_pass http://frontends;
}
location / {
proxy_pass_header Server;
proxy_pass http://frontends;
}
And in Tornado:
@tornado.web.authenticated
def post(self, uid):
# pull in details created by the nginx file upload module
fname = self.get_argument('imagefile_name', None)
content_type = self.get_argument('imagefile_content_type', None)
file_size = self.get_argument('imagefile_size', -1)
file_hash = self.get_argument('imagefile_md5', '')
tmp_path = self.get_argument('imagefile_path', None)
if tmp_path is not None:
logging.info("::::FILE UPLOAD::::")
logging.info("FILE NAME: %s" % fname)
logging.info("FILE TYPE: %s" % content_type)
logging.info("FILE TMP PATH: %s" % tmp_path)
dest_dir = "/www/%s" % addressid
if not os.path.exists(dest_dir):
makedirs(dest_dir)
# move the file into place
fname = '.'.join(map(slugify, os.path.splitext(fname)))
dest_path = '%s/%s' % (dest_dir, fname)
logging.info("FILE ACTUAL PATH: %s" % dest_path)
move(tmp_path, dest_path)
> Below is my nginx.conf :
>
> #user nobody;
> worker_processes 1;
>
> error_log /var/log/error.log;
> #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;
>
> #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"
>
> Posted at Nginx Forum: http://forum.nginx.org/read.php?2,212377,212377#msg-212377
>
>
> _______________________________________________
> nginx mailing list
> nginx at nginx.org
> http://nginx.org/mailman/listinfo/nginx
More information about the nginx
mailing list