Nginx Upload Module
Valery Kholodkov has written a very cool nginx module for handling uploads.
The way this works is that you specify a location block to handle the uploads. So if you are using the standard nginx.conf for rails apps then you would add this in your server block right above your “location /” block:
# Upload form should be submitted to this location
location /upload {
# Pass altered request body to this location
upload_pass @backend;
# Store files to this location
upload_store /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";
}
# Pass altered request body to a proxy
location @backend {
proxy_pass http://backend;
}
Then make a simple upload form that does a multipart POST to /upload. Now you can have your Rails or Merb app on the backend with a route called /upload. In the action of your app that responds to the /upload route you will get a set of params that look like this(assume the name of your upload fields is called ‘file1’ and ‘file2’):
{«file2.path»=>»/tmp/0000123459″, «file1.path»=>»/tmp/0000123458″,
«file2.content_type»=>»image/png», «submit»=>»Upload»,
«file2.name»=>»Picture 2.png», «action»=>»index»,
«file1.name»=>»Picture 1.png», «controller»=>»test»,
«file1.content_type»=>»image/png», «test»=>»value»}
What this is doing if parsing the multi-part mime boundaries in C in the nginx plugin, putting the parsed files into files in /tmp and then stipping the multipart stuff out of the POST body and replacing it with the info you need to get the name and location of the file on disk.
This means that by the time the request hits your application, the expensive mime parsing is already done and you simply move the file to it’s final resting place. This is a huge win since now the hard work is done in C in nginx before your app ever gets involved.
Of course this is a fresh new module so do your own testing and deciding whether or not this is a fit for your needs. But I think this is a great plugin and have verified it works as advertised.