10

我使用 Screencast 335 部署到 VPS 教程成功地设置了一个 rails 站点。现在我想在新域上添加另一个 Rails 应用程序,但我对所需的步骤感到困惑。

在上述设置中,sites-available 或 /etc/nginx/nginx.conf 没有任何更改。唯一的配置在我的应用程序配置目录中的 unicorn.rb、unicorn_init.sh 和 nginx.conf 中。nginx.conf 文件如下所示:-

upstream unicorn {
  server unix:/tmp/unicorn.my_app.sock fail_timeout=0;
}
server {
  listen 80 default deferred;
  # server_name my_app.com.au www.my_app.com.au;
  root /var/www/my_app/current/public;
  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }
  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    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;
}

在我的 Capistrano 食谱中,我有这条线

sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"

添加第二个域仅仅是在侦听和取消注释 server_name 部分后删除默认延迟,然后为第二个应用程序使用不同的上游套接字名称和服务器名称重复此配置文件?这会起作用还是我需要将此文件传输到可用站点并创建指向已启用站点的符号链接?

4

3 回答 3

21

在 unicorn.rb 中:

应用1:

root = "/var/www/application_1/current"
working_directory root
pid "#{root}/tmp/pids/unicorn-app1.pid"
listen "/tmp/unicorn.app1.sock"

应用 2:

root = "/var/www/application_2/current"
working_directory root
pid "#{root}/tmp/pids/unicorn-app2.pid"
listen "/tmp/unicorn.app2.sock"

在 nginx.conf 中:

应用1:

upstream app1_server {
  server unix:/tmp/unicorn.app1.sock fail_timeout=0;
}

server {
  listen 80;
  server_name my_app_1.com www.my_app_1.com;
  root /var/www/app1/current/public;

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

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

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

应用 2:

upstream app2_server {
  # point to app2 sock
  server unix:/tmp/unicorn.app2.sock fail_timeout=0;
}

server {
  listen 80;
  server_name my_app_2.com www.my_app_2.com;
  root /var/www/app2/current/public;

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

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

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}
于 2013-01-06T02:04:26.007 回答
4

使用 Nginx 和 Unicorn 在一台主机上托管不同的应用程序真的很容易。

您可以通过定义socket每个应用程序的文件的不同名称来获得分离。当然,您应该current/publicserver.nginx.conf

最后一点是在unicorn_init.sh文件中:在它的顶部,您应该更改应用程序目录APP_ROOT的完整路径。current/public

如果您的设置类似于 RailsCast 的设置,则所有其他事情都由 capistrano 完成。

于 2013-01-04T15:45:01.143 回答
4

我在这里提出的一个问题上回答了这个问题:

使用 nginx + unicorn 的多个 Rails 4 应用程序

但这里有一个答案:

我正在使用Ruby 2.0Rails 4.0。我想你已经安装了 nginx 和 unicorn。那么,让我们开始吧!

在你的 nginx.conf文件中,我们将让 nginx 指向一个独角兽套接字:

upstream unicorn_socket_for_myapp {
  server unix:/home/coffeencoke/apps/myapp/current/tmp/sockets/unicorn.sock fail_timeout=0;
}

然后,在您的服务器侦听端口 80 的情况下,添加一个 location 块,指向您的 rails 应用程序所在的子目录(此代码必须在服务器块内):

location /myapp/ {
    try_files $uri @unicorn_proxy;
  }

  location @unicorn_proxy {
    proxy_pass http://unix:/home/coffeencoke/apps/myapp/current/tmp/sockets/unicorn.sock;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Forwarded-Proto $scheme;
  }

现在您可以将 Unicorn 用作 Deamon:

sudo unicorn_rails -c config/unicorn.rb -D

最后要做的事情,也是我挖掘最多的事情是为您的 rails 路线文件添加一个范围,如下所示:

MyApp::Application.routes.draw do
  scope '/myapp' do
    root :to => 'welcome#home'

    # other routes are always inside this block
    # ...
  end
end

这样,您的应用程序将映射一个链接/myapp/welcome,而不仅仅是/welcome

但还有更好的方法

好吧,以上将在生产服务器上工作,但开发呢?您是否要正常开发,然后在部署时更改您的 rails 配置?对于每个应用程序?那是不需要的。

因此,您需要创建一个新模块,我们将把它放在lib/route_scoper.rb

require 'rails/application'

module RouteScoper
  def self.root
    Rails.application.config.root_directory
  rescue NameError
    '/'
  end
end

之后,在你routes.rb这样做:

require_relative '../lib/route_scoper'

MyApp::Application.routes.draw do
  scope RouteScoper.root do
    root :to => 'welcome#home'

    # other routes are always inside this block
    # ...
  end
end

我们要做的就是看是否指定了根目录,如果是就使用它,否则就到“/”。现在我们只需要指向 config/enviroments/production.rb 上的根目录:

MyApp::Application.configure do
  # Contains configurations for the production environment
  # ...

  # Serve the application at /myapp
  config.root_directory = '/myapp'
end

在 config/enviroments/development.rb 我没有指定 config.root_directory。这样它使用普通的 url 根。

于 2013-08-10T16:57:59.740 回答