簡體   English   中英

Rails的開發和生產路線不同

[英]Rails Routes Different In Development and Production

我的路由在我的開發環境中工作得很好,但是由於某種原因,它在生產中不起作用,我不知道為什么。

當我在開發環境中轉到http://localhost:3000/api/v1/register_panelist?api_key=ff4a6fa1c975693bedc2122e6943946b時,效果很好。

但是,當我轉到http://example.com/api/v1/register_panelist?api_key=ff4a6fa1c975693bedc2122e6943946b它無法解析,最終掉落到“找不到頁面”,這是捕獲所有路由的底部路由文件。

在我的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"}

我猜想在Web服務器配置中某些東西會在將請求傳遞給Rails之前篡改請求(mod重寫等),或者Web服務器中Rails執行環境的安裝點設置(例如,Unicorn in Nginx,Apache中的Passenger),或者也許是由WEBrick和您的實時服務器以不同方式處理的中間件。

無論如何,我將從檢查服務器上的路由開始:

rake routes

並確保它們是您期望的。 接下來,檢查Rails日志並確認該請求實際上是由Rails處理的,並且未被Web服務器或某些中間件攔截。 如果您提高日志級別以進行調試,則應提供有關其處理方式的詳細信息。 這至少應該為您提供下一步查找的位置。

我不太肯定為什么這樣做,但似乎我不得不更新我的routes.rb文件兩次以調出該行-一次是在約束中找到我的開發環境中的路由,一次是在生產環境中找不到:

constraints(ApiConstraint) do

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

      #find in development
      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

#find in production
match "api/v1/register_panelist", to: "api/v1/appusers#register_panelist", via: 'get'   
match "api/v1/get_surveys", to: "api/v1/appusers#get_surveys", via: 'get' 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM