Ruby on rails Rails子域路由
我正在尝试为管理员页面设置子域路由。然而,当使用admin.localhost时,我能够很好地路由到子域。当我使用lvh.me时,我被路由到正确的主页,尽管当使用admin.lvh.me时,我被路由到相同的主页,而不是管理员登录页面Ruby on rails Rails子域路由,ruby-on-rails,nginx,devise,routing,subdomain,Ruby On Rails,Nginx,Devise,Routing,Subdomain,我正在尝试为管理员页面设置子域路由。然而,当使用admin.localhost时,我能够很好地路由到子域。当我使用lvh.me时,我被路由到正确的主页,尽管当使用admin.lvh.me时,我被路由到相同的主页,而不是管理员登录页面 # config/routes.rb constraints(AdminSubdomain) do constraints subdomain: 'admin' do devise_for :admins devise_scope :
# 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
我尝试过的其他路由子域参数
module: "admin", path: "/", constraints: lambda { |r| r.subdomain.split('.')[0] == 'admin' } do
[...] # Additional routes
end
及
然后是AdminSubdomain类
# lib/admin_subdomain.rb
class AdminSubdomain
def self.matches?(request)
case request.subdomain
when 'admin'
true
else
false
end
end
end
我还尝试了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
然后像这样包装admin_子域类:
# config/routes.rb
constraints(AdminSubdomain) do
constraints subdomain: 'admin' do
[...] # routes same as above
end
end
所有非子域页面都不受路由约束的影响,我首先在一次登台部署期间意识到了这个问题。当我遇到同样的问题,没有链接到正确的子域。这是我认为它可能是运行了muck的NGINX代理的时候
# 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;
}
我尝试了多个nginx配置,包括添加
# wildcard nginx server_name
server_name example.com *.example.com;
# localhost server_name
server_name localhost;
正如您在nginx.conf中看到的那样,只需注释掉服务器名称即可
当我运行“rake routes”时,我得到:
我认为这可能是一个dev env/staging env问题,在查看了其他堆栈问题之后,我添加了:
# development.rb (added)
config.action_dispatch.tld_length = 0
# staging.rb (added)
config.action_dispatch.tld_length = 2
我似乎不知道自己做错了什么,因为似乎没有什么东西能正常工作,我刚刚开始做我喜欢称之为“变异圈”的事情
已检查的资源:
项目规格:
rails版本:5.0.0.1
ruby版本:2.3.0
彪马版本:3.6.2
设计版本:4.2.0
利用ActiveRecord
我已经和RoR一起工作了大约3个月了,并且能够解决大多数问题。我已经花了3天时间解决这个子域问题,但我似乎无法确定问题所在
# development.rb (added)
config.action_dispatch.tld_length = 0
# staging.rb (added)
config.action_dispatch.tld_length = 2