如何让我的rails控制器通过Passenger使用子域?

时间:2012-04-27 02:19:35

标签: ruby-on-rails apache configuration subdomain passenger

所以这对我在开发模式下有效,但是当我尝试使用乘客部署我的rails app时,我的控制器似乎没有被调用。

我已经为www.example.com设置了API的cname记录。另外,我使用的是Rails 3.2和Ruby 1.9.3。

以下是我的routes.rb文件的相关部分。

# API
constraints :subdomain => 'api' do
  scope :module => 'api' do #:constraints => { :format => :json } do
    match '*skippydoo' => redirect('/'), :format => :html
    root :to => 'pages#developer', :format => :html
  end
end

这是Apache配置:

# PassengerHighPerformance on
PassengerMaxPoolSize 12
PassengerPoolIdleTime 1500
# PassengerMaxRequests 1000
PassengerStatThrottleRate 120
# RackAutoDetect Off
# RailsAutoDetect Off

NameVirtualHost 10.28.124.130:80

<VirtualHost 10.28.124.130:80>

ServerName application.example.com
ServerAlias application

DocumentRoot /var/www/application/current/public/
<Directory /var/www/application/current/public>
        Options +FollowSymLinks
        AllowOverride All
        Order allow,deny
        allow from all
</Directory>
RackBaseURI /
RackEnv staging

ErrorDocument 503 /system/maintenance.html
RewriteEngine On
RewriteLog /var/www/application/current/log/rewrite_log
RewriteLogLevel 9
RewriteCond %{REQUEST_URI} !.(css|gif|jpg|png)$
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$  -  [redirect=503,last]

</VirtualHost>

<VirtualHost 10.28.124.130:80>

ServerName api.application.example.com
ServerAlias api.application

DocumentRoot /var/www/application-api/current/public
<Directory /var/www/application-api/current/public>
        Options +FollowSymLinks
        AllowOverride All
        Order allow,deny
        allow from all
</Directory>
RackBaseURI /
RackEnv staging
</VirtualHost>

这只是呈现了我写的描述我的API的文档。

我可以将文档根目录切换到某个空目录并获取该渲染,因此我知道Apache正常工作。 application-api目录是我部署的应用程序的符号链接。

我的API控制器位于$RAILS_ROOT/app/controllers/api/pages_controller.rb,但实际执行工作的是$RAILS_ROOT/app/controllers/pages_controller.rb

Started GET "/" for 10.29.28.157 at 2012-04-26 21:12:51 -0500
Processing by PagesController#home as HTML
  Rendered pages/home.html.erb within layouts/application (283.7ms)
  Rendered layouts/_stylesheets.html.erb (4.4ms)
  Rendered layouts/_header.html.erb (5.7ms)
  Rendered layouts/_footer.html.erb (0.4ms)
Completed 200 OK in 522ms (Views: 394.4ms | ActiveRecord: 12.1ms | Solr: 0.0ms)

那么,是什么给出的?为什么它在开发中起作用而不是在生产中起作用?

2 个答案:

答案 0 :(得分:1)

我没有看到您的www子域名的VirtualHost。 Apache甚至是将请求发送给Passenger吗?

也许你需要更改ServerName,或者添加另一个ServerAlias?

答案 1 :(得分:1)

所以,我改变了我的约束:

constraints :subdomain => /^api/ do

它开始工作了。现在,为什么正则表达式会起作用并且特别命名子域名对我来说不是一个惊喜!