Setting up Sinatra to run in a subdirectory

风流意气都作罢 提交于 2019-12-10 06:49:56

问题


Now I am pretty new to Sinatra/Ruby/Apache but have inherited a Sinatra application to deploy.

Currently Apache is set up to run from a document root (httpdocs) and I need to run a ruby application underneath a folder subdirectory such as: /httpdocs/webapp

What do I need to do to get this up and running under a subdirectory?


回答1:


This link might be helpful, it explains how to deploy a Sinatra app with Apache using Passenger (mod_rack): Deploying a Sinatra App with Apache and Phusion Passenger

The part of particular interest to you is the RackBaseURI option in the virtual host configuration. The official documentation is available here: Phusion Passenger users guide - Deploying Rack to Sub URI




回答2:


I just ran into the same issue. Since there was no answer here for how to do this without Passenger, I'm going to document the solution for Apache + CGI or FastCGI.

The trick is to rewrite PATH_INFO for Rack, and everything will fall into place:

  1. Set up .htaccess:

     RewriteEngine On
     RewriteRule ^(.*)$ sinatra_app.cgi [QSA,L,E=PATHINFO:/$1]
    
  2. In your Sinatra code, before anything else:

     ENV['PATH_INFO'] = ENV['REDIRECT_PATHINFO']
    

Now all URLs like /subfolder/resource/123 will hit the correct route in the Sinatra app.
In the above case get '/resource/:id' will work correctly, assuming the Sinatra app was put into /subfolder.



来源:https://stackoverflow.com/questions/7577622/setting-up-sinatra-to-run-in-a-subdirectory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!