Brian’s Code 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!

Filed under  //   nginx   ruby   upload_module  

Using ngnix with Unicorn

So there's a new kid on the block in the ruby server world, Unicorn. The guys over at github have a good writeup on their blog about what it is and how to user it, that also posted a god config file to monitor it. Also if you want a good explanation of why unicorn is cool, check out Ryan Tomayko's article "I like Unicorn because it's Unix". One important note on both their unicorn config and their config, the paths they have are very specific to their setup. If you're running a normal capistrano setup, you'll want to store the sockets and pids in the shared directory rather than the current. So now the only part is the front end web server. This typically tends to be nginx(pronounced engine x) or apache these days, though litespeed and lighttpd are good as well. My personal choice is nginx, installing it and getting running is pretty easier and straight forward. All that's left to do is tweak the nginx.conf file. I used a modified version of ezra's nginx.conf file. Here's the conf with my optimizations:

Note if you're not using a socket just set the server in the upstream block to 127.0.0.1:<%=@port %>

Filed under  //   nginx   ruby   unicorn