Rails子域路由

时间:2017-01-09 21:53:39

标签: 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    :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

constraints subdomain: '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_subdomain类,如下所示:

# config/routes.rb
constraints(AdminSubdomain) do
  constraints subdomain: 'admin' do
    [...] # routes same as above
  end
 end

所有非子域名页面都没有被路由约束覆盖,我首先在分段部署中意识到了这个问题。当我遇到同样的问题,没有链接到正确的子域。这是我认为可能是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;

并且只是完全注释掉server_name,正如您在nginx.conf

中看到的那样

当我跑去耙路线'我明白了: Rake Routes:: Terminal Output

我认为这可能是一个dev-env / staging-env问题,在查看了其他堆栈问题之后,我已添加:

# development.rb (added)
config.action_dispatch.tld_length = 0

# staging.rb (added)
config.action_dispatch.tld_length = 2

我似乎无法弄清楚我做错了什么似乎没有正常工作,而我刚刚开始做我喜欢称之为变异的圈子。 '

资源检查:

How to access subdomains

Rails tutorial for subdomains

Devise specific subdomain routing

Subdomain NameSpace Error

Rails subdomain redirection

Devise subdomain routes rails 4

项目规格:

rails版本:5.0.0.1

ruby​​版本:2.3.0

美洲狮版本:3.6.2

设计版本:4.2.0

使用ActiveRecord

我已经和RoR一起工作了大约3个月了,并且已经能够解决大多数问题。我在解决这个子域问题的3天内,似乎无法确定问题。

0 个答案:

没有答案