问题
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:
Set up
.htaccess
:RewriteEngine On RewriteRule ^(.*)$ sinatra_app.cgi [QSA,L,E=PATHINFO:/$1]
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