Rails 4 个子域在生产环境中不起作用

Rails 4 subdomains does not work on production

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

开发领域:

生产域(带 Passenger 的 Ngnix):

我的 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;
}

问题是,在我的开发机器(本地主机)或开发环境中,它运行良好,正如我所希望的那样。但是在生产中我得到了错误:“The page you were looking for doesn't exist.”,日志信息是:

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

I, [2015-07-29T11:31:07.197635 #25813] INFO -- : Started 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] FATAL -- : ActionController::RoutingError (No route matches [GET] "/en/sign_in"): actionpack (4.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in call'<br> newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:incall' actionpack (4.2.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in call'<br> newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:incall' railties (4.2.3) lib/rails/rack/logger.rb:38:in call_app' railties (4.2.3) lib/rails/rack/logger.rb:20:inblock in call'

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

更新:

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

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

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

As Domain: "co.ao"

As Subdomain: "api.mydomain"

但是我这里的子域名只有:api

怎么了?

默认

tld_length = 1

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

尝试:

config.action_dispatch.tld_length = 2

在config/enviroments/production.rb

Reference