I'm trying to setup subdomain routing for an admin page. Yet when using admin.localhost I'm able to route to the subdomain just fine. When I use lvh.me I'm routed to the correct home page, although when using admin.lvh.me I'm routing to the same home page and not the admin login page.
# config/routes.rb
constraints(AdminSubdomain) do
constraints subdomain: 'admin' do
devise_for :admins
devise_scope :admin do
authenticated :admin do
root to: 'admin_home#index', as: :authenticated_root
end
unauthenticated do
root to: 'devise/sessions#new', as: :unauthenticated_root
end
end
resources :example
resources :example
end
end
Other routing subdomain parameters I've tried
module: "admin", path: "/", constraints: lambda { |r| r.subdomain.split('.')[0] == 'admin' } do
[...] # Additional routes
end
and
constraints subdomain: 'admin' do
[...] # Additional routes
end
Then for for the AdminSubdomain class
# lib/admin_subdomain.rb
class AdminSubdomain
def self.matches?(request)
case request.subdomain
when 'admin'
true
else
false
end
end
end
I've also tried variations on the admin_subdomain.rb
# lib/admin_subdomain.rb
class AdminSubdomain
def self.matches?(request)
case request.subdomain
when 'www', '', nil
true
else
false
end
end
end
Then wrapping the admin_subdomain class like so:
# config/routes.rb
constraints(AdminSubdomain) do
constraints subdomain: 'admin' do
[...] # routes same as above
end
end
All non-subdomain pages aren't covered by routing constraint, I first realized the problem during a staging deploy. When I encountered the same problem of not linking to the correct subdomain. This is when I believed it might be the NGINX proxy running a muck.
# NGINX.conf {appname} == actual name
upstream puma {
server unix:///home/deploy/{appname}/shared/tmp/sockets/{appname}-puma.sock;
}
server {
listen 80;
listen [::]:80;
# server_name localhost;
root /home/deploy/{appname}/current/public;
access_log /home/deploy/{appname}/current/log/nginx.access.log;
error_log /home/deploy/{appname}/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @puma;
location @puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
I've tried multiple nginx configs including adding
# wildcard nginx server_name
server_name example.com *.example.com;
# localhost server_name
server_name localhost;
and just commenting out server_name altogether as you can see above in the nginx.conf
When I run 'rake routes' I get:
I thought this might be a dev-env/staging-env issue, after looking through other stack questions, I've added:
# development.rb (added)
config.action_dispatch.tld_length = 0
# staging.rb (added)
config.action_dispatch.tld_length = 2
I can't seem to figure out what I'm doing wrong as nothing seems to be working correctly, and I've just started doing what I like to call 'variation circles.'
Resources Checked:
Devise specific subdomain routing
Devise subdomain routes rails 4
Project Specifications:
rails version: 5.0.0.1
ruby version: 2.3.0
puma version: 3.6.2
devise version: 4.2.0
Utilizing ActiveRecord
I've been working with RoR for about 3 months now, and have been able to sort my way though most issues. I'm 3 days deep into fixing this subdomain issue and I can't seem to determine the problem.