# Nginx Tornado File Upload

**URL:** <https://www.ruby-forum.com/t/nginx-tornado-file-upload/209457>\
**Category:** NGINX\
**Created:** [July 14, 2011, 11:30am UTC](https://www.ruby-forum.com/t/nginx-tornado-file-upload/209457 "2011-07-14T11:30:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [July 14, 2011, 11:30am UTC](https://www.ruby-forum.com/t/nginx-tornado-file-upload/209457/1 "2011-07-14T11:30:30Z")

</div>

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 /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/](http://127.0.0.1/)” “Mozilla/5.0 (X11; Linux  
i686; rv:6.0) Gecko/20100101 Firefox/6.0”

Posted at Nginx Forum:

> **[Nginx Tornado File Upload](https://forum.nginx.org/read.php?2,212377,212377#msg-212377)**
>
> 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 /var/log/error.log; #error\_log...

---

<div class="post-metadata">

**Author:** ![akash.gangil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f05b48/32.png) [@akash.gangil](https://www.ruby-forum.com/u/akash.gangil)\
**Post date:** [July 14, 2011, 8:05pm UTC](https://www.ruby-forum.com/t/nginx-tornado-file-upload/209457/2 "2011-07-14T20:05:23Z")

</div>

On Thu, Jul 14, 2011 at 05:30:01AM -0400, akash.gangil wrote:

Hi there,

> 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.

I’m not using tornado, but I’m not seeing any obvious problems when I  
mimic the nginx side of your configuration.

> ```
> upload_set_form_field $upload_field_name.path
> 
> ```
> 
> “$upload\_tmp\_path”;

That ends in “.path”.

> ```
> 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

That ends in “\_path”.

That’s the only odd-looking thing I can see. Might that be a cause of  
the problem?

Good luck,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![akash.gangil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f05b48/32.png) [@akash.gangil](https://www.ruby-forum.com/u/akash.gangil)\
**Post date:** [July 14, 2011, 8:23pm UTC](https://www.ruby-forum.com/t/nginx-tornado-file-upload/209457/3 "2011-07-14T20:23:21Z")

</div>

I have already tried that, with no success 😑

Posted at Nginx Forum:

> **[Re: Nginx Tornado File Upload](https://forum.nginx.org/read.php?2,212377,212403#msg-212403)**
>
> 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 /var/log/error.log; #error\_log...

---

<div class="post-metadata">

**Author:** ![akash.gangil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f05b48/32.png) [@akash.gangil](https://www.ruby-forum.com/u/akash.gangil)\
**Post date:** [July 15, 2011, 1:38am UTC](https://www.ruby-forum.com/t/nginx-tornado-file-upload/209457/4 "2011-07-15T01:38:34Z")

</div>

On Thu, Jul 14, 2011 at 02:22:49PM -0400, akash.gangil wrote:

> I have already tried that, with no success 😑

In that case, the debug log is the next thing to try. It will tell you  
what nginx thinks is happening.

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

> **[A debugging log](http://nginx.org/en/docs/debugging_log.html)**

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![akash.gangil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f05b48/32.png) [@akash.gangil](https://www.ruby-forum.com/u/akash.gangil)\
**Post date:** [July 15, 2011, 8:28am UTC](https://www.ruby-forum.com/t/nginx-tornado-file-upload/209457/5 "2011-07-15T08:28:39Z")

</div>

Here is the debug Log:  
[http://pastebin.com/4NVCdmrj](http://pastebin.com/4NVCdmrj)

Posted at Nginx Forum:

> **[Re: Nginx Tornado File Upload](https://forum.nginx.org/read.php?2,212377,212417#msg-212417)**
>
> 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 /var/log/error.log; #error\_log...

---

<div class="post-metadata">

**Author:** ![akash.gangil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f05b48/32.png) [@akash.gangil](https://www.ruby-forum.com/u/akash.gangil)\
**Post date:** [July 15, 2011, 8:37am UTC](https://www.ruby-forum.com/t/nginx-tornado-file-upload/209457/6 "2011-07-15T08:37:54Z")

</div>

Also Here is the Tornado Server Code if it helps:  
[http://codepad.org/hdw063eg](http://codepad.org/hdw063eg)

Posted at Nginx Forum:

> **[Re: Nginx Tornado File Upload](https://forum.nginx.org/read.php?2,212377,212418#msg-212418)**
>
> 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 /var/log/error.log; #error\_log...

---

<div class="post-metadata">

**Author:** ![akash.gangil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f05b48/32.png) [@akash.gangil](https://www.ruby-forum.com/u/akash.gangil)\
**Post date:** [July 16, 2011, 2:40am UTC](https://www.ruby-forum.com/t/nginx-tornado-file-upload/209457/7 "2011-07-16T02:40:01Z")

</div>

Finally solved it, thanks for your help! 🙂

Posted at Nginx Forum:

> **[Re: Nginx Tornado File Upload](https://forum.nginx.org/read.php?2,212377,212420#msg-212420)**
>
> 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 /var/log/error.log; #error\_log...

---

<div class="post-metadata">

**Author:** ![akash.gangil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f05b48/32.png) [@akash.gangil](https://www.ruby-forum.com/u/akash.gangil)\
**Post date:** [July 16, 2011, 3:10am UTC](https://www.ruby-forum.com/t/nginx-tornado-file-upload/209457/8 "2011-07-16T03:10:14Z")

</div>

On Fri, Jul 15, 2011 at 03:37:07AM -0400, akash.gangil wrote:

> Finally solved it, thanks for your help! 🙂

Good news!

And, to help the next person who has a similar problem and finds this  
thread when using a search engine, can you share what the solution was?

The debug log shows:

hashed path: /tmp/0000000001

and then eventually

finished cleanup of file “/tmp/0000000001” after http status 400 while  
closing request

which may explain why you didn’t see the file in /tmp when you went  
looking for it.

And it also shows

http script var: “upfile”  
http script copy: “.path”  
http script copy: ““”  
http script var: “/tmp/0000000001”  
http script copy: “””

so nginx thinks it is setting upfile.path correctly in what was sent to  
the backend.

So presumably the problem was elsewhere.

Cheers,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
