【问题标题】:Rails 3 Subdomain with Nginx and Unicorn带有 Nginx 和 Unicorn 的 Rails 3 子域
【发布时间】:2012-09-12 18:04:00
【问题描述】:

我有一个 Rails 应用程序,它通过 Capistrano 部署到 VPS,其设置与this 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 下的生产环境中正常运行?

【问题讨论】:

标签: ruby-on-rails nginx unicorn


【解决方案1】:

在我的生产设置下,request.subdomain 似乎设置为“admin.mydomain”,而在开发中它只是“admin”。

因此,使用正则表达式将其添加到 routes.rb 中既可以在本地工作,也可以在我的生产服务器上工作:

constraints :subdomain => /admin.*/ do
  scope :module => "admin" do
    root to: 'admin#index'
  end
end

【讨论】:

  • 您需要更改 :tld_length 参数,因为您的实时服务器比您的测试服务器更深(.co.uk vs .me)
猜你喜欢
  • 1970-01-01
  • 2013-02-01
  • 2014-01-15
  • 2012-05-07
  • 1970-01-01
  • 2015-05-03
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
相关资源
最近更新 更多