Rails 4子域不适用于生产

时间:2015-07-29 10:34:53

标签: ruby-on-rails ruby-on-rails-4 nginx subdomain passenger

我创建了一个包含3个子域的Rails 4应用程序。

开发领域:

  • mydomain.dev
  • api.mydomain.dev
  • account.mydomain.dev

生产域(Ngnix with Passenger):

  • app.mydomain.com(www.mydomain.com和mydomain.com显示其他页面,而不是应用程序)
  • api.mydomain.com
  • account.mydomain.com

我的config / routes.rb看起来像这样:

Rails.application.routes.draw do
    namespace :api, constraints: { subdomain: 'api' }, path: '/', defaults: { format: :json } do
        namespace :v1 do
            resources :clients, only: [:create, :show]
        end
    end

    namespace :account, constraints: { subdomain: 'account' }, path: '/' do
        get '/:locale' => 'welcome#index', locale: /en|pt/
        root 'welcome#index'

        scope "(:locale)", locale: /en|pt/ do
            get :sign_in, to: 'sessions#new'
            post :sign_in, to: 'sessions#create'
        end
    end

    get '/:locale' => 'welcome#index', locale: /en|pt/
    root 'welcome#index'

    scope "(:locale)", locale: /en|pt/ do
        resource :session, only: [:new, :create, :destroy]
        get :login, to: 'sessions#new', as: :login
    end
end

我的ngnix虚拟主机文件:

server {
    listen 80;
    listen [::]:80;

    # SSL configuration
    #
    listen 443 ssl;
    listen [::]:443 ssl;

    server_name mydomain.com api.mydomain.com account.mydomain.com;

    passenger_enabled on;
    passenger_ruby /home/username/.rvm/gems/ruby-2.2.2@myapp/wrappers/ruby;

    root /var/sites/mydomain.com/current/public;

    ssl on;
    ssl_certificate /etc/nginx/ssl/mydomain.com/server.crt;
    ssl_certificate_key /etc/nginx/ssl/mydomain.com/server.key;
}

问题是,在我的开发机器(localhost)或开发环境中,它的工作非常好,正如我想的那样。但是在制作时我得到了错误:" The page you were looking for doesn't exist.",有这个日志信息:

访问:https://account.mydomain.com/en/sign_in

  

I,[2015-07-29T11:31:07.197635#25813]信息 - :开始GET   " / EN / sign_in" for 0.0.0.0 at 2015-07-29 11:31:07 +0100 F,   [2015-07-29T11:31:07.206758#25813]致命 - :   ActionController :: RoutingError(没有路由匹配[GET]" / en / sign_in"):   actionpack(4.2.3)   lib / action_dispatch / middleware / debug_exceptions.rb:21:in call'
newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in
call'   actionpack(4.2.3)   lib / action_dispatch / middleware / show_exceptions.rb:30:in call'
newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in
来电'   railties(4.2.3)lib / rails / rack / logger.rb:38:in call_app' railties (4.2.3) lib/rails/rack/logger.rb:20:in阻止通话'

在其他作品中,account.mydomain.com和api.mydomain.com仍然作为我的主域名" mydomain.com" (作为相同的域名)。

更新:

我在我的页面上添加了这段代码:

<%= request.domain.inspect %><br>
<%= request.subdomain.inspect %>

访问:http://api.mydomain.co.ao 结果我得到了:

  

As Domain:&#34; co.ao&#34;

     

作为子域:&#34; api.mydomain&#34;

但我的子域名只有:api

有什么问题?

1 个答案:

答案 0 :(得分:9)

默认

tld_length = 1

您需要将域名的tld_length更新为2,例如:co.in,co.au

尝试:

config.action_dispatch.tld_length = 2

在config / enviroments / production.rb

Reference