Posterous
Brian is using Posterous to post everything online. Shouldn't you?
Unknown35
 

Brian’s Code Blog

« Back to blog

Nginx Upload Module

So if you're doing any uploads in a ruby/rails webapp and nginx, you should be using the nginx upload module. In fact it'd work with any web app running nginx, but the conf file may be a little different. So why is the upload module so awesome? Lets start off going over a typical upload process in ruby:

  • File is sent to server
  • File is buffered by ruby
  • File is available for use
However, using the upload module the process goes like this:
  • File is sent to server
  • File is buffered by c
  • File is available for use
Guess which one is faster? This will take some tweaking to your app to get it to work. You can either create a new file instance from the file path variable or try using rack-uploads. I took the former route. On our staging server I noticed a huge speed increase. I didn't measure it, but is noticeably faster, espicially for larger files. So no the grand question: How do I use it? Well here's what I added to my nginx.conf file to make it work. If you're unfamiliar with nginx, these go inside the server block. Now this may not work perfect for you if you're not using unicorn. I was banging my head against the wall for a while trying to figure out why rails couldn't recognize the request. Turns out nginx doesn't forward the host by default. You can checkout the source here http://github.com/vkholodkov/nginx-upload-module. As for version compatibility:
  • 2.0.9 is compatible with nginx 0.6.x, 0.7.52-0.7.62, 0.8.1-0.8.8
  • 2.0.10 is compatible with nginx 0.6.x, 0.7.52-0.7.62, 0.8.1-0.8.16

Lastly, here's the conf bits, enjoy!

 
To leave a comment on this posterous, please login by clicking one of the following.
Posterous-login     twitter