title: custom directory listing handler in ruby for nginx
body: <p>I just found <a href="https://serverfault.com/questions/160327/get-nginx-to-always-load-webdir-page-php-on-directories-if-no-index-html-page">this question</a> on serverfault which details how to create a custom directory listing handler for nginx with a php script. I would like to do the same thing but with a ruby script.</p>
  
  <pre><code>location /test/ {
    error_page 404 = /webdir/handler.php;
    return 404;
  }
  </code></pre>
  
  <p>My question is, would I simply do the same thing but reference the <code>handler.rb</code> instead of <code>handler.php</code>, I mean, will nginx automatically know to evaluate the ruby script's and output to the user's browser? So do I simply create a script that does some <code>puts</code> calls, or what is the preferred way of doing this? I don't know much about rack or sinatra, but is this a situation in which either would come in handy? I do have passenger installed.</p>
  
  <p>I would most likely want to include css in this to make the listing look nice, and I would possibly prefer to create the structure in haml or erb. I don't mind if the structure template and css would need to be inline in the same <code>handler.rb</code>, though if it's possible to separate it out into files that would be beautiful.</p>
  
  <p>Finally, I would like this to apply to the <code>mysite.com/downloads/</code> directory as well as all of its sub-directories. What would be the preferred <code>location</code> directive? I saw that</p>
  
  <pre><code>location ~ /$
  </code></pre>
  
  <p>is to make it work on all directories, but I only want it to apply to <code>downloads/</code> and any of its subdirectories. I think it'd be a simple regex modification but I'd rather be sure.</p>
  
  <p>Thanks, I appreciate it.</p>