Rails 4子域不适用于生产环境 [英] Rails 4 subdomains does not work on production

查看:64
本文介绍了Rails 4子域不适用于生产环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了具有3个子域的Rails 4应用程序.

I've created an Rails 4 application with 3 sub-domains.

开发领域:

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

生产域(带乘客的Ngnix):

Production domains (Ngnix with Passenger):

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

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

My config/routes.rb looks like that:

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虚拟主机文件:

My ngnix virtual host file:

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.",并显示了以下日志信息:

The problem, is that on my dev machine (localhost) or development environment its works very well, as I want. But on production I got the error: "The page you were looking for doesn't exist.", with this log info:

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

I,[2015-07-29T11:31:07.197635#25813] INFO-:开始GET "/en/sign_in"在2015-07-29 11:31:07 +0100 F时为0.0.0.0, [2015-07-29T11:31:07.206758#25813]致命-: ActionController :: RoutingError(没有路由与[GET]"/en/sign_in"匹配): 动作包(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' 动作包(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
call' 栏杆(4.2.3)lib/rails/rack/logger.rb:38:in call_app' railties (4.2.3) lib/rails/rack/logger.rb:20: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'
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
call' 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"(作为同一域).

in other works, account.mydomain.com and api.mydomain.com still acting as my main domain "mydomain.com" (as the same domain).

更新:

我将此代码添加到我的页面上:

I add this code on my page:

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

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

Visiting: http://api.mydomain.co.ao As Result I got:

作为域:"co.ao"

As Domain: "co.ao"

作为子域:"api.mydomain"

As Subdomain: "api.mydomain"

但是我的子域仅是:api

But my subdomain here is only: api

怎么了?

推荐答案

默认

tld_length = 1

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

You need to update tld_length to 2 for domain like: co.in, co.au

尝试:

config.action_dispatch.tld_length = 2

在config/enviroments/production.rb

in config/enviroments/production.rb

参考

这篇关于Rails 4子域不适用于生产环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆