【问题标题】:Rails API Routing issue (only in production with nginx + unicorn)Rails API 路由问题(仅在使用 nginx + unicorn 的生产环境中)
【发布时间】:2014-11-03 22:57:21
【问题描述】:

我已经开发了一个带有 rails 的 api,在 localhost 中一切正常。 但是当我的 api 在 prod 服务器上时,我的路由出现错误...

这里是场景:

  • 我的 prod 服务器配置了 nginx 和 unicorn
  • 我在我的路由中使用子域和版本 (api.servername/v1/resource)

routes.rb 文件:

constraints subdomain: 'api' do
  scope module: 'api' do
    namespace :v1 do

      resources :tests, param: :name do
        member do
          get 'perform'
        end
      end
      resources :jobs

    end
  end
end

nginx 配置文件:

server {
  listen 80;
  server_name *.server.com.br;

  # Application root, as defined previously
  root rails_public_path;

  try_files $uri/index.html $uri @app;

  location @app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://appname;
    proxy_connect_timeout 1800;
    proxy_read_timeout 1800;
  }}

当我在 prod 中运行 rake routes 时,我的路线在那里,但 unicorn 返回 404 页面。

【问题讨论】:

    标签: ruby-on-rails-4 nginx unicorn


    【解决方案1】:

    这似乎是一个 TLD 问题。它不仅在独角兽上。 如果你用相同的域配置你的 /etc/hosts,你会得到同样的错误。

    在 routes.rb 中移除 subdomain 的约束按预期工作。

    routes.rb 文件:

    # constraints subdomain: 'api' do
    scope module: 'api' do
      namespace :v1 do
        resources :tests, param: :name do
          member do
            get 'perform'
          end
        end
        resources :jobs
      end
    end
    # end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-15
      • 2014-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多