How to serve protected downloads with Rails

24 November 2006

Do you have a site where you sell downloads? Perhaps you sell e-books, screencasts, or MP3s. Would you like a way to protect these downloads by using your Rails application to authenticate users before they can download the file, but you don't want to tie up a Rails process to actually serve the file? Lighttpd's mod_secdownload was built specifically for this situation, but there's a little gotcha for those of you who are also using mod_proxy to pass traffic to mongrel...

Read the rest of this entry »



How to handle uploaded files

10 November 2006

Are you a web developer who wants to know some tips or tricks about storing files in a database? Would you like a quick example on how to handle a file upload in Rails and stuff that file in your database? My answer to that question is don't do it. Here's why.

The database software you are most likely using is a relational database, which was designed to hold discrete and related chunks of data and to make it easy for you to retrieve that data based on relationships to other data. Your filesystem was designed to store and organize files and to make it easy for you to retrieve those files in a fairly random fashion. In other words, use the file system for storing files, not your database. Still not convinced? Alright, then I can give you an example.

Read the rest of this entry »



Easy Server Migrations

03 November 2006

What if you could complete a server migration with just a few minutes of down-time? What if you didn't have to wait for DNS changes to propagate to have all of your users use your new server? It's not only possible, it's easy! As long as you use mongrel, that is.

Read the rest of this entry »



Conditional Proxying with Lighttpd

28 July 2006

Let's say you have a pretty typical lighttpd -> mod_proxy -> mongrel configuration for your Rails app. You'd rather not proxy requests for static files like stuff in the images directory to mongrel. A quick search of the lazy web didn't turn up a copyable example for me, so here you go:

$HTTP["host"] =~ "example.com" {
  server.document-root = "/var/rails/myapp/current/public"
  proxy.balance = "fair" 
  $HTTP["url"] !~ "^/(images|stylesheets|javascripts)" {
    proxy.server  = ( "/" => (
      ( "host" => "127.0.0.1", "port" => 2000 )
    ) )
  }
}

So there you go ... now lighty won't even attempt to proxy to mongrel anything URL that we know will be static, but instead will serve it up itself.

Oh, and don't forget to take out the backslashes from before the quotes. WP is dumb.