1

我有一个 Rails 应用程序,它通过 Capistrano 部署到 VPS,其设置与此 Railscast非常相似。我有 mydomain.co.uk 和 admin.mydomain.co.uk。子域在本地使用 lvh.me 和标准 Webbrick 服务器可以正常工作,但在生产中 admin.mydomain.co.uk 显示的内容与 mydomain.co.uk 完全相同。

我的 routes.rb 文件:

class AdminDomain
  def self.matches?(request)
    puts "Sub = #{request.subdomain}"
    request.subdomain.present? && request.subdomain == "admin"
  end
end

MyApp::Application.routes.draw do

  constraints(AdminDomain) do
    scope :module => "admin" do
      match '', to: 'admin#index'

      resources :users
    end
  end

  # All the mydomain.co.uk routes...

我的 Nginx 配置:

upstream unicorn {
  server unix:/tmp/unicorn.<%= application %>.sock fail_timeout=0;
}

server {
  listen 80;
  root <%= current_path %>/public;

  server_name mydomain.co.uk admin.mydomain.co.uk;

  listen 443 ssl;
  ssl_certificate /home/deployer/mydomain_combined.crt;
  ssl_certificate_key /home/deployer/mydomain.key;
  proxy_set_header X-Forwarded-Proto $scheme;

  auth_basic            "Restricted";
  auth_basic_user_file  htpasswd;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  location = /favicon.ico {
    expires    max;
    add_header Cache-Control public;
  }

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }

  error_page 503 @maintenance;

  location @maintenance {
    rewrite  ^(.*)$  /system/maintenance.html last;
    break;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

我唯一的想法是 Nginx 没有将请求 url 传递给独角兽。我对 SSL 有类似的问题,但通过添加proxy_set_header X-Forwarded-Proto $scheme;. 如何让子域在 Nginx 和 Unicorn 下的生产环境中正常运行?

4

1 回答 1

2

It seems that under my setup in production request.subdomain was set to 'admin.mydomain' whereas in development it was just 'admin'.

Therefore adding this into the routes.rb with a regex works both locally and on my production server:

constraints :subdomain => /admin.*/ do
  scope :module => "admin" do
    root to: 'admin#index'
  end
end
于 2012-09-12T10:04:00.577 回答