问题描述
我创建了具有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
在其他作品中,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.aoAs Result I got:
作为子域:"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子域不适用于生产环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!