Rails路线在开发和生产方面有所不同

时间:2017-02-12 01:08:52

标签: ruby-on-rails

我的开发环境中有一条工作正常的路线,但由于某些原因它不能在生产中工作,我无法弄清楚原因。

当我在开发环境中访问http://localhost:3000/api/v1/register_panelist?api_key=ff4a6fa1c975693bedc2122e6943946b时,它运行良好。

然而,当我转到http://example.com/api/v1/register_panelist?api_key=ff4a6fa1c975693bedc2122e6943946b时,它无法解决,并最终落入未找到的"页面#34;这是捕获我的路线文件底部的所有路线。

在我的routes.rb文件中,我有这个:

constraints(ApiConstraint) do

  namespace :api, defaults: {format: 'json'} do
    namespace :v1, defaults: {format: 'json'} do   

      match "register_panelist", to: "appusers#register_panelist", via: 'get'   
      match "get_surveys", to: "appusers#get_surveys", via: 'get'     

    end
  end

  match "api/v1/*path", to: "api/v1/misc#api_not_found_404", via: :all, format: 'json'
end

...
#at the very bottom
match "*path", to: "static_pages#not_found_404", via: :all, format: false #, :constraints => {:format => [:html, :png]}

在我的开发环境中,似乎可以正确解析。但是在生产中它似乎正在落到routes.rb文件的底部。

任何想法为什么?

编辑:添加日志:

生产:

    production.log — I, [2017-02-12T11:55:31.455331 #27545] INFO -- : Started GET "/api/v1/register_panelist.json?api_key=ff4a6fa1c975693bedc2122e6943946b&country_id=9&birthday_year=1989&birthday_month=2&birthday_day=16&gender=42198&hispanic=42200&zip=10022&state=45700&ethnicity=42215&standard_relationship=42232&standard_education=42243&standard_hhi_us=43511" for 75.100.38.224 at 2017-02-12 11:55:31 +0000
production.log — I, [2017-02-12T11:55:31.606946 #27545] INFO -- : Prod? true
production.log — I, [2017-02-12T11:55:31.607886 #27545] INFO -- : subdomain:
production.log — I, [2017-02-12T11:55:31.607948 #27545] INFO -- : protocol: https://
production.log — I, [2017-02-12T11:55:31.608020 #27545] INFO -- : Prod? true
production.log — I, [2017-02-12T11:55:31.608052 #27545] INFO -- : subdomain:
production.log — I, [2017-02-12T11:55:31.608086 #27545] INFO -- : protocol: https://
production.log — I, [2017-02-12T11:55:31.617812 #27545] INFO -- : Processing by StaticPagesController#not_found_404 as HTML
production.log — I, [2017-02-12T11:55:31.617925 #27545] INFO -- : Parameters: {"api_key"=>"ff4a6fa1c975693bedc2122e6943946b", "country_id"=>"9", "birthday_year"=>"1989", "birthday_month"=>"2", "birthday_day"=>"16", "gender"=>"42198", "hispanic"=>"42200", "zip"=>"10022", "state"=>"45700", "ethnicity"=>"42215", "standard_relationship"=>"42232", "standard_education"=>"42243", "standard_hhi_us"=>"43511", "path"=>"api/v1/register_panelist.json"}
production.log — I, [2017-02-12T11:55:31.636463 #27545] INFO -- : Rendered text template (0.2ms)
production.log — I, [2017-02-12T11:55:31.636883 #27545] INFO -- : Completed 404 Not Found in 19ms (Views: 11.4ms | ActiveRecord: 0.0ms)

发展:

Started GET "/api/v1/register_panelist?api_key=ff4a6fa1c975693bedc2122e6943946b&country_id=9&birthday_year=1989&birthday_month=2&birthday_day=16&gender=42198&hispanic=42200&zip=53593&state=45700&ethnicity=42215&standard_relationship=42232&standard_education=42243&standard_hhi_us=43511" for ::1 at 2017-02-12 05:56:46 -0600
Prod? false
subdomain: 
protocol: http://
Processing by Api::V1::AppusersController#register_panelist as JSON
  Parameters: {"api_key"=>"ff4a6fa1c975693bedc2122e6943946b", "country_id"=>"9", "birthday_year"=>"1989", "birthday_month"=>"2", "birthday_day"=>"16", "gender"=>"42198", "hispanic"=>"42200", "zip"=>"53593", "state"=>"45700", "ethnicity"=>"42215", "standard_relationship"=>"42232", "standard_education"=>"42243", "standard_hhi_us"=>"43511"}
  User Load (18.7ms)  SELECT  "users".* FROM "users" WHERE "users"."api_key" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["api_key", "ff4a6fa1c975693bedc2122e6943946b"]]
来自开发的

rake routes输出:

api_v1_register_panelist GET    /api/v1/register_panelist(.:format)                                                           api/v1/appusers#register_panelist {:format=>"json"}
                                     api_v1_get_surveys GET    /api/v1/get_surveys(.:format)                                                                 api/v1/appusers#get_surveys {:format=>"json"}

和生产服务器:

api_v1_register_panelist GET    /api/v1/register_panelist(.:format)                                                           api/v1/appusers#register_panelist {:format=>"json"}
                                     api_v1_get_surveys GET    /api/v1/get_surveys(.:format)                                                                 api/v1/appusers#get_surveys {:format=>"json"}

2 个答案:

答案 0 :(得分:1)

我猜想Web服务器配置中有一些东西在传递给Rails(mod重写等)之前篡改了请求,或者是Web服务器中Rails执行环境的挂载点设置(例如,Nginx中的Unicorn,Apache中的Passenger),或者WEBrick和您的实时服务器处理不同的中间件。

无论如何,我都会先检查服务器上的路由:

rake routes

并确保它们符合您的期望。接下来,检查rails日志并验证Rails实际上是否正在处理该请求,而不是被Web服务器或某些中间件拦截。如果你将日志级别调高到调试它应该提供有关它如何处理它的详细信息。这至少应该为您提供下一步的目标。

答案 1 :(得分:0)

我不赞成为什么这样做,但似乎我必须更新我的return new LocalSessionFactoryBean().getObject();文件以调出该行两次 - 一次在约束内找到我的开发环境中的路由,一次不用在生产中找到:

public SessionFactory getObject() {
        return this.sessionFactory;
}