# Nginx and uploads

**URL:** <https://www.ruby-forum.com/t/nginx-and-uploads/222424>\
**Category:** NGINX\
**Created:** [August 28, 2012, 11:58am UTC](https://www.ruby-forum.com/t/nginx-and-uploads/222424 "2012-08-28T11:58:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [August 28, 2012, 11:58am UTC](https://www.ruby-forum.com/t/nginx-and-uploads/222424/1 "2012-08-28T11:58:23Z")

</div>

Hello,

I am trying to upload files to Nginx and process them via itself.  
I have compiled it with nginx\_upload\_module-2.2.0.

My config is the following:  
server {  
listen 80;  
server\_name [example.com](http://example.com);  
client\_max\_body\_size 100m;

```
location / {
    root /www;
    autoindex on;
}
 location /upload {
    root /www;

    if ($request_method = POST) {
    upload_pass @test;
    break;
}
    upload_store /www/upload;
    upload_store_access user:rw group:rw all:rw;
    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”;  
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 “^submit$|^description$”;  
upload\_cleanup 400 404 499 500-505;  
}

location @test {  
proxy\_pass [http://example.com:8080](http://example.com:8080);  
}

}

```
  server {
    listen 8080;
    server_name example.com;
    location / {
    root /www;
  }

```

}

This works, but it saves my uploads with random number names like  
0000000001  
0000000002 0000000003 0000000004 0000000005 0000000006 0000000007  
0045422062 0059512315.  
I want to save them with the original name.  
Remember, my @test backend is also Nginx and not something else.

Pls help.

Posted at Nginx Forum:

> **[Nginx and uploads](https://forum.nginx.org/read.php?2,230184,230184#msg-230184)**
>
> Hello, I am trying to upload files to Nginx and process them via itself. I have compiled it with nginx\_upload\_module-2.2.0. My config is the following: server { listen 80; server\_name example.com; client\_max\_body\_siz

---

<div class="post-metadata">

**Author:** ![w00t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@w00t](https://www.ruby-forum.com/u/w00t)\
**Post date:** [August 28, 2012, 3:44pm UTC](https://www.ruby-forum.com/t/nginx-and-uploads/222424/2 "2012-08-28T15:44:33Z")

</div>

I’m not sure you can really do what you want to. The upload module was  
not  
designed for this purpose. If you want to do this you either need to  
modify  
the source of the upload module or perhaps you can handle such logic by  
using the lua module to script what to do with the file uploads.

Posted at Nginx Forum:

> **[Re: Nginx and uploads](https://forum.nginx.org/read.php?2,230184,230206#msg-230206)**
>
> Hello, I am trying to upload files to Nginx and process them via itself. I have compiled it with nginx\_upload\_module-2.2.0. My config is the following: server { listen 80; server\_name example.com; client\_max\_body\_siz

---

<div class="post-metadata">

**Author:** ![w00t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@w00t](https://www.ruby-forum.com/u/w00t)\
**Post date:** [August 28, 2012, 5:02pm UTC](https://www.ruby-forum.com/t/nginx-and-uploads/222424/3 "2012-08-28T17:02:57Z")

</div>

This seems odd. If it wasn’t meant for Nginx to process uploaded files,  
then  
it couldn’t have processed them by itself.  
For example, I changed  
location @test {  
proxy\_pass [http://example.com:8080](http://example.com:8080);

to  
location @test {  
default\_type text/html;  
root /www;  
and removed the :8080 server part and it still works.  
So I am inclined to think that there must be a way to set the filename  
without going to such lenghts as to change the code.

Posted at Nginx Forum:

> **[Re: Nginx and uploads](https://forum.nginx.org/read.php?2,230184,230212#msg-230212)**
>
> Hello, I am trying to upload files to Nginx and process them via itself. I have compiled it with nginx\_upload\_module-2.2.0. My config is the following: server { listen 80; server\_name example.com; client\_max\_body\_siz

---

<div class="post-metadata">

**Author:** ![w00t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@w00t](https://www.ruby-forum.com/u/w00t)\
**Post date:** [August 29, 2012, 6:51am UTC](https://www.ruby-forum.com/t/nginx-and-uploads/222424/4 "2012-08-29T06:51:58Z")

</div>

## w00t Wrote:

> and removed the :8080 server part and it still works.  
> So I am inclined to think that there must be a way to set the filename  
> without going to such lenghts as to change the code.

Yes there is, but the default nginx install does not have the features  
available to do what you want and the upload module does not introduce  
any  
such feature either. If you want nginx to be able to rename files based  
on  
user input then you need a module which introduces that capability into  
nginx.

Of course, at this point I’m talking theory only as I haven’t ever done  
anything like this, but it should work provided either lua module or  
perl  
module has the adds the required features - in theory.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,230184,230247#msg-230247](http://forum.nginx.org/read.php?2,230184,230247#msg-230247)

---

<div class="post-metadata">

**Author:** ![w00t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@w00t](https://www.ruby-forum.com/u/w00t)\
**Post date:** [August 29, 2012, 1:05pm UTC](https://www.ruby-forum.com/t/nginx-and-uploads/222424/5 "2012-08-29T13:05:24Z")

</div>

On Tuesday 28 August 2012 19:02:31 w00t wrote:

> This seems odd. If it wasn’t meant for Nginx to process uploaded files,  
> then it couldn’t have processed them by itself.

You should do this task in your application and it’s not odd (see below  
why).

[…]

> So I am inclined to think that there must be a way to set the filename  
> without going to such lenghts as to change the code.

Just rename the uploaded file is not enough. You also need to validate  
its  
content. Otherwise, you will open a potential security hole in your  
server.

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![w00t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@w00t](https://www.ruby-forum.com/u/w00t)\
**Post date:** [August 30, 2012, 3:02pm UTC](https://www.ruby-forum.com/t/nginx-and-uploads/222424/6 "2012-08-30T15:02:56Z")

</div>

Indeed, after some more search I have found what you are saying.

> **[Re: upload module help](https://forum.nginx.org/read.php?2,185057,186002#msg-186002)**
>
> hi, i am using nginx upload module to upload files.i am running this in ubuntu.when i upload files .its working fine .but name of the file is something different .this is not taking the upload \_set\_form\_field values. i dont know what went wrong.here...

Posted at Nginx Forum:

> **[Re: Nginx and uploads](https://forum.nginx.org/read.php?2,230184,230323#msg-230323)**
>
> Hello, I am trying to upload files to Nginx and process them via itself. I have compiled it with nginx\_upload\_module-2.2.0. My config is the following: server { listen 80; server\_name example.com; client\_max\_body\_siz

---

<div class="post-metadata">

**Author:** ![w00t](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e19b73/32.png) [@w00t](https://www.ruby-forum.com/u/w00t)\
**Post date:** [August 30, 2012, 9:13pm UTC](https://www.ruby-forum.com/t/nginx-and-uploads/222424/7 "2012-08-30T21:13:22Z")

</div>

Hello!

On Tue, Aug 28, 2012 at 6:43 AM, Ensiferous [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’m not sure you can really do what you want to. The upload module was not  
> designed for this purpose. If you want to do this you either need to modify  
> the source of the upload module or perhaps you can handle such logic by  
> using the lua module to script what to do with the file uploads.

Yes! There’s a lua-resty-upload library for ngx\_lua that can do  
non-buffered uploading:

```
https://github.com/agentzh/lua-resty-upload

```

You don’t have to touch the disk at all if you prefer sending the data  
chunks to the TCP/UDP backends (via cosockets) in a strict  
non-buffered mode.

Best regards,  
-agentzh
