Rails 4 subdomains not working in production
I created a Rails 4 application with three subdomains.
Development domains:
- mydomain.dev
- api.mydomain.dev
- account.mydomain.dev
Production domains (Ngnix with passenger):
- app.mydomain.com (www.mydomain.com and mydomain.com show a different page, not the application)
- api.mydomain.com
- account.mydomain.com
My config / routes.rb looks like this:
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
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 problem is, on my dev machine (localhost) or development environment, it works very well as I want. But in production I got an error: " The page you were looking for doesn't exist.
", with this log information:
Visit: 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 match [GET] "/ ru / sign_in"): actionpack (4.2.3) lib / action_dispatch / middleware / debug_exceptions.rb: 21: in call 'actionpack (4.2.3) lib / action_dispatch / middleware / show_exceptions.rb: 30: in call' railties (4.2.3) lib / rails / rack / logger.rb: 38: in block in call
call'
newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in
call'
newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in
call_app' railties (4.2.3) lib/rails/rack/logger.rb:20:in
in other works, account.mydomain.com and api.mydomain.com still act as my main domain "mydomain.com" (as the same domain).
UPDATE:
I add this code to my page:
<%= request.domain.inspect %><br>
<%= request.subdomain.inspect %>
Visit: http://api.mydomain.co.ao As a result I got:
As domain: "co.ao"
As subdomain: "api.mydomain"
But my subdomain is only here: api
What's wrong?
source to share