nginx 的 ruby 中的自定义目录列表处理程序
我刚刚发现 this Question on serverfault,详细介绍了如何使用 php 脚本为 nginx 创建自定义目录列表处理程序。我想做同样的事情,但是使用 ruby 脚本。
location /test/ {
error_page 404 = /webdir/handler.php;
return 404;
}
我的问题是,我是否会简单地做同样的事情,但引用handler.rb
而不是handler.php
,我的意思是,nginx会自动知道评估ruby脚本的和输出到用户的浏览器?那么我是否只是创建一个执行一些 puts
调用的脚本,或者执行此操作的首选方法是什么?我对rack或sinatra不太了解,但是在这种情况下两者都会派上用场吗?我确实安装了乘客。
我很可能希望在其中包含 css 以使列表看起来不错,并且我可能更喜欢在 haml 或 erb 中创建结构。我不介意结构模板和 CSS 是否需要内联在同一个 handler.rb
中,不过如果可以将其分离到文件中那就太好了。
最后,我希望这适用于 mysite.com/downloads/
目录及其所有子目录。首选的 location
指令是什么?我看到这
location ~ /$
是为了使其适用于所有目录,但我只希望它适用于 downloads/
及其任何子目录。我认为这只是一个简单的正则表达式修改,但我宁愿确定。
谢谢,我很感激。
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 question 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,nginx 不会自动评估 Ruby 脚本。最好的选择是将处理程序设置为某种 Rack 应用程序(现在几乎所有 Ruby Web 框架或处理程序都是 Rack 应用程序,包括 Sinatra 和 Rails),然后使用乘客或 Ruby 服务器(如 unicorn)设置为nginx 代理。
考虑到您的额外要求,我将首先查看 Sinatra。
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.