Ruby on rails 多个子域继续重定向到根路径

Ruby on rails 多个子域继续重定向到根路径,ruby-on-rails,nginx,Ruby On Rails,Nginx,我有一个Rails应用程序在Digital Ocean上运行,Unicorn和NGINX在Ubuntu上运行,我正在尝试处理一些子域,如app1.example.com、app2.example.com等 在我的路线中,我这样做: constraints(Subdomain) do match '/', to: 'pages#landing', via: [:get, :post] end 这有助于我捕获子域前缀,并从控制器显示相应的登录页。在本地,它工作得很好,但无论发生什么,NGINX

我有一个Rails应用程序在Digital Ocean上运行,Unicorn和NGINX在Ubuntu上运行,我正在尝试处理一些子域,如app1.example.com、app2.example.com等

在我的路线中,我这样做:

constraints(Subdomain) do
  match '/', to: 'pages#landing', via: [:get, :post]
end
这有助于我捕获子域前缀,并从控制器显示相应的登录页。在本地,它工作得很好,但无论发生什么,NGINX似乎都会重定向到应用程序的根路径

这是我的NGINX配置:

upstream app_server {
  server unix:/var/run/unicorn.sock fail_timeout=0;
}

server {
  listen 80;
  server_name example.com *.example.com;
  return 301 https://$server_name$request_uri;
}

#When the wildcard above didn't work, 
#I tried hardcoding below but still nothing
server {
  listen 80;
  server_name app1.example.com;
  return 301 https://$server_name$request_uri;
}


server {
  root /home/rails/example/public;
  index index.htm index.html;
  listen 443 ssl spdy;
  listen [::]:443 ssl spdy;
  ...
  ssl_protocols TLSv1.1 TLSv1.2;
  # ssl_ciphers
  ssl_prefer_server_ciphers on;
  add_header Strict-Transport-Security max-age=15768000;
  ssl_stapling on;
  ...
  resolver 8.8.8.8 8.8.4.4 valid=86400;
  resolver_timeout 10;

  location / {
    try_files $uri/index.html $uri.html $uri @app;
  }

  location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
    try_files $uri @app;
  }

  location @app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
  }
}

这里缺少什么?

我认为应该使用$host变量而不是$server\u name,因为$server\u name可能只是主机名列表中的第一个值,并且您希望使用用户在http头中指定的主机名

server {
  listen 80;
  server_name example.com *.example.com;
  return 301 https://$host$request_uri;
}
另外,如果您的硬编码示例位于通配符服务器块之后,那么它将永远不会被匹配,因为请求将始终与它之前的请求匹配,这可能就是它不起作用的原因


这就是你所需要的。您不需要重定向以接受子域,也不需要使用通配符。并删除“return”s

如果任何人存在所有子域路由重定向到根路径的问题(在本地主机和生产环境中),请检查您的子域是否在
routes.rb
中的根域之前定义。否则,它们将始终重定向到登录页

✅ 正确示例
routes.rb
#所有子域优先
获取“/”,到:'pages#status',约束:{subdomain:'status'}
#主页(登录页)
根目录为:'pages#landing',如:'landing'
❌ 这行不通<代码>路由.rb
#登录页
根目录为:'pages#landing',如:'landing'
#子域=>将重定向到登录页。。。
获取“/”,到:'pages#status',约束:{subdomain:'status'}

这似乎是为了我!现在转到ssl;非常感谢。
server {
  listen 80;
  server_name example.com;
}