Ruby on rails Rails 4路由子域约束

Ruby on rails Rails 4路由子域约束,ruby-on-rails,apache,routes,subdomain,passenger,Ruby On Rails,Apache,Routes,Subdomain,Passenger,我有一个rails应用程序在开发模式下工作,路由配置如下: scope module: :web do resources :locations end constraints subdomain: "api" do scope module: :api do resources :locations end end 当启动rails服务器并分别使用www.lvh.me:3000和api.lvh.me:3000时,这种方法就可以工作了。但

我有一个rails应用程序在开发模式下工作,路由配置如下:

  scope module: :web do
    resources :locations
  end


  constraints subdomain: "api" do
    scope module: :api do
      resources :locations
    end
  end
当启动rails服务器并分别使用www.lvh.me:3000和api.lvh.me:3000时,这种方法就可以工作了。但是,当使用apache和passenger在生产环境中运行时,这会失败,导致出现
ActionController::RoutingError(没有与[GET]“/locations]匹配的路由)
访问任一域时出错

我的apache vhost配置文件如下所示:

<VirtualHost *:80>
    ServerName server.XYZ.co.uk
    ServerAlias *.XYZ.co.uk

    DocumentRoot /myapp/current/public

    <Directory /myapp/current/public>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

ServerName server.XYZ.co.uk
ServerAlias*.XYZ.co.uk
DocumentRoot/myapp/current/public
选项索引跟随符号链接
不允许超限
要求所有授权
ErrorLog${APACHE_LOG_DIR}/error.LOG
CustomLog${APACHE\u LOG\u DIR}/access.LOG组合

有人知道我做错了什么吗

我当然希望删除
www
约束-

#config/routes.rb
constraints subdomain: "api" do
   scope module: :api do
     resources :locations
   end
end

scope module: :web do
   resources :locations
end
除非您试图仅从“www”子域为您的应用程序提供服务,否则您将更好地将路由视为整个应用程序的路由,只使用约束指定单个元素

--

Apache

您还需要确保Apache/Passenger能够正确捕获您的子域:

#etc/apache2/apache2.conf
VirtualHost *:80>
    ServerName XYZ.co.uk
    ServerAlias www.XYZ.co.uk

    DocumentRoot /myapp/current/public

    <Directory /myapp/current/public>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>
#etc/apache2/apache2.conf
虚拟主机*:80>
ServerName XYZ.co.uk
ServerAlias www.XYZ.co.uk
DocumentRoot/myapp/current/public
选项索引跟随符号链接
不允许超限
要求所有授权
ErrorLog${APACHE_LOG_DIR}/error.LOG
CustomLog${APACHE\u LOG\u DIR}/access.LOG组合
您需要记住的是,当您向Rails应用程序发送请求时,它将接受浏览器提供的任何约束,并呈现相应的路由。在这个实例中,Apache只是一个网关——一种连接HTTP请求和后端应用程序的方法

您应该尽可能多地使用Rails路由系统,这意味着您应该离开
apache
conf文件来转发请求,而不是尝试处理/路由请求

具体来说,我认为您不需要在Apache配置文件中指定
api
子域。你最好让车辆通过轨道本身

--

导轨

最后,您提到您收到了
no route matches
错误-您可以在Rails日志中查看来自Apache的请求的实际内容


我想你的应用程序要么没有通过正确的约束,要么你的服务器设置不正确。您可能希望确保您的routes和Apache配置设置如上所述,然后使用Rails接收的请求更新您的问题

谢谢我删除了www约束。似乎所有对api子域的请求都被路由到web命名空间,而不是由api命名空间中的控制器处理。好的,我们需要先设置子域约束:)谢谢-刚刚尝试过,但得到了相同的错误。通过在服务器上记录子域发现了错误。由于是.co.uk域,服务器报告的实际子域是“api.X”,而不仅仅是“api”。谢谢你的帮助。没问题-你住在切斯特?我在维拉尔!