custom directory listing handler in ruby for nginx
I just found this question 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.
location /test/ {
error_page 404 = /webdir/handler.php;
return 404;
}
My questio开发者_运维技巧n is, would I simply do the same thing but reference the handler.rb
instead of handler.php
, 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 puts
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.
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 handler.rb
, though if it's possible to separate it out into files that would be beautiful.
Finally, I would like this to apply to the mysite.com/downloads/
directory as well as all of its sub-directories. What would be the preferred location
directive? I saw that
location ~ /$
is to make it work on all directories, but I only want it to apply to downloads/
and any of its subdirectories. I think it'd be a simple regex modification but I'd rather be sure.
Thanks, I appreciate it.
No, nginx will not automatically evaluate Ruby scripts. Your best bet is to set up a handler as a Rack application of some sort (pretty much all Ruby web frameworks or handlers are Rack applications these days, including Sinatra and Rails) and then use passenger or a Ruby server like unicorn set up as an nginx proxy.
Given your additional requirements, I would start by looking at Sinatra.
精彩评论